Spring5框架八:sping5新特性

Spring5新特性

一、基于Java8
  1. 基于Java8,运行时兼容JDK9,许多不建议使用的类和方法在代码中删除
二、Spring5中自带了通用的日志封装
  1. Spring5已将移除了Log4jConfigListener,官方建议使用Log4j2
  2. Spring5框架整合Log4j2
  3. 导入整合Log4j2相关的jar包

    在这里插入图片描述

  4. 创建log4j2.xml配置文件

    ① 设置status的优先级,越往右则优先级别越高,显示的内容越多

    ② 设置日志输出的格式

    <?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>
    
三,Spring5框架核心容器支持@Nullable注解
  1. @Nullable注解可用在方法、属性、参数上面,表示方法返回可以为空属性值可以为空参数值可以为空
四、Spring5核心容器支持函数式风格GenericApplicationContext
  1. 函数式风格创建对象,交给spring进行管理

    public class TestAccount {
        @Test
        public void genericApplicationContextTest() {
            // 1.创建GenericApplicationContext对象
            GenericApplicationContext context = new GenericApplicationContext();
            // 2.调用context内的registerBean方法进行对象注册
            context.refresh();
            context.registerBean("testAccount",TestAccount.class, () -> new TestAccount());
            // 3.获取在spring注册的对象
            TestAccount testAccount =(TestAccount) context.getBean("testAccount");
            System.out.println("testAccount = " + testAccount);
        }
    }
    
五、整合Junit
  1. 导入jar包

    在这里插入图片描述

  2. 整合Junit4
    import org.junit.Test;
    
    // 指定单元测试Junit的版本
    @RunWith(SpringJUnit4ClassRunner.class)
    // 指定配置文件
    @ContextConfiguration("classpath:bean1.xml")
    public class Junit4Test {
        @Autowired
        private UserService userService;
        @Test
        public void test1() {
            userService.accountMoney();
        }
    }
    
  3. 整合Junit5
    import org.junit.jupiter.api.Test;
    
    // 整合Junit5所用的注解
    @ExtendWith(SpringExtension.class)
    // 装载配置文件
    @ContextConfiguration("classpath:bean1.xml")
    public class Junit5Test {
        @Autowired
        private UserService userService;
        @Test
        public void test1() {
            userService.accountMoney();
        }
    }
    
  4. 复合注解@SpringConfig,针对 Junit5
    import org.junit.jupiter.api.Test;
    
    @SpringJUnitConfig(locations = "classpath:bean1.xml")
    public class Junit5Test {
        @Autowired
        private UserService userService;
        @Test
        public void test1() {
            userService.accountMoney();
        }
    }
    
六、小插曲
  1. 在创建sping配置文件时,不小心写错了后缀名,导致整个项目中的sping配置文件全部失效了,里面的字都变成了黄色。
  2. 解决办法:

    在File Types -> XML -> 添加:*.xml

    在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

e_nanxu

感恩每一份鼓励-相逢何必曾相识

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值