博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个最小化的SpringBoot项目
阅读量:6425 次
发布时间:2019-06-23

本文共 8835 字,大约阅读时间需要 29 分钟。

项目结构

项目基于Maven管理,注意使用了父pom

org.springframework.boot
spring-boot-starter-parent
1.3.0.RELEASE

 

pom.xml

 

4.0.0
test.demo
springboot
0.0.1-SNAPSHOT
jar
springboot
org.springframework.boot
spring-boot-starter-parent
1.3.0.RELEASE
UTF-8
org.springframework.boot
spring-boot-starter-web

  App.class

package hello;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;@EnableAutoConfiguration@RestControllerpublic class App{    public static void main(String[] args)    {        SpringApplication.run(App.class, args);    }    @RequestMapping(value = "/test", method = RequestMethod.GET)    public String get()    {        return "get";    }    @RequestMapping(value = "/post", method = RequestMethod.POST)    public String post()    {        return "post";    }}

@EnableAutoConfiguration注解的作用在于让 Spring Boot 根据应用所声明的依赖来对 Spring 框架进行自动配置

@EnableAutoConfiguration注解上注释的翻译

自动配置Spring上下文,企图猜测所需要的Bean,将classpath和你所定义的Bean加入到IOC容器,比如你有tomcat-embedded.jar在你的classpath上

那么看起来你需要一个TomcatEmbeddedServletContainerFactory,那么SpringBoot会给你生成一个这个类的Bean(除非你已经生成这个类的Bean)

你可以使用exclude来指定那些Bean不需要SpringBoot帮你生成Bean

public @interface EnableAutoConfiguration {    /**     * Exclude specific auto-configuration classes such that they will never be applied.     * @return the classes to exclude     */    Class
[] exclude() default {}; /** * Exclude specific auto-configuration class names such that they will never be * applied. * @return the class names to exclude * @since 1.3.0 */ String[] excludeName() default {};}

在你的root package上使用这个注解,它会将这个目录下所有的子目录和类扫描一遍

@RestController和@RequestMapper注解由SpringMvc提供,用于创建Rest服务

关于@RequestMapper注解

value表示路径映射,method说明请求这个路径的Http方法

启动日志解读

2016-03-11 13:58:02.399  INFO 5776 --- [           main] hello.App                                : Starting App on zzzzzz with PID 5776 (E:\space\spring\springboot\target\classes started by zzzzz in E:\space\spring\springboot)2016-03-11 13:58:02.404  INFO 5776 --- [           main] hello.App                                : No profiles are active2016-03-11 13:58:02.508  INFO 5776 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@481a15ff: startup date [Fri Mar 11 13:58:02 CST 2016]; root of context hierarchy2016-03-11 13:58:03.807  INFO 5776 --- [           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-03-11 13:58:05.107  INFO 5776 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)2016-03-11 13:58:05.126  INFO 5776 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat2016-03-11 13:58:05.128  INFO 5776 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.282016-03-11 13:58:05.268  INFO 5776 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext2016-03-11 13:58:05.268  INFO 5776 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2769 ms2016-03-11 13:58:05.673  INFO 5776 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]2016-03-11 13:58:05.680  INFO 5776 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]2016-03-11 13:58:05.681  INFO 5776 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2016-03-11 13:58:05.681  INFO 5776 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]2016-03-11 13:58:05.682  INFO 5776 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]2016-03-11 13:58:06.022  INFO 5776 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@481a15ff: startup date [Fri Mar 11 13:58:02 CST 2016]; root of context hierarchy2016-03-11 13:58:06.115  INFO 5776 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test],methods=[GET]}" onto public java.lang.String hello.App.get()2016-03-11 13:58:06.116  INFO 5776 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/post],methods=[POST]}" onto public java.lang.String hello.App.post()2016-03-11 13:58:06.121  INFO 5776 --- [           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-03-11 13:58:06.121 INFO 5776 --- [ 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)2016-03-11 13:58:06.178 INFO 5776 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-03-11 13:58:06.178 INFO 5776 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-03-11 13:58:06.225 INFO 5776 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-03-11 13:58:06.363 INFO 5776 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2016-03-11 13:58:06.468 INFO 5776 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)2016-03-11 13:58:06.473 INFO 5776 --- [ main] hello.App : Started App in 4.736 seconds (JVM running for 5.798)

 注意查看红色的部分,dispatcherServlet是SpringWeb的核心Servlet,所有进入SpringWeb的请求都由这个Servelt的service方法进入

2016-03-11 13:58:06.115  INFO 5776 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test],methods=[GET]}" onto public java.lang.String hello.App.get()2016-03-11 13:58:06.116  INFO 5776 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/post],methods=[POST]}" onto public java.lang.String hello.App.post() 表示我们声明的路径映射
posted on
2016-03-11 14:10 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/shuiyonglewodezzzzz/p/5265457.html

你可能感兴趣的文章
java编程之:生成rsa密钥
查看>>
★如何证明自己不是精神病?
查看>>
来自数学君的羊年祝福
查看>>
mongoDB报错Cannot find module '../build/Release/bson'
查看>>
又一款开源手机要来了 —— WiPhone
查看>>
跨越鸿沟——工业大数据的实践与思考
查看>>
DBA和开发同事的一些代沟(五)
查看>>
【OGG】关于在一套复制环境中使用不同版本OGG的问题
查看>>
大咖丨交通运输部科学研究院:交通运输大数据的基础环境正日益成熟-清数•思享会...
查看>>
nginx解析配置文件代码备忘
查看>>
IE10浏览器的hack解决方法
查看>>
NiuTrans 统计机器翻译开源系统
查看>>
haproxy配置文件
查看>>
Exchange Server 2013系统要求
查看>>
zabbix监控项
查看>>
Hbase体系结构理解
查看>>
禁止空格提交表单的js代码
查看>>
web安全、XSS、CSRF、注入攻击、文件上传漏洞
查看>>
zabbix用自带的模板监控mysql
查看>>
OK,生命有你完美的刚好。
查看>>