本人已参与[新人创作礼]活动,一起开启掘金创作之路。
整个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;
/**
* Created on 2022/3/22.
*
* @author Hou chaof
*/
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针对测试的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.17</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
*步骤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;
/**
* Created on 2022/3/22.
*
* @author Hou chaof
*/
@RunWith(SpringJUnit4ClassRunner.class) //指定单元测试框架
@ContextConfiguration("classpath:applicationContext.xml") //加载配置文件
public class JTest4 {
@Autowired
private UserService userService;
@Test
public void test1(){
userService.accountMoney();
}
}
(2)Spring5整合Junit5
步骤1*:引入依赖
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
步骤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;
/**
* Created on 2022/3/22.
*
* @author Hou chaof
*/
@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;
/**
* Created on 2022/3/22.
*
* @author Hou chaof
*/
@SpringJUnitConfig(locations = "classpath:applicationContext.xml")
public class JTest5 {
@Autowired
private UserService userService;
@Test
public void test(){
userService.accountMoney();
}
}
整个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;
/**
* Created on 2022/3/22.
*
* @author Hou chaof
*/
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针对测试的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.17</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
*步骤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;
/**
* Created on 2022/3/22.
*
* @author Hou chaof
*/
@RunWith(SpringJUnit4ClassRunner.class) //指定单元测试框架
@ContextConfiguration("classpath:applicationContext.xml") //加载配置文件
public class JTest4 {
@Autowired
private UserService userService;
@Test
public void test1(){
userService.accountMoney();
}
}
(2)Spring5整合Junit5
步骤1*:引入依赖
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
步骤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;
/**
* Created on 2022/3/22.
*
* @author Hou chaof
*/
@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;
/**
* Created on 2022/3/22.
*
* @author Hou chaof
*/
@SpringJUnitConfig(locations = "classpath:applicationContext.xml")
public class JTest5 {
@Autowired
private UserService userService;
@Test
public void test(){
userService.accountMoney();
}
}