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
12
public 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();
}

Read More

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
24
package 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>

结束

MyBatis2-原理

JDBC的优化和封装

(1) 使用数据库连接池对连接进行管理
(2) SQL语句统一存放到配置文件
(3) SQL语句变量和传入参数的映射以及动态SQL
(4) 动态SQL语句的处理
(5) 对数据库操作结果的映射和结果缓存
(6) SQL语句的重复

参考

原理分析之一:从JDBC到Mybatis
MyBatis原理概括
教你手写Mybatis框架

SpringMVC4-JSON2

json用起来

  • 导入json
  • 配置json
  • 新建对象
  • 转化为json
1
2
3
4
5
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.2</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!--解决json 乱码配置-->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
1
2
3
4
5
6
@ResponseBody
@RequestMapping("/json1")
public String json1() throws JsonProcessingException {
return new ObjectMapper().writeValueAsString(
new node(1, 2L, "hello"));
}

fastjson用起来

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
1
2
3
4
5
6
7

@ResponseBody
@RequestMapping("/json2")
public String json2() {
return JSON.toJSONString(new node(1, 2L, "hello fastjson 你好"));
}

SpringMVC3-JSON

json

这是一个字符串,他可以把JavaScript对象变成字符串,然后传给后端,实现前后端分离

bson

bson是由10gen开发的一个数据格式,目前主要用于mongoDB中

bson 的遍历更加迅速,因为他在头部存下了每个元素的长度

bson 的操作更加简单,9变成10对json来说要移动内容,但是bson不需要,他数字超范围会慢一些

bson 支持二进制数据的传输binary array

json 和javascirpt转化

1
2
JSON.parse();
JSON.stringify();

解析json

1
2
3
4
5
6
7
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.0.rc1</version>
</dependency>

参考

BSON的介绍及BSON与JSON的区别

SpringMVC2-注解

注解配置Controller

这里的19行是spring中的注解扫描,21行是不去处理静态资源,23行是配置处理器的适配器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<context:component-scan base-package="com.wsx.controller"/>
<!-- 不处理静态资源-->
<mvc:default-servlet-handler/>
<!-- 配置处理器和适配器-->
<mvc:annotation-driven/>

<!-- 解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

</beans>

Read More