Spring5新功能
Spring5框架新功能
- 代码整体基于 Java8,运行时兼容 Java9,代码库中删除了许多不建议使用的类和方法
- 自带了通用的日志封装:移除了
Log4jConfigListener
,官方建议使用Log4j2
- 核心容器支持
@Nullable
注解 - 核心容器支持函数式风格
GenericApplicationContext
- 支持整合
Junit 5
单元测试
整合日志框架
1)引入
jar
包1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22<!--注意2.17.1以下的版本会有远程代码执行的安全漏洞,
具体参考Apache官方文档(https://logging.apache.org/log4j/2.x/security.html -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
- 2)创建
log4j2.xml
配置文件,配置日志相关信息
1 |
|
随便一段代码,输入以下日志
1 | 2022-03-11 21:38:41.342 [main] INFO com.alibaba.druid.pool.DruidDataSource - {dataSource-1} inited |
如果将log4j2.xml
配置文件级别从INFO
改成DEBUG
,则会打印更多的底层调用信息
1 | 2022-03-11 21:40:41,054 main DEBUG Apache Log4j Core 2.17.2 initializing configuration XmlConfiguration[location=D:\workspace\NOTE_Spring\Spring5_HelloWorld\out\production\Spring5_HelloWorld\log4j2.xml] |
上述配置都是自动进行日志输出,那如何手动进行日志输出呢?
这里需要用到org.slf4j.LoggerFactory
中getLogger()
方法,并将当前类型作为参数传入,就可以获取到一个org.slf4j.Logger
对象
其中可以调用的日志级别有5种:trace
、debug
、info
、warn
、error
,分别对应方法
logger.trace
logger.debug
logger.info
logger.warn
logger.error
测试代码,这里以常用的info
、warn
和error
级别为例
1 | public class TestLog { |
测试结果
1 | 2022-03-11 22:01:56.784 [main] INFO com.vectorx.spring5.s19_log4j2.TestLog - This is info log test. |
@Nullable注解
@Nullable
注解可以用在方法、属性和参数上面,分别表示方法可以为空、属性可以为空、参数可以为空
- 1)
@Nullable
注解作用在方法上,方法返回值可以为空
1 |
|
- 2)
@Nullable
注解作用在属性上,属性值可以为空
1 |
|
- 3)
@Nullable
注解作用在参数上,参数值可以为空
1 | public <T> void registerBean( { String beanName, Class<T> beanClass, Supplier<T> supplier, BeanDefinitionCustomizer... customizers) |
函数式风格
函数式风格创建对象,交给 Spring 进行管理
1 | //1、创建GenericApplicationContext对象 |
运行结果:抛出了NoSuchBeanDefinitionException
异常,No bean named 'user' available
表明Spring容器中没有名为user
的对象
1 | org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'user' available |
这是为什么呢?因为我们没有指定被注册对象的name
,不像注解式编程中 Spring 会帮我们指定一个以类名首字母小写的name
1 | //1、创建GenericApplicationContext对象 |
运行结果
1 | com.vectorx.spring5.s20_functional.User@4d95d2a2 |
还有一种方式,不在注册的时候指定name
,而是获取的时候指明类的全路径,即使用类的全限定名
1 | //1、创建GenericApplicationContext对象 |
运行结果
1 | com.vectorx.spring5.s20_functional.User@4d95d2a2 |
整合Junit
整合Junit4
1)引入相关的
jar
包:spring-test
和junit4
1
2
3
4
5
6
7
8
9
10
11<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
- 2)创建测试类,使用注解方式完成:
@RunWith
、@ContextConfiguration
和@org.junit.Test
1 | //单元测试框架 |
整合Junit5
- 1)引入相关的
jar
包
- 2)创建测试类,使用注解完成:
@ExtendWith
、@ContextConfiguration
和@org.junit.jupiter.api.Test
1 | //单元测试框架 |
不过 Spring 中提供了一个更为简单的方式可以简化代码
- 4)使用一个复合注解代替上述两个注解完成整合:
@SpringJUnitConfig
1 | //使用复合注解简化写法 |
未完待续
课程中还介绍了SpringWebFlux
模块,由于该知识点需要诸如SpringMVC
、SpringBoot
等前置知识,所以此章节暂时先告一段落…
总结
Spring5 框架的新功能:
支持整合日志框架
log4j2
- 自动记录日志:配置
log4j2.xml
- 手动记录日志:
LoggerFactory.getLogger
- 自动记录日志:配置
支持
@Nullable
注解支持函数式风格
支持整合
Junit
Junit4
:@RunWith
、@ContextConfiguration
、@org.junit.Test
Junit5
:@ExtendWith
、@ContextConfiguration
、@org.junit.jupiter.api.Test
- 复合注解:
@SpringJUnitConfig
导图仅供参考
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 凉月の博客!
评论