Spring5-新功能

本文介绍了如何在Spring5中整合Log4j2日志框架,包括引入依赖和配置日志文件。接着讲解了@Nullable注解的使用场景,如方法、属性和参数上。然后展示了Spring5中通过函数式风格的GenericApplicationContext注册和管理Bean。最后对比了JUnit4和JUnit5单元测试框架的使用,包括测试类的创建和注解配置。
摘要由CSDN通过智能技术生成

整合日志框架

Spring 5.0 框架自带了通用的日志封装

(1)Spring5 已经移除 Log4jConfigListener,官方建议使用 Log4j2

(2)Spring5 框架整合 Log4j2

引入依赖

<!--整合log4j2日志-->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.25</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

创建 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 注解可以使用在方法上面,属性上面,参数上面,表示方法返回可以为空,属性值可以为空,参数值可以为空

注解用在方法上面,方法返回值可以为空

image-20210515221114333

注解使用在方法参数里面,方法参数可以为空

image-20210515221138335

注解使用在属性上面,属性值可以为空

image-20210515221207168

函数式注册对象

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

// 函数式风格创建对象,交给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.edu.spring.newfunction.User");
    Object user1 = context.getBean("user1");
    System.out.println(user);
    System.out.println(user1);
}

image-20210515222247583

单元测试框架

JUnit4

引入依赖

<!--junit4相关依赖-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.3.2</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

创建测试类,使用注解方式实现

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

image-20210515223701478

JUnit5

引入依赖

<!--junit5相关依赖-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.3.2</version>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.7.0</version>
</dependency>

创建测试类,使用注解完成

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

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

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

image-20210515225055735

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值