最近两年,各种互联网公司雨后春笋般的出来,各种B轮 C轮的公司都慢慢成长起来,早期为了快速上线,快速试错, 以及 人员技术栈问题使用的PHP,C#编写系统迫切需要用JAVA进行重构。微服务在这种情况被各种互联网企业捡起来,小团队, 独立部署无影响,不限制人员技术栈,而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. 创建UserController.java
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping(value = "/demo.do", method = RequestMethod.GET)
String getDemo(){
return "Hello UserController";
}
}
3. 创建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的例子虽看起来十分简单, 但是原理确是值得研究,如何启动, 怎么区分是否启动web工程, 怎么和Spring交互等等,这个在后面的原理分析的文章中做细聊。