1:spring5中文文档

2:Spring框架的新功能

Spring 5新功能和增强功能
(1):整个spring5框架代码基于java1.8,运行时兼容jdk1.9,许多不建议使用的类和方法,在代码库中进行删除;
(2):spring5框架自带了通用的日志封装
spring5已经移出log4ConfigListenter,官网建议使用log4j2,spring5已经整合了log4j2,如果要使用log4j则需要将spring版本降低到4

3:spring5整合log4j2

(1):引入jar包

image.png

(2):创建log4j2.xml

log4j2配置文件详解log4j2配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration status="INFO">
  3. <Appenders>
  4. <Console name="Console" target="SYSTEM_OUT">
  5. <PatternLayout pattern="%d{yyyy年MM月dd日 HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  6. </Console>
  7. </Appenders>
  8. <Loggers>
  9. <Root level="info">
  10. <AppenderRef ref="Console"/>
  11. </Root>
  12. </Loggers>
  13. </Configuration>

image.png

(3):自定义日志内容输出

image.png4:Nullable注解和函数式注册对象

1:@Nullable 注解

(1):spring5框架核心容器支持@Nullable 注解可以使用在方法上,属性上, 参数上,使用该注解之后的返回值可以为空
(2):注解使用在方法上,方法返回值可以为空
image.png
(3):注解使用在方法参数中,改方法的参数可以为空
image.png
(4):注解使用在属性上,属性可以为空;
image.png

2:函数式风格

(1):spring5核心容器支持函数式风格GenericApplicationContext
(2):函数式创建对象
image.png
image.png
测试:因为现在注册的不是跟之前的方式一样是类名的小写,所以找不到对象image.png
解决方法1:通过类的全路径进行获取创建对象
image.png
解决方法2:通过设置 registerBean中参数进行自定义对象名称,改名称可以为空也可以自定义
image.png

5:整合JUnit5单元测试框架

1:整合junit4测试

(1):引入spring相关测试依赖jar
image.png
(2):创建测试类,使用注解完成测试

  1. - [@RunWith作用](https://blog.csdn.net/weixin_43671497/article/details/90543225#RunWith_2)
  2. - ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1712076/1640783441770-f8fa81a0-6211-46e2-9678-bdc541451ef1.png#clientId=u7ed192ab-f0ad-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=297&id=u97201b1f&margin=%5Bobject%20Object%5D&name=image.png&originHeight=297&originWidth=945&originalType=binary&ratio=1&rotation=0&showTitle=false&size=23471&status=done&style=none&taskId=uf44fdf41-eb2d-4e5c-bd66-77237019590&title=&width=945)
  3. - ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1712076/1640783577656-c0e8024a-4cc5-44b9-9fe5-f51b560f253d.png#clientId=u7ed192ab-f0ad-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=386&id=ue423566d&margin=%5Bobject%20Object%5D&name=image.png&originHeight=386&originWidth=1347&originalType=binary&ratio=1&rotation=0&showTitle=false&size=54054&status=done&style=none&taskId=ue82f7d62-e67e-401f-b96c-15ec911744e&title=&width=1347)
  1. package com.junjay.spring5.test;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.test.context.ContextConfiguration;
  6. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  7. import com.junjay.spring5.service.UserService;
  8. /**
  9. * 整合Junit4测试框架
  10. *
  11. */
  12. // @RunWith:运行器 SpringJUnit4ClassRunner指用JUnit4来运行
  13. @RunWith(SpringJUnit4ClassRunner.class)
  14. // 加载配置文件
  15. @ContextConfiguration("classpath:bean1.xml")
  16. public class JTest4 {
  17. @Autowired
  18. private UserService userService;
  19. @Test
  20. public void test1() {
  21. // 参数1:转账人 参数2:转账金额 参数3:接收转账人
  22. userService.transfer("张三", 100, "李四");
  23. }
  24. }

image.png

2:整合junit5框架测试

1:引入junit5 jar
image.png
测试:

  1. package com.junjay.spring5.test;
  2. import org.junit.jupiter.api.Test;
  3. import org.junit.jupiter.api.extension.ExtendWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.test.context.ContextConfiguration;
  6. import org.springframework.test.context.junit.jupiter.SpringExtension;
  7. import com.junjay.spring5.service.UserService;
  8. /**
  9. * 整合Junit5测试框架
  10. *
  11. */
  12. @ExtendWith(SpringExtension.class)
  13. @ContextConfiguration("classpath:bean1.xml")
  14. public class JTest5 {
  15. @Autowired
  16. private UserService userService;
  17. @Test
  18. public void test1() {
  19. // 参数1:转账人 参数2:转账金额 参数3:接收转账人
  20. userService.transfer("张三", 100, "李四");
  21. }
  22. }

image.png

3:junit4和junit5对比

image.png

4:junit4和junit5复合

@SpringJUnitConfig(locations = “classpath:bean1.xml”) 一个复合注解做到指定版本和加载配置文件

  1. package com.junjay.spring5.test;
  2. import org.junit.jupiter.api.Test;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
  5. import com.junjay.spring5.service.UserService;
  6. /**
  7. * 复合Junit5和Junit4 测试框架
  8. *
  9. */
  10. @SpringJUnitConfig(locations = "classpath:bean1.xml")
  11. public class JTest {
  12. @Autowired
  13. private UserService userService;
  14. @Test
  15. public void test1() {
  16. // 参数1:转账人 参数2:转账金额 参数3:接收转账人
  17. userService.transfer("张三", 100, "李四");
  18. }
  19. }


git@github.com:My-Jun/spring5_demo6.git