写在前面:
为什么使用Spring Boot?
1.现在的项目使用tomcat+springMVC进行构建.每次搭建一个新的环境都需要重新配置tomcat等等一堆东西.水平扩展性比较差.
2.Spring Boot 可以简单的一个java -jar就能运行.后续结合容器技术部署更为方便.开发,运行,调试也更为方便.
0.环境说明:
Java 1.7 + Eclipse 4.3 + maven 3.3.3 + Spring Boot1.3.1.RELEASE
1.构建maven项目
eclipse:
file -> new -> project -> maven project -> quick start
填写各种名称
配置jdk版本为1.7
2.加入Spring Boot相关配置
org.springframework.boot spring-boot-starter-parent 1.3.1.RELEASE org.springframework.boot spring-boot-starter-web
3.编写Boot类
import java.util.Arrays;import org.springframework.context.ApplicationContext;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * boot class * * @author BeeNoisy */@SpringBootApplicationpublic class Boot { public static void main( String[] args) { ApplicationContext ctx = SpringApplication.run(Boot.class, args); System.out.println("Let's inspect the beans provided by Spring Boot:"); }}
4.编写Controller类
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;/** * boot * * @author BeeNoisy */@Controller@EnableAutoConfigurationpublic class HelloController { @RequestMapping("/") @ResponseBody public String hello() { return "Hello spring boot"; }}
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;/** * rest controller * * @author ziyi.wang */@Controller@EnableAutoConfiguration@RequestMapping("rest")public class RestController { @RequestMapping(method = RequestMethod.GET) @ResponseBody public String get() { return "RestController.get()"; } @RequestMapping(method = RequestMethod.POST) public String post() { return "RestController.post()"; }}
5.运行
直接运行Boot类:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.3.1.RELEASE)2016-01-14 16:20:04.721 INFO 21956 --- [ main] com.didichuxing.didi.cx.app.boot.Boot : Starting Boot on Keen with PID 21956 (C:\dev\workspace\didi-cx-app-spring-boot\target\classes started by BeeNoisy in C:\dev\workspace\didi-cx-app-spring-boot)2016-01-14 16:20:04.723 INFO 21956 --- [ main] com.didichuxing.didi.cx.app.boot.Boot : No active profile set, falling back to default profiles: default2016-01-14 16:20:04.764 INFO 21956 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@78fd4889: startup date [Thu Jan 14 16:20:04 CST 2016]; root of context hierarchy2016-01-14 16:20:05.750 INFO 21956 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: 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]]2016-01-14 16:20:06.290 INFO 21956 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)2016-01-14 16:20:06.299 INFO 21956 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat2016-01-14 16:20:06.300 INFO 21956 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.302016-01-14 16:20:06.372 INFO 21956 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2016-01-14 16:20:06.372 INFO 21956 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1610 ms2016-01-14 16:20:06.652 INFO 21956 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]2016-01-14 16:20:06.656 INFO 21956 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]2016-01-14 16:20:06.657 INFO 21956 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2016-01-14 16:20:06.657 INFO 21956 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]2016-01-14 16:20:06.657 INFO 21956 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]2016-01-14 16:20:06.803 INFO 21956 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@78fd4889: startup date [Thu Jan 14 16:20:04 CST 2016]; root of context hierarchy2016-01-14 16:20:06.871 INFO 21956 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.didichuxing.didi.cx.app.boot.controller.HelloController.hello()2016-01-14 16:20:06.875 INFO 21956 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/rest],methods=[GET]}" onto public java.lang.String com.didichuxing.didi.cx.app.boot.controller.RestController.get()2016-01-14 16:20:06.875 INFO 21956 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/rest],methods=[POST]}" onto public java.lang.String com.didichuxing.didi.cx.app.boot.controller.RestController.post()2016-01-14 16:20:06.877 INFO 21956 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)2016-01-14 16:20:06.878 INFO 21956 --- [ 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,javax.servlet.http.HttpServletResponse)2016-01-14 16:20:06.907 INFO 21956 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-01-14 16:20:06.907 INFO 21956 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-01-14 16:20:06.946 INFO 21956 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-01-14 16:20:07.067 INFO 21956 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2016-01-14 16:20:07.144 INFO 21956 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)2016-01-14 16:20:07.148 INFO 21956 --- [ main] com.didichuxing.didi.cx.app.boot.Boot : Started Boot in 2.681 seconds (JVM running for 2.896)Let's inspect the beans provided by Spring Boot:
服务器启动完毕,看倒数第二行:Tomcat started on port(s): 8080 (http)
然后测试即可.
curl http://127.0.0.1:8080/ Hello spring boot curl http://127.0.0.1:8080/restRestController.get()
至此,SpringBoot的Demo工程搭建完毕.
参考:
http://projects.spring.io/spring-boot/#quick-start
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/