14、Spring5 框架新功能-1

  • 整个 Spring5 框架的代码基于 Java8,运行时兼容 JDK9,许多不建议使用的类和方法在代码库删除
  • Spring5 框架自带了通用的日志封装
    • Spring5 已经移除 Log4jConfigListener,官方建议使用 Log4j2
    • Spring5 框架整合 Log4j2

1、Spring5整合Log4j2

pom.xml

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.19.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-slf4j2-impl</artifactId>
      <version>2.19.0</version>
    </dependency>

log4j2.xml (必须是这个名称)

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

<!--日志级别,从低到高的优先级:ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF-->
<!--configuration 后面的 status 用于设置 log4j2 自身内部的信息输出, 可以不设置,
当设置成 trace 时,可以看到 log4j2 内部各种详细输出-->
<configuration status="DEBUG">

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

    <appenders>
        <!--输出日志信息到控制台-->
        <console name="Console" target="SYSTEM_OUT">
            <!--控制日志输出的格式-->
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss SSS} [%t] %-3level %logger{1024} - %msg%n"/>
        </console>
    </appenders>

</configuration>

测试

    @Test
    public void test() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springBean.xml");
        A a = applicationContext.getBean("a", A.class);
    }
@Data
public class A {
    private static Logger log = LoggerFactory.getLogger(A.class);
    public A() {
        System.out.println("A");
        log.info("hello ");
    }
}

image-20221116211015355

2、Nullable注解和函数式注册对象

2.1、@Nullable

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

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

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

      @Nullable
      String getId();
      
    • 注解使用在方法参数里面,方法参数可以为空

      public <T> void registerBean
          (@Nullable String beanName, Class<T> beanClass, @Nullable Supplier<T> supplier, BeanDefinitionCustomizer... customizers) {
              this.reader.registerBean(beanClass, beanName, supplier, customizers);
      }
      
    • 注解使用在属性上面,属性值可以为空

      @Nullable
      private String id;
      

2.2、函数式注册对象

  • Spring5 框架核心容器支持函数式风格:GenericApplicationContext
  1. context.registerBean(A.class, () -> new A());
    //函数式风格创建对象,交给spring进行管理
    @Test
    public void testGenericApplicationContext() {
        //1.创建GenericApplicationContext对象
        GenericApplicationContext context = new GenericApplicationContext();
        //2.调用context的方法对象注册,表示内容清空,里面进行注册
        context.refresh();
        context.registerBean(A.class, () -> new A());
        //3.获取在spring注册的对象。类的全名
        A a = context.getBean("com.cjf.bean.A", A.class);
        System.out.println(a);
    }

  1. context.registerBean(“a1”,A.class, () -> new A());
//函数式风格创建对象,交给spring进行管理
@Test
public void testGenericApplicationContext() {
    //1.创建GenericApplicationContext对象
    GenericApplicationContext context = new GenericApplicationContext();
    //2.调用context的方法对象注册,表示内容清空,里面进行注册
    context.refresh();
    context.registerBean("a1",A.class, () -> new A());
    //3.获取在spring注册的对象
    A a = context.getBean("a1", A.class);
    System.out.println(a);
}

3、整合 JUnit5 单元测试框架

  • Spring5 支持整合 JUnit5

3.1、整合 JUnit4

  1. 引入 Spring 相关针对依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.3.23</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
    </dependency>
  1. 使用注解方式完成测试
package com.cjf;

import com.cjf.bean.User;
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;

/**
 * @PACKAGE_NAME: com.cjf
 * @NAME: JTest4
 * @USER: Mrs.Wang
 * @DATE: 2022/11/16
 * @TIME: 21:35
 * @DAY_NAME_SHORT: 周三
 * @DAY_NAME_FULL: 星期三
 * @PROJECT_NAME: spring6
 **/
@RunWith(SpringJUnit4ClassRunner.class)//单元测试框架
@ContextConfiguration("classpath:spring.xml")//加载配置文件
public class JTest4 {
    @Autowired
    private User user;

    @Test
    public void test1() {
        System.out.println(user);
    }
}

3.2、整合 JUnit5

  1. 引入 JUnit5 的依赖

image-20221116214556803

  • 查看依赖

    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>RELEASE</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>RELEASE</version>
      <scope>test</scope>
    </dependency>
    
  1. 使用注解方式完成测试

    • @ExtendWith(SpringExtension.class)
      @ContextConfiguration("classpath:spring.xml")//加载配置文件
      
    • package com.cjf;
      
      import com.cjf.bean.User;
      
      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;
      
      /**
       * @PACKAGE_NAME: com.cjf
       * @NAME: JTest4
       * @USER: Mrs.Wang
       * @DATE: 2022/11/16
       * @TIME: 21:35
       * @DAY_NAME_SHORT: 周三
       * @DAY_NAME_FULL: 星期三
       * @PROJECT_NAME: spring6
       **/
      @ExtendWith(SpringExtension.class)
      @ContextConfiguration("classpath:spring.xml")//加载配置文件
      public class JTest5 {
          @Autowired
          private User user;
      
          @Test
          public void test1() {
              System.out.println(user);
          }
      }
      
      
  • 复合注解(替代上面的两个注解)

    • @SpringJUnitConfig(locations = "classpath:spring.xml")
      
    • @SpringJUnitConfig(locations = "classpath:spring.xml")
      public class JTest5 {
          @Autowired
          private User user;
      
          @Test
          public void test1() {
              System.out.println(user);
          }
      }
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值