spring boot系列一 demo测试

来源:互联网 发布:宁波奥派网络 编辑:程序博客网 时间:2024/06/03 18:05

1.  参考official  guide

http://projects.spring.io/spring-boot/#quick-start


2.  在eclipse  新建  maven工程,具体细节skip,完成后如下所示:


3. POM 文件配置如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.springboot</groupId><artifactId>springboot</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>springboot</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><strong><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.2.3.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></strong></project>

maven自动load依赖jar包,jdk应该是要求1.8 version 以上

4.  测试类

package hello;import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*;import org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;@Controller@EnableAutoConfigurationpublic class SampleController {    @RequestMapping("/")    @ResponseBody    String home() {        return "Hello World!";    }    public static void main(String[] args) throws Exception {        SpringApplication.run(SampleController.class, args);    }}

5.  启动测试类,log如下:

  .   ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/  ___)| |_)| | | | | || (_| |  ) ) ) )  '  |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::        (v1.2.3.RELEASE)2015-05-05 15:40:33.058  INFO 12356 --- [           main] com.springboot.SampleController          : Starting SampleController on A7P-53352-RUHAN with PID 12356 (D:\Workbench\springboot\target\classes started by ruhan in D:\Workbench\springboot)2015-05-05 15:40:33.096  INFO 12356 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6ed3ccb2: startup date [Tue May 05 15:40:33 CST 2015]; root of context hierarchy2015-05-05 15:40:33.685  INFO 12356 --- [           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-05-05 15:40:34.646  INFO 12356 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)2015-05-05 15:40:34.879  INFO 12356 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat2015-05-05 15:40:34.881  INFO 12356 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.202015-05-05 15:40:35.040  INFO 12356 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext2015-05-05 15:40:35.040  INFO 12356 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1947 ms2015-05-05 15:40:35.834  INFO 12356 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]2015-05-05 15:40:35.838  INFO 12356 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]2015-05-05 15:40:35.839  INFO 12356 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2015-05-05 15:40:36.150  INFO 12356 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6ed3ccb2: startup date [Tue May 05 15:40:33 CST 2015]; root of context hierarchy2015-05-05 15:40:36.235  INFO 12356 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto java.lang.String com.springboot.SampleController.home()2015-05-05 15:40:36.238  INFO 12356 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" 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-05-05 15:40:36.238  INFO 12356 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)2015-05-05 15:40:36.280  INFO 12356 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2015-05-05 15:40:36.280  INFO 12356 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2015-05-05 15:40:36.327  INFO 12356 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2015-05-05 15:40:36.415  INFO 12356 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup2015-05-05 15:40:36.472  INFO 12356 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer <strong><span style="color:#ff0000;">: Tomcat started on port(s): 808</span></strong>0 (http)2015-05-05 15:40:36.474  INFO 12356 --- [           main] com.springboot.SampleController          : Started SampleController in 3.743 seconds (JVM running for 4.443)2015-05-05 15:41:49.620  INFO 12356 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'2015-05-05 15:41:49.620  INFO 12356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started2015-05-05 15:41:49.653  INFO 12356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 33 ms

1. 我们看到spring 集成了tomcat,监听端口为8080,访问tomcate 

http://localhost:8080 返回结果


这是我们测试类预期的结果。



    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }


6  总结

spring boot是什么?有什么特点?


  • Create stand-alone Spring applications
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
  • Provide opinionated 'starter' POMs to simplify your Maven configuration
  • Automatically configure Spring whenever possible
  • Provide production-ready features such as metrics, health checks and externalized configuration
  • Absolutely no code generation and no requirement for XML configuration

其余部分有待进一步研究



0 0
原创粉丝点击