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

Spring5框架新功能(一)(日志封装、@Nullable注解、支持函数式风格、支持整合单元测试)

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

Spring5框架新功能(一)(日志封装、@Nullable注解、支持函数式风格、支持整合单元测试)

文章目录

一、Spring5框架自带了通用的日志封装二、Spring5框架核心容器支持@Nullable注解三、Spring5核心容器支持函数式风格GenericApplicationContext四、Spring5支持整合Junit5


整个Spring5框架的代码基于Java8,运行时兼容JDK9

一、Spring5框架自带了通用的日志封装
*1.Spring5y已经移除了Log4jConfigListener,官方建议使用Log4j2
*2.Spring5框架整合 Log4j2
		*第一步:引入依赖
		*第二部:创建 log4j2.xml
二、Spring5框架核心容器支持@Nullable注解
*1.@Nullable注解可以使用在方法上面,属性上面,参数上面,表示方法的返回值可以为空,属性值可以为空,参数值可以为空。
三、Spring5核心容器支持函数式风格GenericApplicationContext
import cn.hncj.Service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericApplicationContext;


public class TestBook {
    @Test
    public void testGenericApplicationContext() {
        //创建GenericApplicationContext对象
        GenericApplicationContext context = new GenericApplicationContext();
        //调用context中的方法对象注册
        context.refresh();
        context.registerBean("user1", User.class, () -> new User());
        //获取在spring注册的对象
        User user = (User)context.getBean("user1");
        System.out.println(user);
    }

}

四、Spring5支持整合Junit5
*1.Spring5整合Junit4
			*步骤1:引入Spring针对测试的依赖 
					
       				 org.springframework
     				   spring-test
 				       5.3.17
 				       test
				    
			      
   				     junit
   				     junit
    				    4.13.2
    				    test
  				   


   		    *步骤2:创建测试类,使用注解方式完成
import cn.hncj.Service.UserService;
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;


@RunWith(SpringJUnit4ClassRunner.class) //指定单元测试框架
@ContextConfiguration("classpath:applicationContext.xml") //加载配置文件
public class JTest4 {
    @Autowired
    private UserService userService;
    @Test
    public void test1(){
        userService.accountMoney();
    }
}

(2)Spring5整合Junit5
		步骤1*:引入依赖
		
 		   
      		  org.junit.jupiter
     		   junit-jupiter-api
     		   5.8.2
    		    test
  		  
   		 
		        org.junit.jupiter
		        junit-jupiter-engine
	         5.8.2
	          test
	      
	     步骤2*:创建测试类,使用注解完成
import cn.hncj.Service.UserService;
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;


@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JTest5 {
    @Autowired
    private UserService userService;
    @Test
    public void test(){
        userService.accountMoney();
    }
}




//简化如下:使用一个@SpringJunitConfig注解替代注解@ExtendWith和注解
@ContextConfiguration
import cn.hncj.Service.UserService;
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;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;


@SpringJUnitConfig(locations = "classpath:applicationContext.xml")
public class JTest5 {
    @Autowired
    private UserService userService;
    @Test
    public void test(){
        userService.accountMoney();
    }
}


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

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

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