Skip to content

Commit 8572e70

Browse files
authored
Update SpringBoot.md
1 parent 55e0841 commit 8572e70

File tree

1 file changed

+136
-1
lines changed

1 file changed

+136
-1
lines changed

docs/SpringBoot.md

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,45 @@
1212

1313
部署:不需要单独的 Web 服务器。这意味着您不再需要启动 Tomcat 或其他任何东西。
1414

15+
## Spring Boot 启动流程⭐
16+
17+
首先 prepareEnvironment 配置环境,然后准备 Context 上下文,ApplicationContext 的后置处理器,初始化 lnitializers,通知处理上下文准备和加载时间,然后开始refresh。
18+
19+
prepareEnvironment
20+
21+
createApplicationContext
22+
postProcessApplicationContext
23+
applylnitializers
24+
listeners.contextPrepared
25+
listeners.contextLoaded
26+
refreshContext
27+
1528
### Spring Boot提供了两种常用的配置文件:
1629

1730
- properties文件
1831
- yml文件
1932

33+
## Spring Boot 的核心注解是哪个?⭐
34+
35+
启动类上面的注解是@SpringBootApplication,它也是 Spring Boot 的核心注解,主要组合包含了以下 3 个注解:
36+
37+
@SpringBootConfiguration:组合了 @Configuration 注解,实现配置文件的功能。
38+
39+
@EnableAutoConfiguration:打开自动配置的功能,也可以关闭某个自动配置的选项,如关闭数据源自动配置功能: @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })。
40+
41+
@ComponentScan:Spring组件扫描。
42+
43+
## Spring Boot 自动配置原理是什么⭐
44+
45+
@EnableAutoConfiguration这个注解开启自动配置,它的作用:
46+
47+
* 利用EnableAutoConfigurationImportSelector给容器中导入一些组件
48+
* 这个类父类有一个方法:selectImports(),这个方法返回 **configurations**
49+
* List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);获取候选的配置
50+
* 将 类路径下 META-INF/spring.factories 里面配置的所有EnableAutoConfiguration的值加入到了容器中
51+
* 加载某个组件的时候根据注解的条件判断每个加入的组件是否生效,如果生效就把类的属性和配置文件绑定起来
52+
* 这时就读取配置文件的值加载组件
53+
2054
### SpringBoot、SpringMVC和Spring区别
2155

2256
spring boot只是一个配置工具,整合工具,辅助工具.
@@ -29,4 +63,105 @@ Spring 框架就像一个家族,有众多衍生产品例如 boot、security、
2963

3064
- Spring 是一个“引擎”;
3165
- Spring MVC 是基于Spring的一个 MVC 框架;
32-
- Spring Boot 是基于Spring的条件注册的一套快速开发整合包。
66+
- Spring Boot 是基于Spring的条件注册的一套快速开发整合包
67+
68+
## SpringBoot 拦截器和过滤器
69+
70+
​ 1、Filter是依赖于Servlet容器,属于Servlet规范的一部分,而拦截器则是独立存在的,可以在任何情况下使用。
71+
72+
  2、Filter的执行由Servlet容器回调完成,而拦截器通常通过动态代理的方式来执行。
73+
74+
  3、Filter的生命周期由Servlet容器管理,而拦截器则可以通过IoC容器来管理,因此可以通过注入等方式来获取其他Bean的实例,因此使用会更方便。
75+
76+
77+
78+
```java
79+
@WebFilter(urlPatterns = "/*", filterName = "logFilter")
80+
public class LogCostFilter implements Filter {
81+
@Override
82+
public void init(FilterConfig filterConfig) throws ServletException {
83+
84+
}
85+
86+
@Override
87+
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
88+
long start = System.currentTimeMillis();
89+
filterChain.doFilter(servletRequest, servletResponse);
90+
System.out.println("LogFilter Execute cost=" + (System.currentTimeMillis() - start));
91+
}
92+
93+
@Override
94+
public void destroy() {
95+
96+
}
97+
}
98+
```
99+
100+
101+
102+
这段代码的逻辑比较简单,就是在方法执行前先记录时间戳,然后通过过滤器链完成请求的执行,在返回结果之间计算执行的时间。这里需要主要,这个类必须继承Filter类,这个是Servlet的规范,这个跟以前的Web项目没区别。 这里直接用@WebFilter就可以进行配置,同样,可以设置url匹配模式,过滤器名称等。这里需要注意一点的是@WebFilter这个注解是Servlet3.0的规范,并不是Spring boot提供的。除了这个注解以外,我们还需在配置类中加另外一个注解:@ServletComponetScan,指定扫描的包。
103+
104+
```java
105+
@SpringBootApplication
106+
@MapperScan("com.pandy.blog.dao")
107+
@ServletComponentScan("com.pandy.blog.filters")
108+
public class Application {
109+
public static void main(String[] args) throws Exception {
110+
SpringApplication.run(Application.class, args);
111+
}
112+
}
113+
```
114+
115+
上面我们已经介绍了过滤器的配置方法,接下来我们再来看看如何配置一个拦截器。我们使用拦截器来实现上面同样的功能,记录请求的执行时间。首先我们实现拦截器类:
116+
117+
```java
118+
public class LogCostInterceptor implements HandlerInterceptor {
119+
long start = System.currentTimeMillis();
120+
@Override
121+
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
122+
start = System.currentTimeMillis();
123+
return true;
124+
}
125+
126+
@Override
127+
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
128+
System.out.println("Interceptor cost="+(System.currentTimeMillis()-start));
129+
}
130+
131+
@Override
132+
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
133+
}
134+
}
135+
```
136+
137+
 这里我们需要实现HandlerInterceptor这个接口,这个接口包括三个方法,preHandle是请求执行前执行的,postHandler是请求结束执行的,但只有preHandle方法返回true的时候才会执行,afterCompletion是视图渲染完成后才执行,同样需要preHandle返回true,该方法通常用于清理资源等工作。除了实现上面的接口外,我们还需对其进行配置:
138+
139+
```java
140+
@Configuration
141+
public class InterceptorConfig extends WebMvcConfigurerAdapter {
142+
143+
@Override
144+
public void addInterceptors(InterceptorRegistry registry) {
145+
registry.addInterceptor(new LogCostInterceptor()).addPathPatterns("/**");
146+
super.addInterceptors(registry);
147+
}
148+
}
149+
```
150+
151+
这里我们继承了WebMVCConfigurerAdapter,看过前面的文章的朋友应该已经见过这个类了,在进行静态资源目录配置的时候我们用到过这个类。这里我们重写了addInterceptors这个方法,进行拦截器的配置,主要配置项就两个,一个是指定拦截器,第二个是指定拦截的URL。
152+
153+
154+
155+
## spring boot处理一个http请求的全过程
156+
157+
* 由前端发起请求
158+
* 根据路径,Springboot会加载相应的Controller进行拦截
159+
* 拦截处理后,跳转到相应的Service处理层
160+
* 跳转到ServiceImplement(service实现类)
161+
* 在执行serviceimplement时会加载Dao层,操作数据库
162+
* 再跳到Dao层实现类
163+
* 执行会跳转到mapper层
164+
* 然后MallMapper会继续找对应的mapper.xml配置文件
165+
* 之后便会跳转到第4步继续执行,执行完毕后会将结果返回到第1步,然后
166+
* 便会将数据以JSON的形式返回到页面,同时返回状态码,正常则会返回200,便会回到步骤1中查询判断。
167+

0 commit comments

Comments
 (0)