Spring5-新特性
2023-02-19 12:21:14 时间
前言
今天开始学习我自己总结的 Java-学习路线 中的《Spring5-新特性》,小简从 0 开始学 Java 知识,并不定期更新所学笔记,期待一年后的蜕变吧!
Spring5 简介
- Spring5 框架的代码基于Java8,运行时兼容 JDK9,许多不建议使用的类和方法在代码库中删除
- Spring5 框架自带了通用的日志封装 Log4j2
- Spring5 已经移除Log4jConfigListener,官方建议使用 Log4j2
- Spring5 框架整合Log4j2
- Spring5 框架核心容器支持 @Nullable 注解
- Spring5 框架支持整合 JUnit5
- Spring5 核心容器支持函数式风格 GenericApplicationContext
- Spring5 框架新功能 Webflux
通用的日志封装
1、引入jar包
Log4j2下载地址:Download Apache Log4j 2
slf4j下载地址:org/slf4j (maven.org)
slf4j-api-1.7.36.jar
log4j-api-2.17.2.jar
log4j-core-2.17.2.jar
log4j-slf4j-impl-2.17.2.jar
2、创建log4j2.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--Configuration 后面的 status 用于设置 log4j2 自身内部的信息输出,可以不设置,当设置成 trace 时,可以看到 log4j2 内部各种详细输出-->
<configuration status="INFO">
<!--先定义所有的 appender-->
<appenders>
<!--输出日志信息到控制台-->
<console name="Console" target="SYSTEM_OUT">
<!--控制日志输出的格式-->
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</console>
</appenders>
<!--然后定义 logger,只有定义 logger 并引入的 appender,appender 才会生效-->
<!--root:用于指定项目的根日志,如果没有单独指定 Logger,则会使用 root 作为默认的日志输出-->
<loggers>
<root level="info">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>
@Nullable 注解
@Nullable 注解可以使用在方法上面,属性上面,参数上面,表示方法返回可以为空,属性值可以为空,参数值可以为空
- 注解用在方法上面,方法返回值可以为空
@Nullable
String getId()
- 注解使用在方法参数里面,方法参数可以为空
public ClassPathXmlApplicationContext(String[] configLocations, @Nullable ApplicationContext parent) throws BeansException {
this(configLocations, true, parent);
}
- 注解使用在属性上面,属性值可以为空
@Nullable
private Resource[] configResources;
函数式注册对象
// 函数式风格创建对象,交给Spring进行管理
@Test
public void testGenericApplicationContext() {
//1 创建 GenericApplicationContext 对象
GenericApplicationContext context = new GenericApplicationContext();
//2 调用 context 的方法对象注册
// refresh把内容清空进行注册
context.refresh();
context.registerBean(User.class, () -> new User());
context.registerBean("user1", User.class, () -> new User());
// 获取在Spring里注册的对象
Object user = context.getBean("com.jwt.aop.User");
Object user1 = context.getBean("user1");
System.out.println(user);
System.out.println(user1);
}
/**
com.jwt.aop.User@3712b94
com.jwt.aop.User@2833cc44
**/
支持JUnit 5
JUnit4
1、引入依赖
hamcrest-core-1.3.jar //JUnit4
junit-4.12.jar //JUnit4
spring-test-5.3.18.jar //单元测试注解
2、创建测试类,使用注解方式实现
@RunWith(SpringJUnit4ClassRunner.class) //单元测试框架
@ContextConfiguration("classpath:jdbc.xml") //加载配置文件
public class JTest4 {
@Autowired
private UserService userService;
@Test
public void test() {
userService.accountMoney();
}
}
可以代替下面的测试方法,简化代码
@Test
public void testTransaction() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("jdbc.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.accountMoney();
}
JUnit5
1、引入JUnit5的jar包
2、创建测试类,使用注解方式实现
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:jdbc.xml")
public class JTest5 {
@Autowired
private UserService userService;
@Test
public void testJunit5() {
userService.add();
}
}
使用一个复合注解替代上面两个注解完成整合
@SpringJUnitConfig(locations = "classpath:jdbc.xml")
public class JTest5 {
@Autowired
private UserService userService;
@Test
public void testJunit5() {
userService.add();
}
}
Webflux
学完 SpringMVC 再学
相关文章
- VUE 前端文本输出为超文本
- K8s系列-KubeSphere
- k8s系列-helm
- helm 勘误
- 普罗米修斯
- 2022-12-23:portainer是docker的web可视化工具。如果根据docker部署去写yaml,默认local是k8s,而不是docker,这不
- R语言GARCH族模型:正态分布、t、GED分布EGARCH、TGARCH的VaR分析股票指数
- R语言随机森林RandomForest、逻辑回归Logisitc预测心脏病数据和可视化分析|附代码数据
- 【视频】主成分分析PCA降维方法和R语言分析葡萄酒可视化实例|数据分享|附代码数据
- 系列文章| R语言分布滞后线性和非线性模型DLM和DLNM建模应用|附代码数据
- 系列文章| R语言分布滞后线性和非线性模型DLM和DLNM建模应用|附代码数据
- R语言JAGS贝叶斯回归模型分析博士生延期毕业完成论文时间|附代码数据
- Unity下如何实现RTMP或RTSP流播放和录制
- 图片懒加载之判断图片进入可视区域
- 【AJAX】对于AJAX 解析已经XMLHttpRequest对象对于异步的操作等等在项目中的体验
- SAP UI5 Smart Table 和 Smart Filter Bar 的联合使用方法介绍
- SAP Smart Table 的 Personalization(个性化配置)试读版
- 内科大深度学习期末复习笔记
- 【数据预处理】基于Pandas的数据预处理技术【前七个任务】
- SpringBoot:模块探究之spring-boot-devtools