SpringBoot6-启动的源码分析
启动配置原理
几个重要的事件回调机制 - ApplicationContextInitializer - SpringApplicationRunListener - ApplicationRunner - CommandLineRunner
启动流程
1 | return new SpringApplication(primarySources).run(args); |
- 创建SpringApplication对象
- 运行run方法
创建对象
现在左边的参数是null 1
2
3
4
5
6
7
8
9
10
11
12public SpringApplication(Class<?>... primarySources) {
this(null, primarySources);
}
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
this.webApplicationType = WebApplicationType.deduceFromClasspath();
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}
SpringMVC6-Ajax
拦截器
拦截器是SpringMVC框架自己的,只用SpringMVC才能使用 拦截器只会拦截访问的控制方法,静态资源是不会拦截的
定义拦截器
实现HandlerInterceptor 其实只要使用preHandle就可以了,他返回true才执行,否则不执行 比如我们就可以在这里实现登陆请求 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24package com.onlineStore.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
文件上传和下载
MultipartResolver 1
2
3
4
5
6
7
8<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--上传文件的最大大小,单位为字节 -->
<property name="maxUploadSize" value="17367648787"></property>
<!-- 上传文件的编码 -->
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
结束
SpringMVC4-JSON2
json用起来
- 导入json
- 配置json
- 新建对象
- 转化为json
1 | <dependency> |
1 | <!--解决json 乱码配置--> |
1 | @ResponseBody |
fastjson用起来
1 | <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> |
1 |
|
SpringMVC3-JSON
json
这是一个字符串,他可以把JavaScript对象变成字符串,然后传给后端,实现前后端分离
bson
bson是由10gen开发的一个数据格式,目前主要用于mongoDB中
bson 的遍历更加迅速,因为他在头部存下了每个元素的长度
bson 的操作更加简单,9变成10对json来说要移动内容,但是bson不需要,他数字超范围会慢一些
bson 支持二进制数据的传输binary array
json 和javascirpt转化
1 | JSON.parse(); |
解析json
1 | <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> |