一、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-test5.3.17 test *步骤2:创建测试类,使用注解方式完成 junit junit4.13.2 test
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-api5.8.2 test 步骤2*:创建测试类,使用注解完成 org.junit.jupiter junit-jupiter-engine5.8.2 test
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();
}
}



