06-Spring5 新特性


从这章开始才是重点,前面的最多算是复习

运行时环境

整个Spring5框架的代码基于Java8,运行时兼容JDK9,许多不建议使用的类和方法在代码库中删除

日志封装

Spring5.0框架自带了通用的日志封装

  1. Spring5已经移除了Log4jConfigListener, 官方建议使用Log4j2

Spring框架整合Log4J2

引入JAR包

创建log4j.xml配置文件

在src下新建

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



    
    
        
        
            
            
        
    
    
    
    
        
            
        
    

编写测试类

package com.dance.spring.learn.testdemo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestLog {

    public static final Logger log = LoggerFactory.getLogger(TestLog.class);

    public static void main(String[] args) {

        log.info("test logger.....");

    }

}

执行结果

2021-12-12 17:17:17.551 [main] INFO  com.dance.spring.learn.testdemo.TestLog - test logger.....

支持@Nullable注解

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

  1. @Nullable注解可以使用在方法上面,属性上面,参数上面,表示方法返回可以为空,属性值可以为空,参数值可以为空

方法上添加

返回值可以为空

方法参数列表添加

方法参数可以为空

属性上添加

属性值可以为空

 

我不是很理解,感觉这个@Nullable没啥用,应为本来就是可以为空的,但是在我看了一篇文章后,才发现也挺好用的

就是这个大大写的,对于一些入参来说,可以很好的标识,该字段是否可以为空,有助于减少空指针异常等代码的编写,别人看的时候也很容易理解

函数式风格GenericApplicationContext

函数式风格创建对象,交给Spring容器进行管理

编写测试类

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

执行结果

User{userId='null', userName='null', ustatus='null'}

Spring5整合Junit5

整合Junit4

引入依赖

编写测试类

package com.dance.spring.learn.testdemo;

import com.dance.spring.learn.jdbc.config.SpringJdbcConfig;
import com.dance.spring.learn.jdbc.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
// 使用value 指定xml也可以使用classes指定配置类
@ContextConfiguration(classes = SpringJdbcConfig.class)
public class JTest4 {

    @Autowired
    private UserService userService;

    @Test
    public void test(){
        userService.accountMoney();
    }

}

执行结果

十二月 12, 2021 5:43:57 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
十二月 12, 2021 5:43:57 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@4b952a2d, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@3159c4b8, org.springframework.test.context.support.DirtiesContextTestExecutionListener@73846619, org.springframework.test.context.transaction.TransactionalTestExecutionListener@4bec1f0c, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@29ca901e, org.springframework.test.context.event.EventPublishingTestExecutionListener@5649fd9b]
2021-12-12 17:43:58.847 [main] INFO  com.alibaba.druid.pool.DruidDataSource - {dataSource-1} inited
转账完成,flower 账户支出100元

整合Junit5

引入依赖

编写测试类

package com.dance.spring.learn.testdemo;

import com.dance.spring.learn.jdbc.config.SpringJdbcConfig;
import com.dance.spring.learn.jdbc.service.UserService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = SpringJdbcConfig.class)
public class JTest5 {

    @Autowired
    private UserService userService;
    
    @Test
    public void test(){
        userService.accountMoney();
    }

}

执行结果

2021-12-12 17:49:28.230 [main] INFO  com.alibaba.druid.pool.DruidDataSource - {dataSource-1} inited
转账完成,flower 账户支出100元

优化

使用一个注解替换两个注解

//@ExtendWith(SpringExtension.class)
//@ContextConfiguration(classes = SpringJdbcConfig.class)
@SpringJUnitConfig(classes = SpringJdbcConfig.class)

执行结果也是一样的

完结撒花花,下一章开始WebFlux

 若有收获,就点个赞吧  

作者:彼岸舞

时间:2021\12\13

内容关于:Java

本文属于作者原创,未经允许,禁止转发