Spring5.0新特性(日志封装、@Nullable注解、函数式风格 、整合JUnit5、Webflux)

本文介绍了Spring5框架对日志的改进,推荐使用Log4j2,并提供了配置和测试步骤。同时,讲解了@Nullable注解的使用场景。此外,展示了Spring5核心容器中的函数式风格创建对象,并演示了如何整合JUnit5进行测试。
摘要由CSDN通过智能技术生成

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

日志封装

Spring 5.0 框架自带了通用的日志封装
(1)Spring5 已经移除 Log4jConfigListener,官方建议使用 Log4j2
(2)Spring5 框架整合 Log4j2

第一步 引入依赖

    <!--  日志相关-->
    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.13.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-slf4j-impl</artifactId>
      <version>2.13.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.25</version>
    </dependency>

第二步 创建 log4j2.xml 配置文件(固定的,直接用)

日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL
显示内容从all到off递减,默认为info

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

第三步 简单测试

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

public class UserLog {
    private static final Logger log = LoggerFactory.getLogger(UserLog.class);

    public static void main(String[] args) {
        log.info("hello log4j2");
        log.warn("hello log4j2");
    }
}

@Nullable注解

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

(2)注解用在方法上面,方法返回值可以为空
在这里插入图片描述
(3)注解使用在属性上面,属性值可以为空
在这里插入图片描述
(4)注解使用在方法参数里面,方法参数可以为空
在这里插入图片描述

Spring5 核心容器支持函数式风格

比如:GenericApplicationContext

    @Test
    //函数式风格创建对象,交给 spring 进行管理
    public void test9(){
        //1 创建 GenericApplicationContext 对象
        GenericApplicationContext context = new GenericApplicationContext();
        //2 调用 context 的方法对象注册
        context.refresh();
        context.registerBean("user1",User.class,()->new User());
        //3 获取在 spring 注册的对象
        //如果上面不写user1
        // User user = (User)context.getBean("com.spring5.test.User");
        User user1 = (User) context.getBean("user1");
        System.out.println(user1);
    }

整合JUnit5

整合JUnit4

第一步 引入 Spring 相关针对测试依赖

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>compile</scope>
    </dependency>

第二步 通过@RunWith和@ContextConfiguration搭配使用,达到整合JUnit4

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:bean1.xml")
public class TestDemo1 {
    @Autowired
    UserService userService;
    public void test(){
        userService.add();
    }
}

原来的代码是这样的:

    @Test
    public void test1(){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = applicationContext.getBean("userService", UserService.class);
        userService.add();
    }

整合JUnit5

第一步 引入 JUnit5 的 jar 包
在这里插入图片描述
第二步 创建测试类,使用注解完成

@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:bean1.xml")
public class TestDemo1 {
    @Autowired
    UserService userService;
    @Test
    public void test(){
        userService.add();
    }
}

使用一个复合注解替代上面两个注解完成整合

@SpringJUnitConfig(locations = "classpath:bean1.xml")
public class TestDemo1 {
    @Autowired
    UserService userService;
    @Test
    public void test(){
        userService.add();
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值