SpringBoot 使用(一): 初探

最近兩年,各種互聯網公司雨后春筍般的出來,各種B輪 C輪的公司都慢慢成長起來,早期為了快速上線,快速試錯, 以及 人員技術棧問題使用的PHP,C#編寫系統迫切需要用JAVA進行重構。微服務在這種情況被各種互聯網企業(yè)撿起來,小團隊, 獨立部署無影響,不限制人員技術棧,而SpringBoot乃是微服務架構的絕配,作為一個有理想的好孩子,開始研究Spring Boot原理及其在各種場景下的使用, 各位大神可以輕拍, 請勿罵人~
閑言少敘, SpringBoot 使用簡單, 自帶tomcat 或者 jetty 容易啟動。本文以查詢用戶信息為例,生成restful接口, 只需要幾步即可。

1. pom.xml 配置

<parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.2.6.RELEASE</version>
 <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2. 創(chuàng)建UserController.java

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping(value = "/demo.do", method = RequestMethod.GET)
    String  getDemo(){
        return  "Hello UserController";
    }
}

3. 創(chuàng)建DemoApplication.java

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

4. 啟動

mvn  spring-boot:run 
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.2.6.RELEASE)

2015-12-15 15:27:29.311  INFO 2154 --- [           main] demo.DemoApplication                     : Starting DemoApplication on sunjiedeMacBook-Pro.local with PID 2154 (/Users/admin/work/ideawork/SpringBootBaseDemo/target/classes started by sunjie in /Users/admin/work/ideawork/SpringBootBaseDemo)
2015-12-15 15:27:29.461  INFO 2154 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2ba8209b: startup date [Tue Dec 15 15:27:29 CST 2015]; root of context hierarchy
2015-12-15 15:27:31.526  INFO 2154 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2015-12-15 15:27:31.624  INFO 2154 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'entityLinksPluginRegistry': replacing [Root bean: class [org.springframework.plugin.core.support.PluginRegistryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Root bean: class [org.springframework.plugin.core.support.PluginRegistryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
2015-12-15 15:27:31.624  INFO 2154 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'controllerEntityLinks': replacing [Root bean: class [org.springframework.hateoas.core.ControllerEntityLinksFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Root bean: class [org.springframework.hateoas.core.ControllerEntityLinksFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
2015-12-15 15:27:31.624  INFO 2154 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'delegatingEntityLinks': replacing [Root bean: class [org.springframework.hateoas.core.DelegatingEntityLinks]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=true; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Root bean: class [org.springframework.hateoas.core.DelegatingEntityLinks]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=true; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
2015-12-15 15:27:32.611  INFO 2154 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2015-12-15 15:27:33.009  INFO 2154 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2015-12-15 15:27:33.011  INFO 2154 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.26
2015-12-15 15:27:33.135  INFO 2154 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2015-12-15 15:27:33.136  INFO 2154 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3678 ms
2015-12-15 15:27:33.877  INFO 2154 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2015-12-15 15:27:33.884  INFO 2154 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2015-12-15 15:27:33.885  INFO 2154 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2015-12-15 15:27:34.196  INFO 2154 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2ba8209b: startup date [Tue Dec 15 15:27:29 CST 2015]; root of context hierarchy
2015-12-15 15:27:34.272  INFO 2154 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2015-12-15 15:27:34.272  INFO 2154 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
2015-12-15 15:27:34.308  INFO 2154 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-12-15 15:27:34.308  INFO 2154 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-12-15 15:27:34.359  INFO 2154 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-12-15 15:27:34.577  INFO 2154 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2015-12-15 15:27:34.647  INFO 2154 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2015-12-15 15:27:34.649  INFO 2154 --- [           main] demo.DemoApplication                     : Started DemoApplication in 6.002 seconds (JVM running for 6.742)

5. 調用

http://localhost:8080/user/demo.do

6. 后記

SpringBoot demo的例子雖看起來十分簡單, 但是原理確是值得研究,如何啟動, 怎么區(qū)分是否啟動web工程, 怎么和Spring交互等等,這個在后面的原理分析的文章中做細聊。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容