Spring Boot 优点
-
轻量化
-
提供 Spring 框架各种默认配置来简化项目配置
-
内嵌 Web 容器
-
没有冗余代码生成和XML配置要求
Maven 导包
-
spring-boot-starter:核心模块,包括了自动配置支持、日志和YAML
-
spring-boot-starter-test:测试模块,包括JUnit、Hamcrest、Mockito
-
spring-boot-starter-web:Web模块
一个 Spring Boot 案例应该包括四个部分
-
主加载类
-
逻辑实现类
-
单元测试类
-
以及资源配置文件。
创建SpringBoot 很简单 ,直接用idea的选择web工程就行
Spirng Boot 启动debug日志阅读笔记
1、b.l.ClasspathLoggingApplicationListener :先输出使用的各种jar包
2、 一个呆萌的SpringBoot 代表
3、Starting SpringbootlearnApplication on ZhangWenLiang with PID 7664 会启动一个java线程 PID 为7664 ,随机的,这个根据计算机操作系统给的随机调度
4、zu.zwl.SpringbootlearnApplication : The following profiles are active: dev,devdb
打印出所选择的profiles,这里dev是我的开发环境properties和配置环境bean
5、然后跳过了很多个相同的配置文件,很纳闷 不懂
6、ationConfigEmbeddedWebApplicationContext :root of context hierarchy 加载了上下文
7、然后加载了两个默认配置文件
ationConfigEmbeddedWebApplicationContext : Unable to locate MessageSource with name 'messageSource':usingdefault[org.springframework.context.support.DelegatingMessageSource@6676f6a0]
MessageSource :i18n用的
applicationEventMulticaster:事件驱动模型 具体干啥的不知道。应该很关键吧
8、t.TomcatEmbeddedServletContainerFactory : None of the document roots [src/main/webapp, public, static] point to a directory and will be ignored.
最开始没有webapp目录,我自己手动建立了,重启试试(过程中歌曲竟然卡了一下)
这里的代码变为.t.TomcatEmbeddedServletContainerFactory : Document root: J:\MAKER\CodeFile\idea\springbootlearn\src\main\webapp ,证明成功了
我添加一个index.html 试试,看刷新后能自动加载不。 是可以的
9、s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8989 (http)
在8989端口启动了
10、o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
初始化内嵌的webApplicationContext上下文
Root WebApplicationContext: initialization completed in 2520 ms 还是挺快的
11、o.s.b.w.s.ServletContextInitializerBeans : Added existing Servlet initializer bean 'dispatcherServletRegistration' 注册web的总跳转dispatcher
org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration.class
12、o.s.b.w.s.ServletContextInitializerBeans : Created Filter initializer for bean 'characterEncodingFilter'
org/springframework/boot/autoconfigure/web/HttpEncodingAutoConfiguration.class
初始化 字符编码过滤器
看下源码HttpEncodingAutoConfiguration.class
里面有一个annotation
@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true)
一个不可改变量
private final HttpEncodingProperties properties;
应该是有一个properties文件,我找找在哪
在这个包下面找到了spring-configuration-metadata.json 文件下有
就这样设置了把
13、o.s.b.w.s.ServletContextInitializerBeans :Created Filter initializer for bean 'hiddenHttpMethodFilter' 干什么东东的? 它是前端后端数据交互时restFul风格的实现
用的这个org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.class
浏览器 form 表单只支持 GET与 POST 请求,而DELETE、 PUT 等 method 并不支持, Spring3.0 添加了一个过滤器,可以将这些请求转换为标准的 http 方法,使得支持 GET、 POST、 PUT 与DELETE 请求。
实现过程:
前台:
后台:
需注意:
需要注册org.springframework.web.filter.HiddenHttpMethodFilter监听器来代替最初的org.springframework.web.servlet.DispatcherServlet
看下源码
里面有很多前台的东西
估计是和前台有关
14、o.s.b.w.s.ServletContextInitializerBeans : Created Filter initializer for bean 'httpPutFormContentFilter'; order=-9900, resource=class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.class]
也是监听器 这次和put提交方式有关,需注册,该过滤器只能接受enctype值为application/x-www-form-urlencoded的表单 ,我累个去,网上对其解释千篇一律,无语,啥时候google了,我先试着读下源码,也是WebMvcAutoConfiguration 。未果,终。
15、o.s.b.w.s.ServletContextInitializerBeans : Created Filter initializer for bean 'requestContextFilter'; order=-105, resource=class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class] 这个是内部类
requestContextFilter 是将页面提交的request转发到Controller中
16、然后定义各个filter的拦截路径 / 只拦截 /xxxx , /*拦截所有 /xxx 和/xxx.jsp
17、然后是初始化过滤器(上面是创建bean)
18、s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@306279ee: startup date [Tue Sep 26 23:22:39 CST 2017]; root of context hierarchy
注册总的 controler bean
19、紧接着是映射我们自定义的controller
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/first]}" onto public java.util.Map<java.lang.String,java.lang.Object>zzu.zwl.controller.UserController.name1(javax.servlet.ServletResponse)
20、然后是装配好的 映射
两个error页面,一个返回json,一个view ,一个/**配置,一个/**/favicon.ico来设置图标
21、o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
初始化成功了算是
22、ationConfigEmbeddedWebApplicationContext : Unable to locate LifecycleProcessor with name 'lifecycleProcessor':usingdefault[org.springframework.context.support.DefaultLifecycleProcessor@5c1bd44c] 一个控制进程的风格,里面一个
private volatile long timeoutPerShutdownPhase = 30000;
23、utoConfigurationReportLoggingInitializer : 日志报告输出
24、
成功启动
25、会自动检测代码变化,然后自动再次初始化
总结:web方面做的比较多,可能后期做着会非常完善吧 ,写篇读书日志然后睡觉喽