视频学习地址:https://www.bilibili.com/video/BV1Et411Y7tQ?p=131&spm_id_from=pageDriver
在上一篇文章中我们已学会SpringBoot的基本使用,并且明白了SpringBoot的自动配置原理!接下来就是SpringBoot的核心功能,此篇文章会根据视频内容整理SpringBoot的配置文件、web开发、数据访问等一些重要知识!帮助我们快速掌握SpringBoot !
配置文件的作用:修改SpringBoot自动配置的默认值,SpringBoot在底层都给我们自动配置好。
SpringBoot使用一个全局的配置文件,并且配置文件名是固定的:
application.properties
application.yml
application.properties
是SpringBoot初始化向导默认生成的,前面我们也已经接触过了,采用key=value
的语法结构去描述配置信息!
如果两种配置文件同时存在并且配置了相同的key!yml的优先级会大于properties,因为properties是后加载的,所以此时yml就没有生效。
SpringBoot就用application.properties
覆盖了application.yml
的配置信息。如:
application.properties
# 指定端口号
server.port=8010
application.yml
server:
port: 8888 # 指定端口号
项目启动日志:Tomcat started on port(s): 8010 (http) with context path ''
properties需要使用大量的路径来描述层级(或者属性),比如environments.dev.url
和environments.dev.name
。其次,对于较为复杂的结构,比如数组(my.servers),写起来更为复杂。而对应的YAML格式文件就简单很多。
关于更多的properties配置项与使用这里就不多介绍了。
YAML是 “YAML Ain’t a Markup Language” (YAML不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言)
这种语言以数据作为中心,而不是以标记语言为重点!非常适合来做配置文件。
标记语言:
以前的配置文件,大多都使用的是 xxxx.xml文件。
YAML:以数据为中心,比json、xml等更适合做配置文件。
如:xml 与 yaml 的对比,配置项目端口号!
xml
<server>
<port>8081port>
server>
yaml
server:
port: 8081
语法结构:key:(空格)value
表示一对键值对(空格必须有)
如:
server:
port: 8888 # 指定端口号
servlet:
context-path: /hello # 指定根路径
说明:语法要求严格!
#
表示注释。userName
和user-name
是等效的。字面量:普通的值 [ 数字,布尔值,字符串 ]
字面量直接写在后面就可以 , 字符串默认不用加上双引号或者单引号。
""双引号,不会转义字符串里面的特殊字符 , 特殊字符会作为本身想表示的意思。
比如 :name: "kuang \n shen"
输出 :kuang 换行 shen
''单引号,会转义特殊字符 , 特殊字符最终会变成和普通字符一样输出。
比如 :name: 'kuang \n shen'
输出 :kuang \n shen
# 语法
key: value
# 如
debug: true
# 语法
k: {k1:v1,k2:v2,k3:v3} # 行内写法
# 或
k:
k1: v1
k2: v2
k3: v3
# 如
spring:
datasource:
username: root
password: root
# 行内写法
k: [v1,v2,v3]
# 或者
k:
- v1
- v2
- v3
不同数据类型Java代码:
@Data
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String userName;
private Boolean boss;
private Date birth;
private Integer age;
private Pet pet;
private String[] interests;
private List<String> animal;
private Map<String, Object> score;
private Set<Double> salarys;
private Map<String, List<Pet>> allPets;
}
@Data
public class Pet {
private String name;
private Double weight;
}
对应的yaml配置
person:
userName: zhangsan
boss: false
birth: 2019/12/12 20:12:33
age: 18
pet:
name: tomcat
weight: 23.4
interests: [篮球,游泳]
animal:
- jerry
- mario
score:
english:
first: 30
second: 40
third: 50
math: [131,140,148]
chinese: {first: 128,second: 136}
salarys: [3999,4999.98,5999.99]
allPets:
sick:
- {name: tom}
- {name: jerry,weight: 47}
health: [{name: mario,weight: 47}]
当我们自定义的类,绑定SpringBoot的全局配置后,编写相关配置时,是没有提示信息的,并且idea会爆出一些警告!如下图所示:
这是因为缺少配置处理器,我们只需添加一组依赖即可解决:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
官方文档地址:https://docs.spring.io/spring-boot/docs/2.3.9.RELEASE/reference/html/appendix-configuration-metadata.html#configuration-metadata
引入配置处理器依赖后,我们在项目打包时不应该打包配置处理器,所以还需要改变打包的配置!
<project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
exclude>
excludes>
configuration>
plugin>
plugins>
build>
project>
官网说明:https://docs.spring.io/spring-boot/docs/2.3.9.RELEASE/reference/html/spring-boot-features.html#boot-features-developing-web-applications
Spring Boot provides auto-configuration for Spring MVC that works well with most applications.(大多场景我们都无需自定义配置)
The auto-configuration adds the following features on top of Spring’s defaults:
Inclusion of ContentNegotiatingViewResolver
and BeanNameViewResolver
beans.
翻译:内容协商视图解析器和BeanName视图解析器
Support for serving static resources, including support for WebJars (covered later in this document)).
翻译:静态资源(包括webjars)
Automatic registration of Converter
, GenericConverter
, and Formatter
beans.
翻译:自动注册 Converter,GenericConverter,Formatter
Support for HttpMessageConverters
(covered later in this document).
翻译:支持 HttpMessageConverters (后来我们配合内容协商理解原理)
Automatic registration of MessageCodesResolver
(covered later in this document).
翻译:自动注册 MessageCodesResolver (国际化用)
Static index.html
support.
翻译:静态index.html 页支持
Custom Favicon
support (covered later in this document).
翻译:自定义 Favicon
Automatic use of a ConfigurableWebBindingInitializer
bean (covered later in this document).
翻译:自动使用 ConfigurableWebBindingInitializer ,(DataBinder负责将请求数据绑定到JavaBean上)
自定义SpringMVC组件的三种方式:
① 官网原文:If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own @Configuration
class of type WebMvcConfigurer
but without @EnableWebMvc
.
说明:不用@EnableWebMvc注解。使用 @Configuration
+ WebMvcConfigurer
自定义规则
② 官网原文:If you want to provide custom instances of RequestMappingHandlerMapping
, RequestMappingHandlerAdapter
, or ExceptionHandlerExceptionResolver
, and still keep the Spring Boot MVC customizations, you can declare a bean of type WebMvcRegistrations
and use it to provide custom instances of those components.
说明:声明 WebMvcRegistrations
改变默认底层组件
③ 官网原文:If you want to take complete control of Spring MVC, you can add your own @Configuration
annotated with @EnableWebMvc
, or alternatively add your own @Configuration
-annotated DelegatingWebMvcConfiguration
as described in the Javadoc of @EnableWebMvc
.
说明:使用 @EnableWebMvc
+@Configuration+DelegatingWebMvcConfiguration
全面接管SpringMVC。
以上都是基于官网介绍:SpringMVC的自动配置。当然在开发web应用时,我们随时都需要使用SpringMVC所以这里简单的了解一下SpringMVC的自动配置,关于详细的内容,后面会涉及到。
静态资源是指JS、CSS、Image等,当一个http请求进来,先去找Controller看能不能处理。不能处理的所有请求又都交给静态资源处理器。静态资源也找不到则响应404页面。
默认情况下静态资源处理器又通过 项目根路径+静态资源名称/路径 处理静态资源!值得注意的是静态资源只要放在类路径下,可放在如下目录下(/
表示根目录):
/static
/public
/resources
/META-INF/resources
就可以通过当前项目根路径/ + 静态资源名
去访问!
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":当前项目的根路径
例如我在static
目录下放一张图片c.jpg
,如下图所示:
访问:http://localhost:8080/c.jpg
就可访问得到这张图片!
改变默认的静态资源路径:
我们还可以使用spring.resources.static-locations
属性来自定义静态资源位置(用目录位置列表替换默认值)。根Servlet上下文路径/
也会自动添加为位置。
spring:
resources:
static-locations: [classpath:/haha/] # 自定义静态资源位置为类路径下的haha目录
默认是无前缀的,静态资源映射到/**
,但是我们可以使用spring.mvc.static-path-pattern
属性对其进行调整。例如,将所有静态资源重新定位到以下位置/app/**
可以实现:
spring:
mvc:
static-path-pattern: /app/** # 指定访问静态资源前缀为app
在访问静态资源时:当前项目根路径 + static-path-pattern + 静态资源名
= 静态资源文件夹下找。
如:http://localhost:8080/app/c.jpg
自动映射 /webjars/**
https://www.webjars.org/
<dependency>
<groupId>org.webjarsgroupId>
<artifactId>jqueryartifactId>
<version>3.5.1version>
dependency>
访问地址:http://localhost:8080/webjars/jquery/3.5.1/jquery.js
后面地址要按照依赖里面的包路径
Spring Boot支持静态和模板欢迎页面。它首先index.html
在配置的静态资源目录中查找文件。如果找不到,它将寻找一个index模板。如果找到任何一个,它将自动用作应用程序的欢迎页面。
注意:如果设置了静态资源路径的访问前缀,SpringBoot对欢迎页的支持会失效!
spring:
# mvc:
# static-path-pattern: /res/** 这个会导致welcome page功能失效
favicon.ico
放在静态资源目录下即可。
spring:
# mvc:
# static-path-pattern: /res/** 这个也会导致favicon功能失效
更多favicon.ico
图标的制作与使用,可查看我前端的博客。
1、SpringBoot启动默认加载 xxxAutoConfiguration
类(自动配置类)。
2、SpringMVC功能的自动配置类 WebMvcAutoConfiguration
生效。
WebMvcAutoConfiguration.java
配置生效条件:
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class) // 当容器中没有这个类型的组件以下配置才生效
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
...
}
WebMvcAutoConfiguration.java
给容器中配了什么?
@Configuration(proxyBeanMethods = false)
@Import(EnableWebMvcConfiguration.class)
@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
// 与全局配置文件进行绑定
@Order(0)
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {
...
}
配置文件的相关属性和xxx进行了绑定。
WebMvcProperties
的前缀是spring.mvc
ResourceProperties
的前缀是spring.resources
知识扩展:如果配置类中只有一个有参构造器!
那么构造器中所有参数都在IOC容器中获取!言外之意所有参数都是组件实例!如:
// WebMvcAutoConfigurationAdapter类的有参构造器
/*
ResourceProperties resourceProperties;获取和spring.resources绑定的所有的值的对象
WebMvcProperties mvcProperties 获取和spring.mvc绑定的所有的值的对象
ListableBeanFactory beanFactory Spring的beanFactory
HttpMessageConverters 找到所有的HttpMessageConverters
ResourceHandlerRegistrationCustomizer 找到 资源处理器的自定义器。=========
DispatcherServletPath
ServletRegistrationBean 给应用注册Servlet、Filter....
*/
public WebMvcAutoConfigurationAdapter(WebMvcProperties mvcProperties, ListableBeanFactory beanFactory,
ObjectProvider<HttpMessageConverters> messageConvertersProvider,
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider,
ObjectProvider<DispatcherServletPath> dispatcherServletPath,
ObjectProvider<ServletRegistrationBean<?>> servletRegistrations) {
this.mvcProperties = mvcProperties;
this.beanFactory = beanFactory;
this.messageConvertersProvider = messageConvertersProvider;
this.resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizerProvider.getIfAvailable();
this.dispatcherServletPath = dispatcherServletPath;
this.servletRegistrations = servletRegistrations;
}
3、资源处理的默认规则
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
// 1、webjars资源的处理规则
addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
// 2、其他静态资源的映射规则
addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(),
this.resourceProperties.getStaticLocations());
}
方法点进去后就可查看其他静态资源的映射规则:
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/" };
/**
* Locations of static resources. Defaults to classpath:[/META-INF/resources/,
* /resources/, /static/, /public/].
*/
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
...
}
4、欢迎页的映射规则
// HandlerMapping:处理器映射。保存了每一个Handler能处理哪些请求。
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
this.mvcProperties.getStaticPathPattern());
welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
return welcomePageHandlerMapping;
}
WelcomePageHandlerMapping类:
WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders,
ApplicationContext applicationContext, Resource welcomePage, String staticPathPattern) {
// 要使用欢迎页功能,必须是 /**
if (welcomePage != null && "/**".equals(staticPathPattern)) {
logger.info("Adding welcome page: " + welcomePage);
setRootViewName("forward:index.html");
}
else if (welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) {
logger.info("Adding welcome page template: index");
// 调用 Controller /index
setRootViewName("index");
}
}
1、Rest 风格的使用与原理
/getUser
获取用户 /deleteUser
删除用户 /editUser
修改用户 /saveUser
保存用户/user
GET-获取用户
、 DELETE-删除用户
、 PUT-修改用户
、 POST-保存用户
使用POST、DELETE、PUT、GET去操作不同的资源,使得代码更简洁,更有层次,更易于实现缓存等机制,这就是Rest风格
!那么在SpringBoot中我们应该怎样使用Rest风格呢?
默认情况下,从form
表单提交一个DELETE
等请求,SpringBoot是不能正常处理的,当然使用Postman
工具是可以的!所以想要表单支持Rest风格请求,我们需要配合hiddenHttpMethodFilter
组件与表单隐藏域的书写!
method=post
,隐藏域 _method=put
Rest风格使用示例与总结:
// 页面提交Rest风格的使用示例:发送一个 put 请求
// 1、后端代码,编写一个处理 PUT 请求的方法
@RequestMapping(value = "/user",method = RequestMethod.PUT)
public String putUser() {
return "PUT - Howie";
}
// 2、修改配置文件(application.yml)
spring:
mvc:
hiddenmethod:
filter:
enabled: true // 开启隐藏域功能,来支持表单可以提交Rest风格请求
// 3、前端代码
<form action="/user" method="post">
// 注意隐藏域的写法
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="REST-PUT 提交">
</form>
// 为什么需要手动配置,才能支持form提交REST风格请求?
@Bean
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
@ConditionalOnProperty(prefix = "spring.mvc.hiddenmethod.filter", name = "enabled", matchIfMissing = false)
// 如果没有在配置文件里配置spring.mvc.hiddenmethod.filter就默认是false,hiddenHttpMethodFilter组件也就默认不生效!
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
return new OrderedHiddenHttpMethodFilter();
}
为什么表单隐藏域中name
要等于_method
??我们可以自定义_method
的名字吗?
答案是肯定的,我们来看看HiddenHttpMethodFilter
组件的源码:
public class HiddenHttpMethodFilter extends OncePerRequestFilter {
private static final List<String> ALLOWED_METHODS =
Collections.unmodifiableList(Arrays.asList(HttpMethod.PUT.name(),
HttpMethod.DELETE.name(), HttpMethod.PATCH.name()));
/** Default method parameter: {@code _method}. */
public static final String DEFAULT_METHOD_PARAM = "_method"; // 组件指定的默认值
private String methodParam = DEFAULT_METHOD_PARAM;
...
}
不知道小伙伴们有没有发现呢?我们只需自定义自己的HiddenHttpMethodFilter
组件,就可改变表单隐藏域name属性的值!
//自定义filter
@Bean
public HiddenHttpMethodFilter hiddenHttpMethodFilter(){
HiddenHttpMethodFilter methodFilter = new HiddenHttpMethodFilter();
methodFilter.setMethodParam("_m"); // 指定表单隐藏域name属性的值。
return methodFilter;
}
Rest原理(表单提交要使用REST的时候):
1、表单提交会带上_method=PUT
2、请求过来被HiddenHttpMethodFilter拦截
_method
的值。PUT.DELETE.PATCH
Rest使用客户端工具:
spring:
mvc:
hiddenmethod:
filter:
enabled: true #开启页面表单的Rest功能
2、请求映射原理
我们都知道servlet
任务规范中,http服务器能调用的【动态资源文件】必须是一个servlet接口实现类,然后重写doGet()
、doPost()
或者是doService()
去处理不同的网络请求。他们的继承关系如下:
经过分析发现,重写了doGet()
、doPost()
、service()
(注意不是doService()
)等方法的类是FrameworkServlet
这个抽象类!这个抽象类中doGet()
、doPost()
方法里又都调用了processRequest()
方法,如:
// FrameworkServlet抽象类中重写的service()方法
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpMethod httpMethod = HttpMethod.resolve(request.getMethod());
if (httpMethod == HttpMethod.PATCH || httpMethod == null) {
processRequest(request, response); // 调用 processRequest() 方法
}
else {
super.service(request, response);
}
}
// FrameworkServlet抽象类中重写的doGet()方法
@Override
protected final void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response); // 调用 processRequest() 方法
}
// FrameworkServlet抽象类中重写的doPost()方法
@Override
protected final void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response); // 调用 processRequest() 方法
}
所以,每个过来的请求都会调用processRequest()
方法!而此方法里又调用了doService()
方法:
然后我们继续看doService()
的实现:
最终是DispatcherServlet
这个类继承了FrameworkServlet
抽象类并重写了doService
方法,重写的时候又调用了doDispatch()
这个方法!可以自行查看,于是得出结论:SpringMVC的功能分析都要从DispatcherServlet
类中的doDispatch()
这个方法开始!
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
// 找到当前请求使用哪个Handler(Controller的方法)处理
mappedHandler = getHandler(processedRequest);
// HandlerMapping:处理器映射。/xxx->>xxxx
5 个HandlerMapping
(处理器映射)
第0个处理器映射 RequestMappingHandlerMapping
:保存了所有@RequestMapping 和handler的映射规则。
结论:所有的请求映射都在HandlerMapping中,也就是处理器映射。
WelcomePageHandlerMapping
。访问/
能访问到index.html
。RequestMappingHandlerMapping
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
if (this.handlerMappings != null) {
// 遍历查找
for (HandlerMapping mapping : this.handlerMappings) {
HandlerExecutionChain handler = mapping.getHandler(request);
if (handler != null) {
return handler;
}
}
}
return null;
}
1、基本注解
关于请求参数的绑定,SpringMVC中我们都已经接触过了,所以在这里我们来简单的回顾一下:
@PathVariable、@RequestHeader、@ModelAttribute、@RequestParam、@MatrixVariable、@CookieValue、@RequestBody
测试:@PathVariable
// 1、controller 代码
/**
* API:http://localhost:8080/car/2/owner/lisi
* 注解 @PathVariable:绑定路径变量,如果请求参数是 key=value 的形式,还可以封装成一个 map
*/
@GetMapping("/car/{id}/owner/{username}")
public Map<String,Object> getCarInfo(@PathVariable("id") Integer id,
@PathVariable("username") String username,
@PathVariable Map<String,Object> params) {
System.out.println("debug: carId = " + id);
System.out.println("debug: username = " + username);
System.out.println("debug: params = " + params);
Map<String,Object> map = new HashMap<>();
map.put("id",id);
map.put("username",username);
map.put("car","迈凯伦");
return map;
}
// 2、前端代码
<li>
<a href="/car/1/owner/lisi">测试@PathVariable注解</a>
</li>
// 3、测试
{
car: "迈凯伦",
id: 1,
username: "lisi"
}
测试:@RequestHeader
// 1、controller代码
/**
* API:http://localhost:8080/car/header
* 注解 @RequestHeader:获取请求头信息,我们可以传一个map、请求头名、MultiValueMap,没有参数就是提取所以请求头。
*/
@GetMapping("/car/header")
public Map<String,Object> requestHeaderTest(@RequestHeader("host") String host,
@RequestHeader("User-Agent") String userAgent) {
System.out.println("host: " + host);
System.out.println("User-Agent: " + userAgent);
Map<String,Object> map = new HashMap<>();
map.put("host",host);
map.put("User-Agent",userAgent);
return map;
}
// 2、前端代码
<li>
<a href="/car/header">测试@RequestHeader注解</a>
</li>
// 3、测试
{
host: "localhost:8080",
User-Agent: "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
}
测试:@RequestParam
// 1、controller 代码
/**
* API:http://localhost:8080/owner?id=1&name=alex&age=22&like=eat&like=code
* 注解 @RequestParam:主要解决前台参数名称与后台接收参数变量名称不一致的问题,我们还可以封装成一个 list 或 map
*/
@GetMapping("/owner")
public Map<String,Object> setUserInfo(@RequestParam("id") Integer userId,
@RequestParam("name") String userName,
@RequestParam("age") String userAge,
@RequestParam("like") List<String> likes,
@RequestParam Map<String,Object> params) {
System.out.println(params);
Map<String,Object> map = new HashMap<>();
map.put("user_id",userId);
map.put("username",userName);
map.put("user_age",userAge);
map.put("like",likes);
return map;
}
// 2、前端代码
<li>
<a href="/owner?id=1&name=alex&age=22&like=eat&like=code">测试@RequestParam</a>
</li>
// 3、测试
{
user_id: 1,
like: [
"eat",
"code"
],
user_age: "22",
username: "alex"
}
测试:@CookieValue
// 1、controller 代码
/**
* API:http://localhost:8080/cookie
* 注解 @CookieValue:获取cookie的值
*/
@GetMapping("/cookie")
public Map<String,Object> getCookie(@CookieValue("ts_uid") String ts_uid){
Map<String,Object> map = new HashMap<>();
map.put("cookie_ts_uid",ts_uid);
return map;
}
// 2、前端代码
<li>
<a href="/cookie">测试@CookieValue注解</a>
</li>
// 3、测试
{
cookie_ts_uid: "1520892790"
}
测试:@RequestBody
获取请求体数据,例如post请求!
// 1、Book 实体类
/**
* @description: 书籍实体类
* @author: laizhenghua
* @date: 2021/3/29 14:32
*/
public class Book {
private Integer id;
private String bookName;
private String bookAuthor;
...
// getter and setter
// toString()
}
// 2、controller 代码
/**
* API:http://localhost:8080/book
*/
@PostMapping("/book")
public Map<String,Object> saveBook(Book book,@RequestBody String content) {
System.out.println("debug => " + book);
System.out.println("debug => " + content);
Map<String,Object> map = new HashMap<>();
map.put("status","success");
map.put("book",book);
map.put("content",content);
return map;
}
// 3、前端代码
<li>
<form action="/book" method="post">
<input type="hidden" name="id" value="1">
书籍名称:<input type="text" name="bookName">
书籍作者:<input type="text" name="bookAuthor">
<input type="submit" value="提交">
</form>
</li>
// 4、测试
{
book: {
id: 1,
bookName: "Java编程思想",
bookAuthor: "Alex"
},
content: "id=1&bookName=Java%E7%BC%96%E7%A8%8B%E6%80%9D%E6%83%B3&bookAuthor=Alex",
status: "success"
}
另外还有@RequestAttribute
和@MatrixVariable
注解这里就不测试了!可自行学习。
@RequestAttribute
:获取request域属性@MatrixVariable
:矩阵变量2、Servlet API
WebRequest、ServletRequest、MultipartRequest、 HttpSession、javax.servlet.http.PushBuilder、Principal、InputStream、Reader、HttpMethod、Locale、TimeZone、ZoneId
// ServletRequestMethodArgumentResolver 类参数解析器支持的参数类型
@Override
public boolean supportsParameter(MethodParameter parameter) {
Class<?> paramType = parameter.getParameterType();
return (WebRequest.class.isAssignableFrom(paramType) ||
ServletRequest.class.isAssignableFrom(paramType) ||
MultipartRequest.class.isAssignableFrom(paramType) ||
HttpSession.class.isAssignableFrom(paramType) ||
(pushBuilder != null && pushBuilder.isAssignableFrom(paramType)) ||
Principal.class.isAssignableFrom(paramType) ||
InputStream.class.isAssignableFrom(paramType) ||
Reader.class.isAssignableFrom(paramType) ||
HttpMethod.class == paramType ||
Locale.class == paramType ||
TimeZone.class == paramType ||
ZoneId.class == paramType);
}
3、复杂参数
Map
、Model
(map、model里面的数据会被放在request的请求域 request.setAttribute)、Errors/BindingResult、RedirectAttributes( 重定向携带数据)
、ServletResponse(response)
、SessionStatus、UriComponentsBuilder、ServletUriComponentsBuilder
验证map
、model
、request
参数所传的值最终都保存到request域里:
/**
* @description: 浏览器访问:http://localhost:8080/param 即可查看效果
* @author: laizhenghua
* @date: 2021/3/30 9:44
*/
@Controller
public class RequestController {
@GetMapping("/param")
public String testParam(Map<String,Object> params, Model model,
HttpServletRequest request,
HttpServletResponse response) {
params.put("message","hello world"); // 使用 map 参数存值
model.addAttribute("code",200); // 使用 model 参数存值
request.setAttribute("status","success"); // 原生 request 存值
Cookie cookie = new Cookie("k1","v1");
cookie.setDomain("localhost");
response.addCookie(cookie);
return "forward:/success";
}
@GetMapping("/success")
@ResponseBody
public Map<String,Object> success(@RequestAttribute("code") Integer code, HttpServletRequest request) {
Map<String,Object> map = new HashMap<>();
// 验证 map,model 参数里面的数据存放到 request请求域里
Object message = request.getAttribute("message"); // 获取 map 参数所传的值
map.put("message",message);
map.put("code",code); // 注意 code 是 model参数 所传的值
Object status = request.getAttribute("status"); // 获取原生 request 参数所传的值
map.put("status",status);
return map;
}
}
原理解析:
Map、Model类型的参数,会返回 mavContainer.getModel();—> BindingAwareModelMap 是Model 也是Map
mavContainer.getModel(); 获取到值
最后怎样存到request域里的原理这里不好描述,推荐看视频讲解:https://www.bilibili.com/video/BV1Et411Y7tQ?p=145&spm_id_from=pageDriver
在开发时,我们经常使用实体类作为参数去接收前端或客户端传过来的请求参数!如:
// 1、实体类的书写
public class Book {
private Integer id;
private String bookName;
private String bookAuthor;
...
// getter and setter
// toString()
}
// 2、ctroller 代码
/**
* 使用实体类接收参数
* 数据绑定:页面提交的请求数据(GET、POST)都可以和对象属性进行绑定,还支持级联属性的写法(pet.name)
*/
@PutMapping("/book")
public Map<String,Object> updateBook(Book book) {
Map<String,Object> map = new HashMap<>();
map.put("book",book);
map.put("status","success");
return map;
}
// 3、前端代码
<li>
<form action="/book" method="post">
<input type="hidden" name="_method" value="put">
<input type="hidden" name="id" value="1">
书籍名称:<input type="text" name="bookName">
书籍作者:<input type="text" name="bookAuthor">
<input type="submit" value="提交">
</form>
</li>
// 4、测试
那么传过来的参数在SpringBoot中是如何封装成一个实体类的呢?经过分析发现,自定义类型参数(实体类接收参数)是通过如下步骤解析与封装为POJO
:
ServletModelAttributeMethodProcessor
这个参数解析器解析传过来的参数。this.annotationNotRequired && !BeanUtils.isSimpleProperty(parameter.getParameterType()))
Object attribute = null
WebDataBinder binder = binderFactory.createBinder(webRequest, attribute, name);
,将请求参数的值绑定到指定的JavaBean
属性里。Converters
把参数转换成JavaBean需要的类型,再次封装到JavaBean中。GenericConversionService
找它里面的所有converter那个可以将这个数据类型转换到指定的类型(request带来参数的字符串转换到指定的类型。JavaBean --> Integer、byte --> file)。核心方法:
@Override
@Nullable
public final Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
Assert.state(mavContainer != null, "ModelAttributeMethodProcessor requires ModelAndViewContainer");
Assert.state(binderFactory != null, "ModelAttributeMethodProcessor requires WebDataBinderFactory");
String name = ModelFactory.getNameForParameter(parameter);
ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class);
if (ann != null) {
mavContainer.setBinding(name, ann.binding());
}
Object attribute = null; // 创建一个空对象
BindingResult bindingResult = null;
if (mavContainer.containsAttribute(name)) {
attribute = mavContainer.getModel().get(name);
}
else {
// Create attribute instance
try {
attribute = createAttribute(name, parameter, binderFactory, webRequest);
}
catch (BindException ex) {
if (isBindExceptionRequired(parameter)) {
// No BindingResult parameter -> fail with BindException
throw ex;
}
// Otherwise, expose null/empty value and associated BindingResult
if (parameter.getParameterType() == Optional.class) {
attribute = Optional.empty();
}
bindingResult = ex.getBindingResult();
}
}
if (bindingResult == null) {
// Bean property binding and validation;
// skipped in case of binding failure on construction.
// 创建 web 数据绑定器
WebDataBinder binder = binderFactory.createBinder(webRequest, attribute, name);
if (binder.getTarget() != null) {
if (!mavContainer.isBindingDisabled(name)) {
bindRequestParameters(binder, webRequest);
}
validateIfApplicable(binder, parameter);
if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
throw new BindException(binder.getBindingResult());
}
}
// Value type adaptation, also covering java.util.Optional
if (!parameter.getParameterType().isInstance(attribute)) {
attribute = binder.convertIfNecessary(binder.getTarget(), parameter.getParameterType(), parameter);
}
bindingResult = binder.getBindingResult();
}
// Add resolved attribute and BindingResult at the end of the model
Map<String, Object> bindingResultModel = bindingResult.getModel();
mavContainer.removeAttributes(bindingResultModel);
mavContainer.addAllAttributes(bindingResultModel);
return attribute;
}
核心:
HandlerMapping
中找到能处理请求的Handler(Controller.method())RequestMappingHandlerAdapter
1、4 种 HandlerAdapter
0 - 支持方法上标注@RequestMapping
。
1 - 支持函数式编程的controller方法。
等等最常用的是以上两个!
2、执行目标方法
// Actually invoke the handler.
// 在 DispatcherServlet 类的 doDispatch() 方法
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
// No synchronization on session demanded at all...
mav = invokeHandlerMethod(request, response, handlerMethod); // 执行目标方法
// ServletInvocableHandlerMethod 类
Object returnValue = invokeForRequest(webRequest, mavContainer, providedArgs); // 在这里真正执行controller方法,拿到返回值
// 获取方法的参数值
Object[] args = getMethodArgumentValues(request, mavContainer, providedArgs);
3、参数解析器 - HandlerMethodArgumentResolver
确定将要执行的目标方法的每一个参数的值是什么。
SpringMVC目标方法能写多少种参数类型。取决于参数解析器。
参数解析器的原生接口,也就是说,以上26个解析器是接口实现类:
两个接口方法解读:
// InvocableHandlerMethod 类中
protected Object[] getMethodArgumentValues(NativeWebRequest request, @Nullable ModelAndViewContainer mavContainer,
Object... providedArgs) throws Exception {
MethodParameter[] parameters = getMethodParameters();
if (ObjectUtils.isEmpty(parameters)) {
return EMPTY_ARGS;
}
Object[] args = new Object[parameters.length];
for (int i = 0; i < parameters.length; i++) {
MethodParameter parameter = parameters[i];
parameter.initParameterNameDiscovery(this.parameterNameDiscoverer);
args[i] = findProvidedArgument(parameter, providedArgs);
if (args[i] != null) {
continue;
}
// 判断那个参数解析器支持解析参数
if (!this.resolvers.supportsParameter(parameter)) {
throw new IllegalStateException(formatArgumentError(parameter, "No suitable resolver"));
}
try {
args[i] = this.resolvers.resolveArgument(parameter, mavContainer, request, this.dataBinderFactory);
}
catch (Exception ex) {
// Leave stack trace for later, exception may actually be resolved and handled...
if (logger.isDebugEnabled()) {
String exMsg = ex.getMessage();
if (exMsg != null && !exMsg.contains(parameter.getExecutable().toGenericString())) {
logger.debug(formatArgumentError(parameter, exMsg));
}
}
throw ex;
}
}
return args;
}
// 5.1、挨个判断所有参数解析器那个支持解析器这个参数
@Nullable
private HandlerMethodArgumentResolver getArgumentResolver(MethodParameter parameter) {
HandlerMethodArgumentResolver result = this.argumentResolverCache.get(parameter);
if (result == null) {
for (HandlerMethodArgumentResolver resolver : this.argumentResolvers) {
if (resolver.supportsParameter(parameter)) {
result = resolver;
this.argumentResolverCache.put(parameter, result);
break;
}
}
}
return result;
}
// 5.2、解析这个参数的值
// 调用各自 HandlerMethodArgumentResolver 的 resolveArgument 方法即可
以前的开发,我们给请求响应JSON
数据,都需要自己手动转换,但是在SpringBoot中我们已不需要自己转换只需使用一些简单的注解(@ResponseBody / @RestController
)就可实现JSON
数据的转换。
引入的依赖:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jsonartifactId>
<version>2.3.9.RELEASEversion>
<scope>compilescope>
dependency>
@ResponseBody
@GetMapping("/test/book")
public Book getBook() {
Book book = new Book();
book.setId(2);
book.setBookName("大话数据结构");
book.setBookAuthor("程杰");
return book;
}
// 只需发起 http://localhost:8080/test/book 请求,就能给浏览器响应json数据。
那么响应 json 数据的原理又是什么呢?
try {
// 利用返回值解析器
this.returnValueHandlers.handleReturnValue(
returnValue, getReturnValueType(returnValue), mavContainer, webRequest);
}
handleReturnValue()
方法内部寻找那个返回值解析器可以处理返回值
@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
// 获取返回值与返回值类型,并寻找可以处理返回值的返回值解析器 selectHandler()
HandlerMethodReturnValueHandler handler = selectHandler(returnValue, returnType);
if (handler == null) {
throw new IllegalArgumentException("Unknown return value type: " + returnType.getParameterType().getName());
}
handler.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
}
最终是RequestResponseBodyMethodProcessor
类中的此方法将数据写为 json 。
@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest)
throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException {
mavContainer.setRequestHandled(true);
ServletServerHttpRequest inputMessage = createInputMessage(webRequest);
ServletServerHttpResponse outputMessage = createOutputMessage(webRequest);
// Try even with null return value. ResponseBodyAdvice could get involved.
// 使用消息转换器进行写出操作
writeWithMessageConverters(returnValue, returnType, inputMessage, outputMessage);
}
返回值解析原理:
1.利用 MessageConverters 进行处理 将数据写为json
1、内容协商(浏览器默认会以请求头的方式告诉服务器他能接受什么样的内容类型)
2、服务器最终根据自己自身的能力,决定服务器能生产出什么样内容类型的数据,
3、SpringMVC会挨个遍历所有容器底层的 HttpMessageConverter ,看谁能处理?
1、得到MappingJackson2HttpMessageConverter可以将对象写为json
2、利用MappingJackson2HttpMessageConverter将对象转为json再写出去。
SpringMVC到底支持哪些返回值:
ModelAndView
Model
View
ResponseEntity
ResponseBodyEmitter
StreamingResponseBody
HttpEntity
HttpHeaders
Callable
DeferredResult
ListenableFuture
CompletionStage
WebAsyncTask
有 @ModelAttribute 且为对象类型的
@ResponseBody 注解 ---> RequestResponseBodyMethodProcessor(返回值解析器);
HTTPMessageConverter(消息转换器)原理
1、HTTPMessageConverter接口
HttpMessageConverter: 看是否支持将 此 Class类型的对象,转为MediaType类型的数据。
例子:Person对象转为JSON。或者 JSON转为Person
2、HTTPMessageConverter接口实现类,有着不同的功能!
0 - 只支持Byte类型的
1 - String
2 - String
3 - Resource
4 - ResourceRegion
5 - DOMSource.class \ SAXSource.class) \ StAXSource.class \StreamSource.class \Source.class
6 - MultiValueMap
7 - true 支持所有类型
8 - true 支持所有类型
9 - 支持注解方式xml处理的。
最终 MappingJackson2HttpMessageConverter 把对象转为JSON(利用底层的jackson的objectMapper转换的)
根据客户端接收能力不同,返回不同媒体类型的数据。
返回xml
格式数据测试:
1、引入依赖
<dependency>
<groupId>com.fasterxml.jackson.dataformatgroupId>
<artifactId>jackson-dataformat-xmlartifactId>
dependency>
2、postman分别测试返回 json 和 xml
只需要改变请求头中Accept字段。Http协议中规定的,告诉服务器本客户端可以接收的数据类型。
json
xml
我们并没有改变代码,只是引入了xml的支持依赖,为什么SpringBoot就拥有了返回json和xml数据两种不同的能力??SpringBoot内容协商??
内容协商原理:
xml
数据。// 也就是匹配客户端需要的类型(application/xml)和服务端提供的类型(10种类型)
List<MediaType> mediaTypesToUse = new ArrayList<>();
for (MediaType requestedType : acceptableTypes) {
for (MediaType producibleType : producibleTypes) {
if (requestedType.isCompatibleWith(producibleType)) {
// 匹配成功
mediaTypesToUse.add(getMostSpecificMediaType(requestedType, producibleType));
}
}
}
/*
对于浏览器没有写明json情况下,是响应xml格式的数据,因为在Accept请求头字段里,xml的权重更高!所以
想要响应json格式的数据,需要开启浏览器参数方式内容协商功能。
*/
开启浏览器参数方式内容协商功能
为了方便内容协商,开启基于请求参数的内容协商功能。
application.yml
spring:
mvc:
contentnegotiation:
favor-parameter: true # 通过 format 请求参数指定内容协商类型
浏览器访问:http://localhost:8080/test/book?format=json
或是http://localhost:8080/test/book?format=xml
即可指定协商类型。
自定义 MessageConverter
@ResponseBody
响应数据出去 调用 RequestResponseBodyMethodProcessor 处理MessageConverter
处理MessageConverter
合起来可以支持各种媒体类型数据的操作(读、写)messageConverter
;/*
现有这样一个开发场景(多协议数据兼容):
1. 浏览器发起请求直接返回 xml。 [application/xml] jacksonXmlConverter
2. 如果是 ajax 请求,返回 json。 [application/json] jacksonJsonConverter
3. 如果硅谷app 发起的请求,返回自定义协议数据。 [application/x-guigu] jacksonXGuiguConverter
4. 自定义协议数据就为:属性值1;属性值2;
*/
@ResponseBody
@GetMapping("/test/book")
public Book getBook() {
Book book = new Book();
book.setId(2);
book.setBookName("大话数据结构");
book.setBookAuthor("程杰");
return book;
}
/*
实现步骤:
1.添加自定义的 MessageConverter 进系统底层
2.系统底层就会统计出所有 MessageConverter 能操作那些类型
3.内容协商,不同请求端,响应不同格式的数据
*/
添加或修改SpringMVC的功能。只需从一个入口给容器中添加一个 WebMvcConfigurer。
自定义 MessageConverter实现类:
/**
* @description: 自定义 MessageConverter
* @author: laizhenghua
* @date: 2021/3/31 19:54
*/
public class GuiguMessageConverter implements HttpMessageConverter<Book> {
@Override
public boolean canRead(Class<?> clazz, MediaType mediaType) {
return false;
}
@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return false;
}
/*
* SpringBoot底层需要统计所有 MessageConverter 都能写出哪些内容类型。
* application/x-guigu
*/
@Override
public List<MediaType> getSupportedMediaTypes() {
return MediaType.parseMediaTypes("application/x-guigu");
}
@Override
public Book read(Class<? extends Book> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
return null;
}
@Override
public void write(Book book, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
// 自定义协议数据的写出
String data = book.getId() + ";" + book.getBookName() + ";" + book.getBookAuthor();
// 写出去
OutputStream out = outputMessage.getBody();
out.write(data.getBytes());
}
}
给容器中添加一个 WebMvcConfigurer:
/**
* @description: WebMvcConfigurer 定制化 SpringMVC的功能
* @author: laizhenghua
* @date: 2021/3/31 19:50
*/
@Configuration
public class WebMvcConfig {
@Bean
public WebMvcConfigurer webMvcConfigurer() {
return new WebMvcConfigurer() {
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new GuiguMessageConverter()); // 添加自己消息转换器
}
};
}
}
视图解析:SpringBoot默认不支持 JSP,需要引入第三方模板引擎技术实现页面渲染。
前端交给我们的页面,是html页面。如果是我们以前开发,我们需要把他们转成jsp页面,jsp好处就是当我们查出一些数据转发到JSP页面以后,我们可以用jsp轻松实现数据的显示,及交互等。
jsp支持非常强大的功能,包括能写Java代码,但是呢,我们现在的这种情况,SpringBoot这个项目首先是以jar的方式,不是war,并且我们用的还是嵌入式的Tomcat,所以呢,他现在默认是不支持jsp的。
那不支持jsp,如果我们直接用纯静态页面的方式,那给我们开发会带来非常大的麻烦,那怎么办呢?
SpringBoot推荐我们使用模板引擎:
模板引擎:我们其实大家听到很多,其实jsp就是一个模板引擎,还有用的比较多的freemarker,包括SpringBoot给我们推荐的Thymeleaf。
注意引入Thymeleaf的依赖才能使用Thymeleaf:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
1、thymeleaf是现代化、服务端Java模板引擎 – 摘自官网
官网地址:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html
2、基本语法
注意页面上使用Thymeleaf语法时,需要加上名称空间:
<html xmlns:th="http://www.thymeleaf.org">
表达式名字 | 语法 | 用途 |
---|---|---|
变量取值 | ${…} | 获取请求与、session域、对象等值 |
选择变量 | *{…} | 获取上下文对象值 |
消息 | #{…} | 获取国际化等值 |
链接 | @{…} | 生成连接 |
片段表达式 | ~{…} | 与jsp:include作用一致。引入公共页面片段 |
1、字面量
文本值: one text'
、Another one!
等。数字: 0 , 34 , 3.0 , 12.3
等。布尔值: true , false
空值: null
变量:one,two,....
变量不能有空格
2、文本操作
字符串拼接: +
变量替换: |The name is ${name}
|
3、数学运算
运算符: + , - , * , / , %
4、布尔运算
运算符: and , or
一元运算: ! , not
5、比较运算
比较: > , < , >= , <= ( gt , lt , ge , le )
等式: == , != ( eq , ne )
6、条件运算
If-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)
7、特殊操作
无操作: _
3、设置属性值 – th:attr
设置单个属性:
<form action="subscribe.html" th:attr="action=@{/subscribe}">
<fieldset>
<input type="text" name="email" />
<input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>
fieldset>
form>
<form action="subscribe.html" th:action="@{/subscribe}">
<fieldset>
<input type="text" name="email" />
<input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/>
fieldset>
form>
设置多个属性:
<img src="../../images/gtvglogo.png" th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />
所有h5兼容的标签写法
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#setting-value-to-specific-attributes
4、循环
<tr th:each="prod : ${prods}">
<td th:text="${prod.name}">Onionstd>
<td th:text="${prod.price}">2.41td>
<td th:text="${prod.inStock}? #{true} : #{false}">yestd>
tr>
<tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'">
<td th:text="${prod.name}">Onionstd>
<td th:text="${prod.price}">2.41td>
<td th:text="${prod.inStock}? #{true} : #{false}">yestd>
tr>
5、条件运算
<a href="comments.html"
th:href="@{/product/comments(prodId=${prod.id})}"
th:if="${not #lists.isEmpty(prod.comments)}">viewa>
<div th:switch="${user.role}">
<p th:case="'admin'">User is an administratorp>
<p th:case="#{roles.manager}">User is a managerp>
<p th:case="*">User is some other thingp>
div>
引入依赖后,我们可以直接使用了。除了基本语法外,还需要知道Thymeleaf的使用规则。首先得按照SpringBoot的自动配置原理看一下我们这个Thymeleaf的自动配置规则:
// Thymeleaf的自动配置类
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(ThymeleafProperties.class)
@ConditionalOnClass({ TemplateMode.class, SpringTemplateEngine.class })
@AutoConfigureAfter({ WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class })
public class ThymeleafAutoConfiguration {
...
}
自动配好的策略:
ThymeleafProperties
SpringTemplateEngine
ThymeleafViewResolver
// ThymeleafProperties里的默认配置
public static final String DEFAULT_PREFIX = "classpath:/templates/"; // 前缀
public static final String DEFAULT_SUFFIX = ".html"; // 后缀
// 例如
@GetMapping("/index")
public String toIndexPage() {
return "index"; // 返回 "classpath:/templates/index.html" 页面
}
1、目标方法处理的过程中,所有数据都会被放在ModelAndViewContainer
里面。包括数据和视图地址。
2、方法的参数是一个自定义类型对象(从请求参数中确定的),把他重新放在ModelAndViewContainer
。
3、任何目标方法执行完成以后都会返回 ModelAndView
(数据和视图地址)。
4、processDispatchResult
处理派发结果(页面改如何响应)。
// 进入到 render 方法
1、render(mv,request,response) // 进行页面渲染逻辑
/*
根据方法的String返回值得到 View 对象(定义了页面的渲染逻辑):
1、所有的视图解析器尝试是否能根据当前返回值得到 View 对象
2、得到了 redirect:/main.html --> Thymeleaf new RedirectView()
3、ContentNegotiationViewResolver 里面包含了下面所有的视图解析器,内部还是利用下面所有视图解析器得到视图对象。
4、view.render(mv.getModelInternal(), request, response); 视图对象调用自定义的render进行页面渲染工作
RedirectView 如何渲染【重定向到一个页面】??
1、获取目标url地址
2、response.sendRedirect(encodedURL);
*/
new InternalResourceView(forwardUrl);
--> 转发request.getRequestDispatcher(path).forward(request, response);
new RedirectView()
—> render就是重定向render()
方法进行页面逻辑渲染1、SpringMVC
中的拦截器:将堆内存SpringMVC使用拦截器对请求进行拦截处理,以实现特定的功能。在AOP中,拦截器用于在某个方法或者字段被访问之前,进行拦截,然后再之前或者之后加入某些操作。
2、拦截器的特点:
可以定义拦截器链,连接器链就是将拦截器按着一定的顺序结成一条链,在访问被拦截的方法时,拦截器链 中的拦截器会按着定义的顺序执行。
拦截器和过滤器的功能比较类似,有区别:
过滤器是Servlet规范的一部分,任何框架都可以使用过滤器技术。
拦截器是SpringMVC框架独有的。
过滤器 url-pattern 配置了/*
,可以拦截任何资源。
拦截器只会对控制器中的方法进行拦截。
拦截器也是AOP思想的一种实现方式
要自定义拦截器,需要实现HandlerInterceptor
接口。
3、HandlerInterceptor 接口
拦截器的使用:
1、编写一个拦截器实现 HandlerInterceptor 接口
package com.laizhenghua.admin.interceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* @description: 登录拦截器
* @author: laizhenghua
* @date: 2021/4/3 22:00
*/
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
/**
* 目标方法执行之前
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("拦截器拦截的请求路径 : {}",request.getRequestURI());
// 登录检查逻辑
HttpSession session = request.getSession();
Object admin = session.getAttribute("admin");
if(admin != null) {
return true; // 放心请求
}
request.setAttribute("message","亲!您还未登录网站,不可以访问哦!");
request.getRequestDispatcher("/to_login").forward(request,response);
return false;
}
/**
* 目标方法执行完成以后
*/
@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 {
}
}
2、拦截器注册到容器中(实现 WebMvcConfigurer
的 addInterceptors()
方法),并制定拦截规则
package com.laizhenghua.admin.config;
import com.laizhenghua.admin.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @description: 自定义 WebMVC 组件
* @author: laizhenghua
* @date: 2021/4/3 22:11
*/
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// registry 拦截器注册中心
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**") // 所有请求都会拦截,包括静态资源
.excludePathPatterns("/admin/css/**","/admin/fonts/**","/admin/images/**","/admin/js/**") // 放行静态资源
.excludePathPatterns("/","/to_login","/admin/login"); // 不用拦截的请求
/*
1、编写一个拦截器实现 HandlerInterceptor 接口
2、拦截器注册到容器中(实现 WebMvcConfigurer 的 addInterceptors() 方法)
3、指定拦截器规则(如果是拦截所有,静态资源也会被拦截)
*/
}
}
拦截器原理:
1、根据当前请求,找到HandlerExecutionChain
【可以处理请求的handler以及handler的所有 拦截器】
2、先来顺序执行所有拦截器的 preHandle方法
3、如果任何一个拦截器返回false。直接跳出不执行目标方法
4、所有拦截器都返回True。执行目标方法
5、倒序执行所有拦截器的postHandle方法。
6、前面的步骤有任何异常都会直接倒序触发 afterCompletion
7、页面成功渲染完成以后,也会倒序触发 afterCompletion
1、页面表单
<form role="form" th:action="@{upload}" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="exampleInputEmail1">邮箱label>
<input type="email" name="email" class="form-control" id="exampleInputEmail1"
placeholder="Enter email">
div>
<div class="form-group">
<label for="exampleInputPassword1">账户label>
<input type="text" name="name" class="form-control" id="exampleInputPassword1"
placeholder="Name">
div>
<div class="form-group">
<label for="exampleInputFile">头像label>
<input type="file" name="headerImg" id="exampleInputFile">
<p class="help-block">Example block-level help text here.p>
div>
<div class="form-group">
<label for="exampleInputFile">生活照label>
<input type="file" name="photos" multiple>
<p class="help-block">Example block-level help text here.p>
div>
<div class="checkbox">
<label>
<input type="checkbox"> Check me out
label>
div>
<button type="submit" class="btn btn-primary">Submitbutton>
form>
2、文件上传代码
/**
* MultipartFile 自动封装上传过来的文件
*/
@PostMapping("/admin/upload")
public String upload(@RequestParam("email") String email,
@RequestParam("name") String name,
@RequestPart("headerImg") MultipartFile headerImg,
@RequestPart("photos") MultipartFile[] photos) throws IOException {
log.info("上传了文件 : headerImg : {}",headerImg.getSize());
log.info("上传了文件 : photos : {}", photos.length);
String basePath = null;
if(!headerImg.isEmpty()) {
// 保存到文件服务器或者是oss服务器
String filename = headerImg.getOriginalFilename();
// 根路径,在 resources/static/upload
basePath = ResourceUtils.getURL("classpath:").getPath() + "static/upload/";
assert filename != null;
File file = new File(basePath);
// 文件夹不存在,则新建
if(!file.exists()){
file.mkdirs();
}
// 保存文件
headerImg.transferTo(new File(basePath,filename));
}
if(photos.length > 0) {
for(MultipartFile file : photos) {
if(!file.isEmpty()) {
String filename = file.getOriginalFilename();
assert filename != null;
file.transferTo(new File(basePath,filename));
}
}
}
return "admin/index";
}
3、自动配置原理
文件上传自动配置类是MultipartAutoConfiguration
:
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ Servlet.class, StandardServletMultipartResolver.class, MultipartConfigElement.class })
@ConditionalOnProperty(prefix = "spring.servlet.multipart", name = "enabled", matchIfMissing = true)
@ConditionalOnWebApplication(type = Type.SERVLET)
@EnableConfigurationProperties(MultipartProperties.class) // 属性配置类
public class MultipartAutoConfiguration {
...
}
SpringBoot自动配好了文件上传解析器StandardServletMultipartResolver
。如果是以流的方式上传文件,我们需要自定义文件上传解析器。
文件上传原理:
MultipartHttpServletRequest
)文件上传请求MultiValueMap
1、错误处理
默认情况下,Spring Boot提供/error
处理所有错误的映射:
View
解析为error
ErrorController
并注册该类型的Bean定义,或添加ErrorAttributes类型的组件
以使用现有机制并替换其内容。error/
下的4xx,5xx页面会被自动解析。自定义错误页
error/404.html
、error/500.html
有精确的错误状态码页面就匹配精确,没有就找 4xx.html;如果都没有就触发白页@ControllerAdvice + @ExceptionHandler
处理全局异常。底层是 ExceptionHandlerExceptionResolver 支持的。所有controller产生的异常都由我们指定的方法处理!
@ResponseStatus+自定义异常
;底层是 ResponseStatusExceptionResolver ,把responsestatus注解的信息底层调用 response.sendError(statusCode, resolvedReason);tomcat发送的/error
Spring底层的异常,如参数类型转换异常;DefaultHandlerExceptionResolver
处理框架底层的异常。
自定义异常解析器:实现HandlerExceptionResolver
接口;可以作为默认的全局异常处理规则。
package com.laizhenghua.admin.exception;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @description: 自定义的异常解析器
* @author: laizhenghua
* @date: 2021/4/6 9:28
*/
@Order(value = Ordered.HIGHEST_PRECEDENCE) // 数字越小优先级越高(枚举类是一个数字)
@Component
public class CustomerHandlerExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
// 可定制自己的解析规则
try {
response.sendError(520,"我喜欢的错误!");
} catch (IOException e) {
e.printStackTrace();
}
return new ModelAndView();
}
}
ErrorViewResolver实现自定义异常处理
basicErrorController
要去的页面地址是 ErrorViewResolver ;3、异常处理自动配置原理
ErrorMvcAutoConfiguration
自动配置类 —> 自动配置异常处理规则
// 重要组件1
@Bean
@ConditionalOnMissingBean(value = ErrorAttributes.class, search = SearchStrategy.CURRENT)
public DefaultErrorAttributes errorAttributes() {
return new DefaultErrorAttributes();
}
// 我们发现 DefaultErrorAttributes类 实现的接口有:
public class DefaultErrorAttributes implements ErrorAttributes, HandlerExceptionResolver, Ordered {
...
// 定义错误页面中可以包含哪些数据
}
// 重要组件2
@Bean
@ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT)
public BasicErrorController basicErrorController(ErrorAttributes errorAttributes,
ObjectProvider<ErrorViewResolver> errorViewResolvers) {
return new BasicErrorController(errorAttributes, this.serverProperties.getError(),
errorViewResolvers.orderedStream().collect(Collectors.toList()));
}
// BasicErrorController映射的规则
@Controller
@RequestMapping("${server.error.path:${error.path:/error}}") // 默认处理 /error 路径的请求
public class BasicErrorController extends AbstractErrorController {
...
// 根据内容协商响应不同的数据(json 或者是 白页面whitelabel)
}
/*
白页面响应:new ModelAndView("error", model)
*/
// 重要组件3(id为 error 的组件) --> 响应默认的错误页
@Bean(name = "error")
@ConditionalOnMissingBean(name = "error")
public View defaultErrorView() {
return this.defaultErrorView;
}
// 容器中还存放组件 BeanNameViewResolver(视图解析器);按照返回的视图名作为组件的id去容器中找View对象
// 如果想要返回页面;就会找error视图【StaticView】。(默认是一个白页)
// 重要组件4(错误视图解析器)
@Bean
@ConditionalOnBean(DispatcherServlet.class)
@ConditionalOnMissingBean(ErrorViewResolver.class)
DefaultErrorViewResolver conventionErrorViewResolver() {
return new DefaultErrorViewResolver(this.applicationContext, this.resourceProperties);
/*
功能描述:
如果发生错误,会以HTTP的状态码 作为视图页地址(viewName),找到真正的页面
error/404、5xx.html
*/
}
4、异常处理流程
1、请求过来时,找到可以处理请求的处理器映射!处理器映射执行目标方法。目标方法运行期间有任何异常都会被catch、而且标志当前请求结束,并且用 dispatchException
记录目标方法出现的异常。
2、拿到dispatchException
记录的异常后,执行此方法:processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
进入到视图解析流程。
3、mv = processHandlerException(request, response, handler, exception);
处理handler发生的异常,处理完成返回ModelAndView
/*
1、DefaultErrorAttributes先来处理异常。把异常信息保存到rrequest域,并且返回null;
2、默认没有任何人能处理异常,所以异常会被抛出
1、如果没有任何人能处理最终底层就会发送 /error 请求。会被底层的 BasicErrorController 处理
2、解析错误视图;遍历所有的 ErrorViewResolver 看谁能解析。
3、默认的 DefaultErrorViewResolver ,作用是把响应状态码作为错误页的地址,error/500.html
4、模板引擎最终响应这个页面 error/500.html
*/
1、使用Servlet API
/**
* @description: 新建一个类并用 @WebServlet() 标注
* @author: laizhenghua
* @date: 2021/4/6 10:16
*/
@WebServlet(urlPatterns = "/my")
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("66666");
}
}
主程序类:
@SpringBootApplication
@ServletComponentScan(basePackages = "com.laizhenghua.admin.servlet") // 指定原生 Servlet组件位置
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
}
}
当我们在浏览器中访问:http://localhost:8080/my
,会发现SpringBoot中的拦截器并没有进行拦截,而是直接响应!这是为什么呢?
/*
容器中有两个servlet:
1.MyServlet --> /my
2.DispatcherServlet(统一派发器) --> /
DispatchServlet 如何注册进来呢? DispatcherServletAutoConfiguration 自动配置类中
1.容器中自动配置了 DispatcherServlet 属性绑定到 WebMvcProperties;对应的配置文件配置项是 spring.mvc。
2.通过 ServletRegistrationBean 把 DispatcherServlet 配置进来。
3.默认映射的是 / 路径。
Tomcat-Servlet;
多个 Servlet 都能处理到同一层路径,精确优选原则
A: /my/
B: /my/1
*/
使用Servlet3.0配置自定义Filter:
/**
* @description: 过滤器 WebFilter 可以指定要拦截的资源
* @author: laizhenghua
* @date: 2021/4/6 10:33
*/
@WebFilter(urlPatterns = {"/admin/css/*"}) // 注意 * 和 ** 的写法,*为servlet写法,**为Spring写法
@Slf4j
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("MyFilter初始化完成");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
log.info("MyFilter开始工作");
chain.doFilter(request,response);
}
@Override
public void destroy() {
log.info("MyFilter已销毁");
}
}
使用Servlet3.0的注解自定义原生Listener监听器:
/**
* @description: 监听器
* @author: laizhenghua
* @date: 2021/4/6 10:43
*/
@WebListener
@Slf4j
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
log.info("MyServletContextListener监听到项目初始化完成");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
log.info("MyServletContextListener监听到项目已销毁");
}
}
以上都是Servlet 3.0提供的注解,用于注册原生的三大web组件。我们还有更简便的注册方式,这也是比较推荐的方式,通过RegistrationBean
注册配置配置原生组件。
2、通过RegistrationBean注册原生组件
ServletRegistrationBean
、 FilterRegistrationBean
、 and ServletListenerRegistrationBean
:
/**
* @description:
* @author: laizhenghua
* @date: 2021/4/6 12:18
*/
@Configuration
public class MyRegistrationConfig {
@Bean
public ServletRegistrationBean<MyServlet> myServlet() {
MyServlet myServlet = new MyServlet();
return new ServletRegistrationBean<>(myServlet,"/my");
}
@Bean
public FilterRegistrationBean<MyFilter> myFilter() {
MyFilter myFilter = new MyFilter();
FilterRegistrationBean<MyFilter> filterRegistrationBean = new FilterRegistrationBean<>(myFilter);
filterRegistrationBean.setUrlPatterns(Arrays.asList("/admin/css/*","/my"));
return filterRegistrationBean;
}
@Bean
public ServletListenerRegistrationBean<MyServletContextListener> myListener() {
MyServletContextListener myListener = new MyServletContextListener();
return new ServletListenerRegistrationBean<>(myListener);
}
}
1、切换嵌入式的 Servlet 容器
我们都知道 SpringBoot 默认使用Tomcat作为嵌入式的Servlet容器。SpringBoot也支持切换不同的web 服务器。如Tomcat
、Jetty
、or Undertow
。
ServletWebServerApplicationContext
容器启动寻找ServletWebServerFactory 并引导创建服务器。
我们想要切换服务器,只需排除tomcat服务器并引入相应服务器的依赖场景即可:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcatartifactId>
<groupId>org.springframework.bootgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jettyartifactId>
dependency>
原理:
ServletWebServerApplicationContext
ServletWebServerApplicationContext
启动的时候寻找 ServletWebServerFactory
(Servlet 的web服务器工厂—> Servlet 的web服务器)TomcatServletWebServerFactory
, JettyServletWebServerFactory
, or UndertowServletWebServerFactory
ServletWebServerFactoryAutoConfiguration
ServletWebServerFactoryAutoConfiguration
导入了ServletWebServerFactoryConfiguration
(配置类)ServletWebServerFactoryConfiguration
配置类 根据动态判断系统中到底导入了那个Web服务器的包。(默认是web-starter导入tomcat包),容器中就有 TomcatServletWebServerFactory
TomcatServletWebServerFactory
创建出Tomcat服务器并启动;TomcatWebServer
的构造器拥有初始化方法initialize — this.tomcat.start();
2、定制servlet容器
WebServerFactoryCustomizer
接口
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;
@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
@Override
public void customize(ConfigurableServletWebServerFactory server) {
server.setPort(9000);
}
}
xxxxxCustomizer:SpringBoot中称为定制化器,可以改变xxxx的默认规则。
3、定制化原理
/*
定制化常见的方式:
1.修改配置文件;
2.使用xx定制化器:xxxxxCustomizer;
3.编写自定义的配置类 xxxConfiguration;+ @Bean 替换、增加容器中默认组件;视图解析器
4.Web应用 编写一个配置类实现 WebMvcConfigurer 即可定制化web功能;+ @Bean给容器中再扩展一些组件
*/
// 如自定义 WebMVC 组件
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {
// 定制格式化器
// 定制拦截器等
...
}
/*
全面接管 SpringMVC:
1.@EnableWebMvc + WebMvcConfigurer —— @Bean 可以全面接管SpringMVC
2.所有规则全部自己重新配置; 实现定制和扩展功能
原理:
1、WebMvcAutoConfiguration 默认的SpringMVC的自动配置功能类。静态资源、欢迎页.....
2、一旦使用 @EnableWebMvc 会 @Import(DelegatingWebMvcConfiguration.class)
3、DelegatingWebMvcConfiguration 的 作用,只保证SpringMVC最基本的使用
1.把所有系统中的 WebMvcConfigurer 拿过来。所有功能的定制都是这些 WebMvcConfigurer 合起来一起生效
2.自动配置了一些非常底层的组件。RequestMappingHandlerMapping、这些组件依赖的组件都是从容器中获取
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport
4、WebMvcAutoConfiguration 里面的配置要能生效 必须 @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
5、@EnableWebMvc 导致了 WebMvcAutoConfiguration 没有生效。
*/
原理分析套路:场景starter —> xxxxAutoConfiguration
—> 导入xxx组件 —> 绑定xxxProperties
—> 绑定配置文件项
我们先引入 jdbc 的场景:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jdbcartifactId>
dependency>
jdbc 场景自动引入的依赖:
值得注意的是:JDBC场景并没有为我们导入数据库的连接驱动!因为官方不知道我们接下来要操作什么数据库。
MySQL数据库驱动(数据库版本和驱动版本对应):
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.38version>
dependency>
JDBC的自动配置类:
DataSourceAutoConfiguration
:数据源的自动配置DataSourceTransactionManagerAutoConfiguration
:事务管理器的自动配置JdbcTemplateAutoConfiguration
:JdbcTemplate自动配置,可以来对数据库进行crud1、DataSourceAutoConfiguration 自动配置类
spring.datasource
@Configuration(proxyBeanMethods = false)
@Conditional(PooledDataSourceCondition.class)
@ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
@Import({ DataSourceConfiguration.Hikari.class, DataSourceConfiguration.Tomcat.class,
DataSourceConfiguration.Dbcp2.class, DataSourceConfiguration.OracleUcp.class,
DataSourceConfiguration.Generic.class, DataSourceJmxConfiguration.class })
protected static class PooledDataSourceConfiguration {
...
}
2、JdbcTemplateAutoConfiguration 自动配置类
@Bean
、@Primary
, JdbcTemplate;容器中有这个组件3、修改数据库连接信息
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://180.76.178.21:3306/ssm_grxz?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: laizhenghua
password: 123456
jdbc:
template:
query-timeout: 3 # 3s超时
4、测试
@SpringBootTest
class AdminApplicationTests {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void jdbcCRUDTest() {
String sql = "select count(*) from admin";
Long count = jdbcTemplate.queryForObject(sql, Long.class);
System.out.println("记录数:" + count);
}
}
Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0、DBCP、Proxool等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB连接池,可以说是目前最好的连接池之一。
druid官方github地址:https://github.com/alibaba/druid
我们使用 druid 作为数据源属于整合第三方技术!我们可以使用两种方式进行整合:
1、自定义方式
① 创建数据源
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.10version>
dependency>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="20" />
bean>
编写 druid 配置类:
/**
* @description: Druid 数据源的配置
* @author: laizhenghua
* @date: 2021/4/7 9:41
*/
@Configuration
public class DruidDataSourceConfig {
/**
1、默认的自动配置是判断容器中没有才会配@ConditionalOnMissingBean(DataSource.class)
2、将自定义的 Druid数据源添加到容器中,不再让 Spring Boot 自动创建
绑定全局配置文件中的 druid 数据源属性到 com.alibaba.druid.pool.DruidDataSource从而让它们生效
注解 @ConfigurationProperties(prefix = "spring.datasource"):作用就是将 全局配置文件中
前缀为 spring.datasource的属性值注入到 com.alibaba.druid.pool.DruidDataSource 的同名参数中
*/
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
return dataSource;
}
}
② StatViewServlet
Druid内置提供了一个StatViewServlet用于展示Druid的统计信息 以前的配置方式 我们使用RegistrationBean注册的方式注入 配好以后,我们访问: 要打开SQL的监控功能,还需要通过 StatFilter 打开Druid的监控统计功能。StatFilter 用于统计监控信息;如SQL监控、URI监控。 以前的配置: 系统中所有filter: 修改配置: 配好 StatFilter 我们可以自己编写访问SQL的controller接口,测试SQL的监控功能。 Web应用 监控: 2、Druid 提供的场景启动器 starter 相对于上面自定义的方式,我们需要各种配置,而starter的方式更加简洁,可以简化我们的开发。 starter里的自动配置类: 默认配好的filter: 明白这些配置好的组件后,我们只需根据需要在全局配置文件中配置即可。配置示例: 更多可配置项: MyBatis GitHub地址:https://github.com/mybatis 在 SpringBoot 中 MyBatis 属于第三方技术,所以在starter的命名上遵循 引入的依赖: 一切已准备就绪,我们来简单测试一下MyBatis是否整合成功: 1、编写实体类 2、编写dao接口 3、编写Mapper映射文件(AdminMapper.xml) 4、修改全局配置文件(application.yml) 配置了 5、编程测试API 浏览器访问: 很喜欢 MyBatis-plus 的一句话:我们不做改变只做增强,笔者也很喜欢MyBatis-plus,MyBatis与MyBatis-plus是亲兄弟,两者互相配合才能发挥1+1大于2的效果。 MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。这里主要介绍与SpringBoot整合相关知识。更多有关MyBatis-plus的知识(逻辑删除、自动填充、乐观锁与悲观锁,分页查询等),可自行学习。 MyBatis-plus官网:https://mp.baomidou.com/ 我们将通过一个简单的 Demo 来阐述 MyBatis-Plus 的强大功能。准备数据库脚本: 自动配置类: 1、编写实体类(user) 2、编写dao接口 3、编写业务层接口与实现类 业务层接口实现类 4、controller层API 5、前端 thymeleaf 模板语法渲染 效果: MyBatis-plus分页: 1、添加分页插件 2、修改controller层响应的数据 3、前端代码 NoSQL= Not Only SQL (不仅仅是SQL)。泛指非关系型数据库,随着web2.0互联网的诞生,传统的关系型数据库很难对付web2.0时代的产物,尤其是大规模、高并发的数据社区。暴露出来很多难以客服的问题,NoSQL在当今大数据环境卞发展的十分迅速,Redis是发展最快的,而且是我们当下必须要掌握的一个技术。 所以我们以redis为例进行NoSQL的整合,首先引入starter: 修改SpringBoot的全局配置文件: 简单测试: 2、jedis 与 Lettuce 说明:在 /SpringBoot 2.0以后,原来使用的jedis被替换成为了 lettuce,我们也可以手动切换至 jedis。 两种客户端: jedis:采用的直连,多个线程操作不安全,若要避免,则要使用jedis pool连接池,更像Bio模式 lettuce:采用netty,可以在多个线程中共享,不存在线程不安全的情况,可以减少线程数量,更像Nio模式 切换至 jedis: 引入 jedis 依赖 修改配置 Spring Boot 2.2.0 版本开始引入 JUnit 5 作为单元测试默认库 作为最新版本的JUnit框架,JUnit5与之前版本的Junit框架有很大的不同。由三个不同子项目的几个不同模块组成。 JUnit Platform: Junit Platform是在JVM上启动测试框架的基础,不仅支持Junit自制的测试引擎,其他测试引擎也都可以接入。 JUnit Jupiter: JUnit Jupiter提供了JUnit5的新的编程模型,是JUnit5新特性的核心。内部 包含了一个测试引擎,用于在Junit Platform上运行。 JUnit Vintage: 由于JUint已经发展多年,为了照顾老的项目,JUnit Vintage提供了兼容JUnit4.x,Junit3.x的测试引擎。 现在版本( 以前版本( 现在版本官方引入的依赖(starter): JUnit5的注解与JUnit4的注解有所变化 https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations 断言(assertions)是测试方法中的核心部分,用来对测试需要满足的条件进行验证。这些断言方法都是 1、简单断言 用来对单个值进行简单的验证。如: 2、数组断言 通过 assertArrayEquals 方法来判断两个对象或原始类型的数组是否相等 3、组合断言 assertAll 方法接受多个 4、异常断言 在JUnit4时期,想要测试方法的异常情况时,需要用@Rule注解的ExpectedException变量还是比较麻烦的。而JUnit5提供了一种新的断言方式 5、快速失败 通过 fail 方法直接使得测试失败 未来每一个微服务在云上部署以后,我们都需要对其进行监控、追踪、审计、控制等。SpringBoot就抽取了Actuator场景,使得我们每个微服务快速引用即可获得生产级别的应用监控、审计等功能。这些功能需要引入场景依赖才能生效: 关于指标监控 1.x 与 2.x 的比较: 监控指标的可视化web开源项目:https://github.com/codecentric/spring-boot-admin 使用指标监控功能的方式: … 等等更多可查看端点信息可查看官网描述。 1、常用端点 如果您的应用程序是Web应用程序(Spring MVC,Spring WebFlux或Jersey),则可以使用以下附加端点: 最常用的Endpoint 2、Health Endpoint 健康检查端点,我们一般用于在云平台,平台会定时的检查应用的健康状况,我们就需要 重要的几点: 当我们访问 再次访问: 3、Metrics Endpoint 提供详细的、层级的、空间指标信息,这些信息可以被 push(主动推送)或者 pull(被动获取)方式得到: 一次请求: 在实际应用中,我们不应该在 web 端暴露所有端点,这是非常危险的。根据需要暴露指定的端点,才是正确的做法。 开启与禁用Endpoints: 或者禁用所有的Endpoint然后手动开启指定的Endpoint 1、定制 health 信息 访问: 默认情况下,info 并没有展示什么信息。所以我们需要自定 info 信息。 3、定制Metrics信息 SpringBoot支持自动适配的Metrics: JVM metrics, report utilization of: CPU metrics File descriptor metrics Kafka consumer and producer metrics Log4j2 metrics: record the number of events logged to Log4j2 at each level Logback metrics: record the number of events logged to Logback at each level Uptime metrics: report a gauge for uptime and a fixed gauge representing the application’s absolute start time Tomcat metrics ( Spring Integration metrics 增加定制Metrics: 我们都知道一个产品的开发有开发、测试、生产等环境,为了方便多环境适配,SpringBoot简化了proflie功能。 官方地址:https://docs.spring.io/spring-boot/docs/2.3.9.RELEASE/reference/html/spring-boot-features.html#boot-features-profiles 命令行激活: @Profile不仅能标注在类上,还能标注在方法上。 我们要修改已上线的产品的配置信息,我们可能会这样操作:关闭上线的应用 -> 修改配置 -> 重新打包部署。但是这样的流程相对比较繁琐。 而在SpringBoot中允许你外化(externalize)你的配置,这样你能够在不同的环境下使用相同的代码。你可以使用 官网地址:https://docs.spring.io/spring-boot/docs/2.3.9.RELEASE/reference/html/spring-boot-features.html#boot-features-external-config 1、常用的外部配置源 官方说明的外部配置源十多种,但是我们最常用的还是Java属性文件、YAML文件、环境变量、命令行参数; 2、配置文件查找位置 3、配置文件加载顺序: 4、重点记住这句话:指定环境优先,外部优先,后面的可以覆盖前面的同名配置项 SpringBoot的一大优势就是Starter,由于SpringBoot有很多开箱即用的Starter依赖,使得我们开发变得简单,我们不需要过多的关注框架的配置。 在日常开发中,我们也会自定义一些Starter,特别是现在微服务框架,我们一个项目分成了多个单体项目,而这些单体项目中会引用公司的一些组件,这个时候我们定义Starter,可以使这些单体项目快速搭起,我们只需要关注业务开发。 在此之前我们再深入的了解下SpringBoot启动原理。而后再将如何自定义starter。 编写自动配置类 xxxAutoConfiguration -> xxxxProperties @Configuration 引入starter — xxxAutoConfiguration — 容器中放入组件 — 绑定xxxProperties — 配置项 模式: 启动器只用来做依赖导入; 专门来写一个自动配置模块; 启动器依赖自动配置;别人只需要引入启动器(starter) mybatis-spring-boot-starter;命名规范:自定义启动器名-spring-boot-starter 也就是说,我们只需编写好启动器(starter)和自动配置(autoconfigurer)就能完成自定义starter。 如何编写自动配置? 步骤1:编写启动器(starter) 通过maven创建一个工程,命名为 步骤2:编写自动配置模块(autoconfigure) 使用 SpringBoot 的初始化向导,命名为 步骤3:在场景启动器中引入自动配置模块的依赖 步骤4:就是编写自己需要的服务,并把服务安装到本机仓库中(maven 执行 install 命令,注意去掉springboot的maven插件才能打包成功) 如自己写了个 HelloProperties.java HelloService.java 自动配置类要能加载,需要将启动就加载的自动配置类,配置在META-INF/spring.factories spring.factories 步骤5:在其他项目中引入自定义的 starter 依赖,并测试服务。 application.yml 编写controller访问API: 测试: SpringBoot的启动,又分为创建SpringApplication对象与运行run()方法两个步骤! SpringApplication对象的初始化器: run() 方法源码: https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-application-events-and-listeners 几个重要的事件回调机制。配置在
<servlet>
<servlet-name>DruidStatViewservlet-name>
<servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>DruidStatViewservlet-name>
<url-pattern>/druid/*url-pattern>
servlet-mapping>
com.alibaba.druid.support.http.StatViewServlet
:/*
配置 druid 的监控页功能
*/
@Bean
public ServletRegistrationBean<StatViewServlet> statViewServlet() {
StatViewServlet statViewServlet = new StatViewServlet();
return new ServletRegistrationBean<>(statViewServlet,"/druid/*");
}
http://localhost:8080/druid/index.html
就可以进入 druid 的监控页。
③ StatFilter
<property name="filters" value="stat,slf4j" />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
... ...
<property name="filters" value="stat,log4j" />
bean>
别名
Filter类名
default
com.alibaba.druid.filter.stat.StatFilter
stat
com.alibaba.druid.filter.stat.StatFilter
mergeStat
com.alibaba.druid.filter.stat.MergeStatFilter
encoding
com.alibaba.druid.filter.encoding.EncodingConvertFilter
log4j
com.alibaba.druid.filter.logging.Log4jFilter
log4j2
com.alibaba.druid.filter.logging.Log4j2Filter
slf4j
com.alibaba.druid.filter.logging.Slf4jLogFilter
commonlogging
com.alibaba.druid.filter.logging.CommonsLogFilter
/*
配置 druid 的监控页功能
*/
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setFilters("stat,wall"); // 别名配置,打开监控统计功能
return dataSource;
}
/*
webStatFilter 用于采集 web-jdbc 关联监控的数据
*/
@Bean
public FilterRegistrationBean<WebStatFilter> webStatFilter() {
WebStatFilter webStatFilter = new WebStatFilter();
FilterRegistrationBean<WebStatFilter> registrationBean = new FilterRegistrationBean<>(webStatFilter);
registrationBean.setUrlPatterns(Arrays.asList("/*"));
HashMap<String, String> map = new HashMap<>();
map.put("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
registrationBean.setInitParameters(map);
return registrationBean;
}
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.17version>
dependency>
DruidDataSourceAutoConfigure
:@Configuration
@ConditionalOnClass(DruidDataSource.class)
@AutoConfigureBefore(DataSourceAutoConfiguration.class) // 提前生效
@EnableConfigurationProperties({DruidStatProperties.class, DataSourceProperties.class})
@Import({DruidSpringAopConfiguration.class, //监控SpringBean的;配置项:spring.datasource.druid.aop-patterns
DruidStatViewServletConfiguration.class, // 监控页的配置:spring.datasource.druid.stat-view-servlet;默认开启
DruidWebStatFilterConfiguration.class, // web监控配置;spring.datasource.druid.web-stat-filter;默认开启
DruidFilterConfiguration.class}) // 所有Druid自己filter的配置
public class DruidDataSourceAutoConfigure {
private static final Logger LOGGER = LoggerFactory.getLogger(DruidDataSourceAutoConfigure.class);
// 数据源
@Bean(initMethod = "init")
@ConditionalOnMissingBean
public DataSource dataSource() {
LOGGER.info("Init DruidDataSource");
return new DruidDataSourceWrapper();
}
}
private static final String FILTER_STAT_PREFIX = "spring.datasource.druid.filter.stat";
private static final String FILTER_CONFIG_PREFIX = "spring.datasource.druid.filter.config";
private static final String FILTER_ENCODING_PREFIX = "spring.datasource.druid.filter.encoding";
private static final String FILTER_SLF4J_PREFIX = "spring.datasource.druid.filter.slf4j";
private static final String FILTER_LOG4J_PREFIX = "spring.datasource.druid.filter.log4j";
private static final String FILTER_LOG4J2_PREFIX = "spring.datasource.druid.filter.log4j2";
private static final String FILTER_COMMONS_LOG_PREFIX = "spring.datasource.druid.filter.commons-log";
private static final String FILTER_WALL_PREFIX = "spring.datasource.druid.filter.wall";
private static final String FILTER_WALL_CONFIG_PREFIX = FILTER_WALL_PREFIX + ".config";
spring:
servlet:
multipart:
max-file-size: 10MB # 指定文件上传大小
max-request-size: 100MB # 指定文件上传请求大小
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://180.76.178.21:3306/ssm_grxz?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: laizhenghua
password: 123
druid:
filter:
stat:
log-slow-sql: true
slow-sql-millis: 1000
enabled: true
wall:
enabled: true
config:
drop-table-allow: false
aop-patterns: com.laizhenghua.admin.*
stat-view-servlet:
enabled: true # 是否开启Druid监控页功能
login-username: druid
login-password: druid
resetEnable: false # 是否开启一键清空监控信息
web-stat-filter:
enabled: true # 是否打开SQL URL等监控功能
url-pattern: /*
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
jdbc:
template:
query-timeout: 3 # 3s超时
https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter3、整合 MyBatis
*-spring-boot-starter
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>
在以前持久层我们使用 MyBatis 需要编写全局配置文件、SqlSessionFactory、SqlSession、Mapper映射文件等一堆东西,然而引入MyBatis场景启动器后,我已不需要在编写复杂的SqlSessionFactory或配置文件,MybatisAutoConfiguration
都给我们自动配置好了。MybatisAutoConfiguration
自动配置类:
@org.springframework.context.annotation.Configuration
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties(MybatisProperties.class) // MyBatis配置项绑定的类
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class })
public class MybatisAutoConfiguration implements InitializingBean {
...
// 1、自动配置好了 SqlSessionFactory
// 2、自动配置了 SqlSessionTemplate 组合了 SqlSession
/*
3、@Import(AutoConfiguredMapperScannerRegistrar.class) 找到 @Mapper 标注的接口并自动扫描注入,
所以只要我们写的操作MyBatis的接口标注了 @Mapper 就会被自动扫描进来
*/
}
// MybatisProperties配置类
@ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)
public class MybatisProperties {
public static final String MYBATIS_PREFIX = "mybatis"; // 配置文件前缀
...
}
// 可以修改全局配置文件中 mybatis 开头的所有配置;
/**
* @description: admin 实体类
* @author: laizhenghua
* @date: 2021/4/7 20:00
*/
@Data
public class AdminEntity {
private String adminId;
private String username;
private String password;
private String realName;
private String contact;
}
/**
* @description: dao 接口
* @author: laizhenghua
* @date: 2021/4/7 20:10
*/
@Mapper
public interface AdminMapper {
public AdminEntity getAdminEntityById(@Param("id") String id);
}
<mapper namespace="com.laizhenghua.admin.dao.AdminMapper">
<select id="getAdminEntityById" resultType="com.laizhenghua.admin.bean.AdminEntity">
select adminid adminId,username,password,realname realName,contact from admin where adminid = #{id}
select>
mapper>
# 配置 MyBatis 的规则
mybatis:
config-location: classpath:mybatis/mybatis-config.xml # myBatis全局配置文件位置
mapper-locations: classpath:mybatis/mapper/*.xml # SQL映射文件
configuration:
map-underscore-to-camel-case: true # 开启驼峰命名规则
private Configuration configuration;
。mybatis.configuration下面的所有,就是相当于改mybatis全局配置文件中的值。例如开启驼峰命名规则。注意:config-location
和configuration
不能同时存在!意思就是要么使用myBatis全局配置文件修改相关配置,要么就在SpringBoot的全局配置文件中修改MyBatis的相关配置。# 配置 MyBatis 的规则
mybatis:
# config-location: classpath:mybatis/mybatis-config.xml # myBatis全局配置文件位置
mapper-locations: classpath:mybatis/mapper/*.xml # SQL映射文件
configuration:
map-underscore-to-camel-case: true # 开启驼峰命名规则
/**
* @description: 测试MyBatis
* @author: laizhenghua
* @date: 2021/4/7 20:38
*/
@RestController
public class MyBatisController {
@Autowired
private AdminMapper adminMapper;
@GetMapping("/mybatis/{id}")
public AdminEntity getAdminEntity(@PathVariable("id") String id){
AdminEntity adminEntityById = adminMapper.getAdminEntityById(id);
return adminEntityById;
}
}
http://localhost:8080/mybatis/1
并查看SQL监控信息。
小结:
mybatis.configuration
)4、整合 MyBatis-plus
DROP TABLE IF EXISTS user;
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');
好了数据库,已经准备完毕,然后添加 MyBatis-plus
依赖:
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.2version>
dependency>
MybatisPlusAutoConfiguration
:@Configuration
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties(MybatisPlusProperties.class) // 配置项绑定的类
@AutoConfigureAfter({DataSourceAutoConfiguration.class, MybatisPlusLanguageDriverAutoConfiguration.class})
public class MybatisPlusAutoConfiguration implements InitializingBean {
...
// 1、自动配置好了 SqlSessionFactory。底层是容器中默认的数据源
// 2、private String[] mapperLocations = new String[]{"classpath*:/mapper/**/*.xml"}; SQL映射文件的默认位置。
// 任意包的类路径下的所有 mapper 文件夹下任意路径下的所有xml都是sql映射文件。 建议以后sql映射文件,放在 mapper下
// 3、自动配好了SqlSessionTemplate
// 4、@Mapper 标注的接口也会被自动扫描;建议直接 @MapperScan("com.atguigu.admin.mapper") 批量扫描就行
// 优点:开发时只需要我们的Mapper继承 BaseMapper 就可以拥有crud能力,无需编写SQL语句,根据条件构造器,可满足大多数场景。
}
// 配置项绑定的类
@Data
@Accessors(chain = true)
@ConfigurationProperties(prefix = Constants.MYBATIS_PLUS) // 前缀是 mybatis-plus
public class MybatisPlusProperties {
...
}
/**
* @description: 用户实体类
* @author: laizhenghua
* @date: 2021/4/1 13:47
*/
@Data
@TableName("user")
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
/**
* @description: dao接口
* @author: laizhenghua
* @date: 2021/4/7 22:51
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
// 继承BaseMapper该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
}
/**
* @description: 业务层接口
* @author: laizhenghua
* @date: 2021/4/8 8:24
*/
public interface UserService extends IService<User> {
// 继承 IService 后,拥有很多crud方法,也可以自定义接口方法,非常方便
}
/**
* @description: 业务层接口实现类
* @author: laizhenghua
* @date: 2021/4/8 8:26
*/
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
// UserMapper 相当于自动注入了 UserMapper 类型组件
}
@Autowired
private UserService userService;
/**
API:http://localhost:8080/admin/dynamic_table
展示用户信息(user表)
*/
@GetMapping("/dynamic_table")
public String toDynamicTablePage(Model model) {
List<User> userList = userService.list();
model.addAttribute("userList",userList);
return "admin/table/dynamic_table";
}
<tbody>
<tr class="gradeX odd" th:each="user : ${userList}">
<td th:text="${user.id}">td>
<td th:text="${user.name}">td>
<td th:text="${user.age}">Win 95+td>
<td class="center hidden-phone" th:text="${user.email}">4td>
<td class="center hidden-phone">
<a href="#">编辑a>
<a href="#">删除a>
td>
tr>
tbody>
以上操作,我们只完成了 查 的操作,更多增删该可自己练习完成。/**
* @description: MyBatis-plus的配置
* @author: laizhenghua
* @date: 2021/4/8 10:33
*/
@Configuration
public class MyBatisPlusConfig {
/**
* 注入分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
/**
API:http://localhost:8080/admin/dynamic_table
展示用户信息(user表)
*/
@GetMapping("/dynamic_table")
public String toDynamicTablePage(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, Model model) {
Page<User> userPage = new Page<>(pageNum,3); // 分页模型
// 分页查询的结果
Page<User> page = userService.page(userPage, null);
model.addAttribute("page",page);
return "admin/table/dynamic_table";
}
<ul>
<li class="prev disabled"><a href="#">← 上一页a>li>
<li th:class="${num == page.current ? 'active' : ''}" th:each="num : ${#numbers.sequence(1,page.pages)}">
<a th:href="@{'/admin/dynamic_table?pageNum=' + ${num}}" th:text="${num}">a>
li>
<li class="next disabled"><a href="#">下一页 → a>li>
ul>
3.2、NoSQL
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
1、redis的自动配置:
RedisAutoConfiguration
自动配置类:@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class) // Redis配置项绑定的类,spring.redis.xxx 是对redis的配置
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
// 连接工厂是准备好的。LettuceConnectionConfiguration、JedisConnectionConfiguration。也就是说支持两种客户端操作redis
public class RedisAutoConfiguration {
...
// 1、自动注入了 RedisTemplate
// 2、自动注入了 StringRedisTemplate类型的组件;k:v都是String
// 3、底层只要我们使用 StringRedisTemplate、RedisTemplate 就可以操作 redis
}
spring:
# Redis 配置
redis:
host: 49.234.92.74 # 换成自己的服务器 ip 地址
port: 6379
@Slf4j
@SpringBootTest
public class RedisTest {
@Autowired
private StringRedisTemplate redisTemplate;
@Test
public void redisTest() {
ValueOperations<String, String> ops = redisTemplate.opsForValue();
ops.set("hello","hello redis");
String hello = ops.get("hello");
log.info("hello -> {}",hello);
}
}
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
dependency>
spring:
redis:
host: r-bp1nc7reqesxisgxpipd.redis.rds.aliyuncs.com
port: 6379
password: lfy:Lfy123456
client-type: jedis # 切换客户端
jedis:
pool:
max-active: 10
4、单元测试
4.1、JUnit5 的变化
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
JUnit 5’s Vintage Engine Removed from spring-boot-starter-test
。如果需要继续兼容junit4需要自行引入vintage。@SpringBootTest
+ @Test
):/**
* @description:
* @author: laizhenghua
* @date: 2021/4/9 9:45
*/
@Slf4j
@SpringBootTest
public class RedisTest {
@Autowired
private StringRedisTemplate redisTemplate;
@Test
public void redisTest() {
ValueOperations<String, String> ops = redisTemplate.opsForValue();
ops.set("hello","hello redis");
String hello = ops.get("hello");
log.info("hello -> {}",hello);
}
}
@SpringBootTest
+ @RunWith(SpringTest.class)
)<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
@Autowired
、比如 @Transactional
标注测试方法,测试完成后自动回滚4.2、JUnit5常用注解
@Test
:表示方法是测试方法。但是与JUnit4的@Test不同,他的职责非常单一不能声明任何属性,拓展的测试将会由Jupiter提供额外测试@ParameterizedTest
:表示方法是参数化测试,下方会有详细介绍@RepeatedTest
:表示方法可重复执行,下方会有详细介绍@DisplayName
:为测试类或者测试方法设置展示名称@BeforeEach
:表示在每个单元测试之前执行@AfterEach
:表示在每个单元测试之后执行@BeforeAll
:表示在所有单元测试之前执行@AfterAll
:表示在所有单元测试之后执行@Tag
:表示单元测试类别,类似于JUnit4中的@Categories@Disabled
:表示测试类或测试方法不执行,类似于JUnit4中的@Ignore@Timeout
:表示测试方法运行如果超过了指定时间将会返回错误@ExtendWith
:为测试类或测试方法提供扩展类引用4.3、断言(assertions)
org.junit.jupiter.api.Assertions
的静态方法。JUnit 5 内置的断言可以分成如下几个类别:
方法
说明
assertEquals
判断两个对象或两个原始类型是否相等
assertNotEquals
判断两个对象或两个原始类型是否不相等
assertSame
判断两个对象引用是否指向同一个对象
assertNotSame
判断两个对象引用是否指向不同的对象
assertTrue
判断给定的布尔值是否为ture
assertFalse
判断给定的布尔值是否为false
assertNull
判断给定的对象引用是否为null
assertNotNull
判断给定的对象引用是否不为null
@Test
@DisplayName("simple assertion")
public void simple() {
assertEquals(3, 1 + 2, "simple math");
assertNotEquals(3, 1 + 1);
assertNotSame(new Object(), new Object());
Object obj = new Object();
assertSame(obj, obj);
assertFalse(1 > 2);
assertTrue(1 < 2);
assertNull(null);
assertNotNull(new Object());
}
@Test
@DisplayName("array assertion")
public void array() {
assertArrayEquals(new int[]{1, 2}, new int[] {1, 2});
}
org.junit.jupiter.api.Executable
函数式接口的实例作为要验证的断言,可以通过 lambda 表达式很容易的提供这些断言。@DisplayName("组合断言")
@Test
public void allTest() {
Assertions.assertAll("test",
() -> Assertions.assertEquals(1,1 + 2),
() -> Assertions.assertTrue(2 > 1));
}
Assertions.assertThrows()
,配合函数式编程就可以进行使用。@DisplayName("异常断言")
@Test
public void exceptionTest() {
Assertions.assertThrows(ArithmeticException.class,() -> {int a = 1/0;},"业务逻辑正常运行了");
}
@Test
@DisplayName("fail")
public void shouldFail() {
fail("This should fail");
}
5、指标监控
5.1、SpringBoot Actuator
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
官网地址:https://docs.spring.io/spring-boot/docs/2.3.9.RELEASE/reference/html/production-ready-features.html#production-ready
http://localhost:8080/actuator/**
health
、info
等端点),例如http://localhost:8080/actuator/health
# 官网都有详细介绍
management:
endpoints:
enabled-by-default: true #暴露所有端点信息
web:
exposure:
include: '*' #以web方式暴露
http://localhost:8080/actuator/beans
:查看当前容器中的所有组件
http://localhost:8080/actuator/configprops
:显示所有@ConfigurationProperties的配置
http://localhost:8080/actuator/metrics
:查看当前应用的指标列表
http://localhost:8080/actuator/metrics/jvm.gc.pause
:根据指标名字查看当前应用的具体指标信息
http://localhost:8080/actuator/endpointName/detailPath
5.2、Actuator Endpoint
ID
描述
auditevents
暴露当前应用程序的审核事件信息。需要一个
AuditEventRepository组件
。
beans
显示应用程序中所有Spring Bean的完整列表。
caches
暴露可用的缓存。
conditions
显示自动配置的所有条件信息,包括匹配或不匹配的原因。
configprops
显示所有
@ConfigurationProperties
。
env
暴露Spring的属性
ConfigurableEnvironment
flyway
显示已应用的所有Flyway数据库迁移。 需要一个或多个
Flyway
组件。
health
显示应用程序运行状况信息。
httptrace
显示HTTP跟踪信息(默认情况下,最近100个HTTP请求-响应)。需要一个
HttpTraceRepository
组件。
info
显示应用程序信息。
integrationgraph
显示Spring
integrationgraph
。需要依赖spring-integration-core
。
loggers
显示和修改应用程序中日志的配置。
liquibase
显示已应用的所有Liquibase数据库迁移。需要一个或多个
Liquibase
组件。
metrics
显示当前应用程序的“指标”信息。
mappings
显示所有
@RequestMapping
路径列表。
scheduledtasks
显示应用程序中的计划任务。
sessions
允许从Spring Session支持的会话存储中检索和删除用户会话。需要使用Spring Session的基于Servlet的Web应用程序。
shutdown
使应用程序正常关闭。默认禁用。
startup
显示由
ApplicationStartup
收集的启动步骤数据。需要使用SpringApplication
进行配置BufferingApplicationStartup
。
threaddump
执行线程转储。
ID
描述
heapdump
返回
hprof
堆转储文件。
jolokia
通过HTTP暴露JMX bean(需要引入Jolokia,不适用于WebFlux)。需要引入依赖
jolokia-core
。
logfile
返回日志文件的内容(如果已设置
logging.file.name
或logging.file.path
属性)。支持使用HTTPRange
标头来检索部分日志文件的内容。
prometheus
以Prometheus服务器可以抓取的格式公开指标。需要依赖
micrometer-registry-prometheus
。
Health Endpoint
可以为平台返回当前应用的一系列组件健康状况的集合。
汇总报告
http://localhost:8080/actuator/health
只响应了健康状态,并没有响应详细信息!所以我们还需要修改配置,让SpringBoot响应详细健康信息汇总报告。
application.ymlmanagement:
endpoints:
enabled-by-default: true
web:
exposure:
include: '*'
endpoint:
health:
show-details: always # 响应详细健康信息
http://localhost:8080/actuator/health
即可获得详细健康信息报告。
http://localhost:8080/actuator/metrics
根据名字二次请求:http://localhost:8080/actuator/metrics/jvm.gc.live.data.size
4、管理Endpoints
management.endpoint.
management:
endpoint:
beans:
enabled: true
management:
endpoints:
enabled-by-default: false
endpoint:
beans:
enabled: true
health:
enabled: true
5.3、定制 Endpoint
package com.laizhenghua.admin.health;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* @description: 定制 health 信息
* @author: laizhenghua
* @date: 2021/4/10 10:41
*/
@Component
public class MyComponentHealthIndicator extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
boolean isHealth = true;
Map<String,Object> map = new HashMap<>();
// 检查逻辑
// ...
if(isHealth) {
// builder.up(); // 表示健康
builder.status(Status.UP);
map.put("count",1);
map.put("connectTime","100ms");
}else {
// builder.down(); 不健康
builder.status(Status.OUT_OF_SERVICE);
map.put("error","连接超时");
map.put("connectTimeOut","3000ms");
}
builder.withDetail("code",500).withDetails(map);
}
}
http://localhost:8080/actuator/health
2、定制 info 信息
常用两种方式自定义 info 信息::
info:
appName: boot-admin
version: 2.0.1
mavenProjectName: @project.artifactId@ #使用@@可以获取maven的pom文件值
mavenProjectVersion: @project.version@
package com.laizhenghua.admin.acutuator.info;
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;
import java.util.Collections;
/**
* @description: 编写 InfoContributor
* @author: laizhenghua
* @date: 2021/4/10 18:52
*/
@Component
public class AppInfoInfoContributor implements InfoContributor {
@Override
public void contribute(Info.Builder builder) {
builder
.withDetail("message","hello world")
.withDetails(Collections.singletonMap("hello","SpringBoot"));
}
}
http://localhost:8080/actuator/info
会输出以上方式返回的所有info信息
server.tomcat.mbeanregistry.enabled
must be set to true
for all Tomcat metrics to be registered)class MyService{
Counter counter;
public MyService(MeterRegistry meterRegistry){
counter = meterRegistry.counter("myservice.method.running.counter");
}
public void hello() {
counter.increment();
}
}
//也可以使用下面的方式
@Bean
MeterBinder queueSize(Queue queue) {
return (registry) -> Gauge.builder("queueSize", queue::size).register(registry);
}
6、Profile 功能
6.1、application-profile功能
application-{env}.yaml
• 配置文件激活
• 命令行激活:java -jar xxx.jar --spring.profiles.active=prod --person.name=haha
spring:
profiles:
active: dev # 指定激活环境为 dev
spring:
profiles:
active: test # 指定激活环境为 test
# 指定激活环境
java -jar profile-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
# 指定配置属性
java -jar profile-0.0.1-SNAPSHOT.jar --server.port=8999
6.2、@Profile条件装配功能
@Profile("test") // 表示此组件在 test 环境上才能生效
@Component
@ConfigurationProperties("person")
@Data
public class Boss implements Person {
private String name;
private Integer age;
}
@Profile("dev")
@Component
@ConfigurationProperties("person")
@Data
public class Worker implements Person {
private String name;
private Integer age;
}
6.3、profile分组
spring.profiles.group.production[0]=proddb
spring.profiles.group.production[1]=prodmq
# 使用:--spring.profiles.active=production 激活
7、外部化配置
properties
文件,YAML
文件,环境变量和命令行参数来外化配置。使用@Value
注解,可以直接将属性值注入到你的beans中,并通过Spring的Environment抽象或绑定到结构化对象来访问。
/config
子目录的直接子目录
8、自定义starter
8.1、starter启动原理
autoconfigure包
中配置使用 META-INF/spring.factories
中 EnableAutoConfiguration 的值,使得项目启动加载指定的自动配置类
@Conditional
@EnableConfigurationProperties
@Bean
…8.2、自定义starter
@Configuration // 指定这个类是一个配置类
@ConditionalOnXXX // 在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter // 指定自动配置类的顺序
@Bean //给容器中添加组件
@ConfigurationPropertie // 结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties // 让xxxProperties生效加入到容器中
/*
自动配置类要能加载
将需要启动就加载的自动配置类,配置在META-INF/spring.factories
*/
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
howie-spring-boot-starter
,不需要有任何源代码,未来给别人使用的。
得到一个 pom.xml 文件:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.howiegroupId>
<artifactId>howie-spring-boot-starterartifactId>
<version>1.0-SNAPSHOTversion>
project>
howie-spring-boot-starter-autoconfigure
。无需选中任何开发场景。
自动配置模块编写的详细内容,请看步骤4。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.howiegroupId>
<artifactId>howie-spring-boot-starterartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>com.howiegroupId>
<artifactId>howie-spring-boot-starter-autoconfigureartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
dependencies>
project>
sayHello
服务:
HelloServiceAutoConfiguration.java/**
* @description: 自动配置类
* @author: laizhenghua
* @date: 2021/4/11 9:55
*/
@Configuration
@EnableConfigurationProperties(HelloProperties.class) // 除了会绑定配置类以外还会将 HelloProperties 类型的组件放在容器中
public class HelloServiceAutoConfiguration {
@Bean
@ConditionalOnMissingBean(HelloService.class)
public HelloService helloService() {
return new HelloService();
}
}
/**
* @description: 属性配置类
* @author: laizhenghua
* @date: 2021/4/11 9:50
*/
@ConfigurationProperties("howie.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
/**
* @description: 默认不要放在组件中
* @author: laizhenghua
* @date: 2021/4/11 9:46
*/
public class HelloService {
@Autowired
private HelloProperties helloProperties;
public String sayHello(String name) {
return helloProperties.getPrefix() + " : " + name + " : " + helloProperties.getSuffix();
}
}
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.howie.autoconfigure.auto.HelloServiceAutoConfiguration
<dependency>
<groupId>com.howiegroupId>
<artifactId>howie-spring-boot-starterartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
引入依赖后,我们修改SpringBoot全局配置文件,指定sayHello
服务的响应的前缀与后缀。注意是在其他测试项目中。howie:
hello:
prefix: '这里是对抗路'
suffix: '很高兴您的到来'
/**
* @description:
* @author: laizhenghua
* @date: 2021/4/10 20:03
*/
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/sayHello/{username}")
public String sayHello(@PathVariable("username") String name) {
return helloService.sayHello(name);
}
}
ok 完美!需要注意的是如果我们在容器中注入了自己的HelloService
类型的组件,就能覆盖我们之前自己编写的sayHello
服务。9、SpringBoot启动配置原理
9.1、SpringBoot启动过程
/**
* Static helper that can be used to run a {@link SpringApplication} from the
* specified sources using default settings and user supplied arguments.
* @param primarySources the primary sources to load
* @param args the application arguments (usually passed from a Java main method)
* @return the running {@link ApplicationContext}
*/
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
// 创建 SpringApplication对象 并 调用run()方法
return new SpringApplication(primarySources).run(args);
}
1、创建SpringApplication对象
// 创建SpringApplication对象时的构造器
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
// 给 SpringApplication对象保存一些基础信息
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
// 使用ClassUtils抽象类判定当前应用的类型 reactive ? servlet ?
this.webApplicationType = WebApplicationType.deduceFromClasspath(); // 判定当前应用是 servlet应用
// bootstrappers是初始启动引导器(List
SpringApplication对象的应用监听器:
2、运行run方法
/**
* Run the Spring application, creating and refreshing a new
* {@link ApplicationContext}.
* @param args the application arguments (usually passed from a Java main method)
* @return a running {@link ApplicationContext}
*/
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start(); // 记录应用的启动时间
// 创建引导上下文(Context环境)createBootstrapContext()。
// 作用:获取到所有之前的 bootstrappers 挨个执行 intitialize() 来完成对引导启动器上下文环境设置
DefaultBootstrapContext bootstrapContext = createBootstrapContext();
ConfigurableApplicationContext context = null;
// 让当前应用进入headless模式。java.awt.headless
configureHeadlessProperty();
// 获取所有 RunListener(运行监听器)【为了方便所有Listener进行事件感知】
// 作用1:getSpringFactoriesInstances 去spring.factories找 SpringApplicationRunListener
SpringApplicationRunListeners listeners = getRunListeners(args);
// 回调所有的获取SpringApplicationRunListener.starting()方法。相当于通知所有感兴趣系统正在启动过程的人,项目正在 starting。
listeners.starting(bootstrapContext, this.mainApplicationClass);
try {
// 封装命令行参数
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 准备环境 prepareEnvironment()
// 作用:返回或者创建基础环境信息对象。StandardServletEnvironment
ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
/*
同时在放里面:
1.配置了环境信息信息对象(读取所有的配置源的配置属性值)
2.绑定环境信息
3.监听器调用 listener.environmentPrepared(); 通知所有的监听器当前环境准备完成
*/
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment); // 控制台打印 Banner 信息
// 创建IOC容器(createApplicationContext())
context = createApplicationContext();
/*
1.根据项目类型(Servlet)创建容器
2.当前会创建 AnnotationConfigServletWebServerApplicationContext
*/
context.setApplicationStartup(this.applicationStartup);
// 准备ApplicationContext IOC容器的基本信息 prepareContext()
prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
/*
1.保存环境信息
2.IOC容器的后置处理流程。
3.应用初始化器;applyInitializers(context);
1.遍历所有的 ApplicationContextInitializer 调用 initialize.。来对ioc容器进行初始化扩展功能
2.遍历所有的 listener 调用 contextPrepared EventPublishRunListenr;通知所有的监听器contextPrepared
4.所有的监听器 调用 contextLoaded。通知所有的监听器 contextLoaded;
*/
// 刷新IOC容器。refreshContext。(创建容器中的所有组件(Spring注解))
refreshContext(context);
// 容器刷新完成后工作?afterRefresh
afterRefresh(context, applicationArguments);
stopWatch.stop(); // 记录应用启动成功时间
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
// 所有监听器 调用 listeners.started(context); 通知所有的监听器 started
listeners.started(context);
// 调用所有runners;callRunners()
callRunners(context, applicationArguments);
/*
1.获取容器中的 ApplicationRunner
2.获取容器中的 CommandLineRunner
3.合并所有runner并且按照@Order进行排序
4.遍历所有的runner。调用 run 方法
*/
}
catch (Throwable ex) {
// 如果以上有异常,调用Listener 的 failed
handleRunFailure(context, ex, listeners);
throw new IllegalStateException(ex);
}
try {
// 调用所有监听器的 running 方法 listeners.running(context); 通知所有的监听器 running
listeners.running(context);
}
catch (Throwable ex) {
// running如果有问题。继续通知 failed (调用所有 Listener 的 failed;通知所有的监听器 failed)
handleRunFailure(context, ex, null);
throw new IllegalStateException(ex);
}
return context;
}
public interface Bootstrapper {
/**
* Initialize the given {@link BootstrapRegistry} with any required registrations.
* @param registry the registry to initialize
* @since 2.4.4
*/
default void initialize(BootstrapRegistry registry) {
intitialize(registry);
}
/**
* Initialize the given {@link BootstrapRegistry} with any required registrations.
* @param registry the registry to initialize
* @deprecated since 2.4.4 in favor of
* {@link Bootstrapper#initialize(BootstrapRegistry)}
*/
@Deprecated
void intitialize(BootstrapRegistry registry);
}
9.2、Application Events and Listeners
META-INF/spring.factories
:
End
Thank you for watching
End