栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring5框架八:sping5新特性

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring5框架八:sping5新特性

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

  4. 创建log4j2.xml配置文件

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

    ② 设置日志输出的格式

    
    
    
    
        
        
            
            
                
                
            
        
        
        
        
            
                
            
        
    
    
三,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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/843106.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号