Spring5框架新功能

一、

1、整个Spring5框架基于java8,运行时兼容java9,许多不建议使用的方法在代码库中被删除

2、Spring5自带通用的日志封装

(1)Spring5已经移除了log4jConfigListener,官方建议使用log4j2

(2)Spring5框架整合log4j2

第一步引入jar包

Spring5框架新功能

第二步创建log4j2.xml

xml version="1.0" encoding="UTF-8"?>

<configuration status="INFO">

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

    <loggers>
        <root level="info">
            <appender-ref ref="Console"/>
        root>
    loggers>
configuration>

3、Spring5框架核心容器支持@Nullable注解

(1)@Nullable注解可以使用在方法上面,属性上面,参数上面,表示方法返回可以为空,属性值可以为空,参数值可以为空
(2)注解用在方法上面,方法返回值可以为空
(3)注解使用在方法参数里面,方法参数可以为空
(4)注解使用在属性上面,属性值可以为空
4、Spring5核心容器支持函数式风格GenericApplicationContext //函数式风格创建对象,交给spring进行管理

@Test
public void testGenericApplicationContext() {
//1 创建GenericApplicationContext对象
 GenericApplicationContext context = new nericApplicationContext(); //2 调用context的方法对象注册
context.refresh();
context.registerBean("user1",User.class,() -> new User());
 //3 获取在spring注册的对象 // User user = (User)context.getBean("com.atguigu.spring5.test.User");
User user = (User)context.getBean("user1"); System.out.println(user); }

5、Spring5支持整合JUnit5
(1)整合JUnit4
第一步 引入Spring相关针对测试依赖
第二步 创建测试类,使用注解方式完成

@RunWith(SpringJUnit4ClassRunner.class) //单元测试框架 @ContextConfiguration("classpath:bean1.xml") //加载配置文件
public class JTest4 {
@Autowired private UserService userService;
@Test public void test1() {
userService.accountMoney();
 }
 }

(2)Spring5整合JUnit5
第一步 引入JUnit5的jar包
第二步 创建测试类,使用注解完成

@ExtendWith(SpringExtension.class) @ContextConfiguration("classpath:bean1.xml")
public class JTest5 {
 @Autowired
private UserService userService;
 @Test
public void test1() {
 userService.accountMoney();
 }
}(3)使用一个复合注解替代上面两个注解完成整合 @SpringJUnitConfig(locations = "classpath:bean1.xml")public class JTest5 {    @Autowired   private UserService userService;    @Test    public void test1() {         userService.accountMoney();    } }

后续的新特性基于Spring MVC 、Spring Boot 、学完后再回来添加(^_^)!

Original: https://www.cnblogs.com/wuzkkk/p/15921397.html
Author: 在厂的上进者
Title: Spring5框架新功能

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/579682/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球