Spring5新特性

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

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

1、Spring5移除了Log4jConfigListener,官方建议使用log4j2

2、Spring框架整合log4j2

第一步引入相关的jar包

第二步创建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>

测试日志

演示优先级定为INFO

演示优先级定为DEBUG会出现更多的日志信息

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

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

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

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

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

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

支持Lambda表达式

public void testGenericApplicationContext(){
    //创建GenericApplicationContext对象
    GenericApplicationContext context = new GenericApplicationContext();
    //调用context对象的方法对象注册
    context.refresh();
    context.registerBean("account",Account.class,()->new Account());
    Account account = (Account) context.getBean("account");
    System.out.println(account);
}

Spring5.0支持整合JUnit5

下面我先演示JUnit4

@RunWith(SpringJUnit4ClassRunner.class)//单元测试框架

@ContextConfiguration("classpath:bean.xml")//加载配置文件

@RunWith(SpringJUnit4ClassRunner.class)//单元测试框架
@ContextConfiguration("classpath:bean.xml")//加载配置文件
public class TestJUnit4 {
    @Autowired
    private AccountService accountService;
    @Test
    public void test(){
        System.out.println(accountService.transfer("2","1")?"转账成功":"转账失败");
    }
}

下面演示JUnit5

导入JUnit5的依赖

两个注解

@ExtendWith(SpringExtension.class)

@ContextConfiguration("classpath:bean.xml")

@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:bean.xml")
public class TestJUnit5 {
    @Autowired
    private AccountService accountService;
    @Test
    public void test(){
        System.out.println(accountService.transfer("2","1")?"转账成功":"转账失败");
    }
}

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

@SpringJUnitConfig(locations = "classpath:bean.xml")//使用一个复合注解完成整合

@SpringJUnitConfig(locations = "classpath:bean.xml")//使用一个复合注解完成整合
public class TestJUnit5 {
    @Autowired
    private AccountService accountService;
    @Test
    public void test(){
        System.out.println(accountService.transfer("2","1")?"转账成功":"转账失败");
    }
}

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值