1.特性一整个框架基于java8,运行的时候兼容JDK9,许多不建议的类和方法删除。
2. 核心特性
1. Spring5.0 框架自带了通用的日志封装。
1.Spring5 已经移除了 Log4jConfigListener, 官方建议使用 Log4j2
2.Spring5 框架整个Log4j2
第一步,引入相关jar包,注意有scope的千万去掉
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.3</version>
<!-- <scope>test</scope>-->
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!--日志级别以及有限排序:OFF>FATAL>ERROR>WARN>INFO>DEBUG>TRACE>ALL-->
<!--configuration 后面的status 用于设置lo4j2自身内部的信息输出,可以不设置,当设置成trace时,可以看到log4j2内部各种详细输出-->
<Configuration status= "INFO" packages="com.demo.study_spring">
<!--先定义所有的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">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
测试方法
@Test
public void testLog() {
logger.info("sadsadsad==");
logger.warn("测试双生视界");
}
public static void main(String[] args) {
logger.info("sadsadsad==");
logger.warn("测试双生视界");
}
2. Spring5 框架核心容器支持@Nullable 注解
1. @Nullable 注解可以使用在方法上面,属性上面,参数上面,表示方法返回可以为空,属性值可以为空,参数值可以为空。
2. 注解用在方法上,表示返回值可以为空
3.注解用在方法的参数中,标识参数可以为空
3. 支持函数式风格创建对象,交给spring管理GenericApplicationContext
@Test
public void testGene() {
// 创建 GenericApplicationContext对象
GenericApplicationContext context = new GenericApplicationContext();
// 调用context的方法对象注册
context.refresh();
context.registerBean(BankLog.class, () -> new BankLog());
// 获取在spring注册的对象
BankLog bankLog = (BankLog) context.getBean("com.demo.study_spring.bank.entity.BankLog");
System.out.println(bankLog);
}
getbean中的为全路径
@Test
public void testGene() {
// 创建 GenericApplicationContext对象
GenericApplicationContext context = new GenericApplicationContext();
// 调用context的方法对象注册
context.refresh();
context.registerBean("bankLog",BankLog.class, () -> new BankLog());
// 获取在spring注册的对象
BankLog bankLog = (BankLog) context.getBean("bankLog");
System.out.println(bankLog);
}
4. Spring5 支持整个Junit5
1. 整合Junit4
第一步 引入Spring 相关针对测试依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.13</version>
</dependency>
第二步创建测试类,使用注解方式完成
import com.demo.study_spring.bank.service.BankAccountServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
// 单元测试框架
@RunWith(SpringJUnit4ClassRunner.class)
// 加载配置文件
@ContextConfiguration("classpath:bean4.xml")
public class Test4 {
@Resource
private BankAccountServiceImpl bankAccountService;
@Test
public void mytest() {
bankAccountService.change();
}
}
2. Spring5 整合junit5
第一步,引入Junit5 的jar包
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
</dependency>
第二步测试方法
import com.demo.study_spring.bank.service.BankAccountServiceImpl;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import javax.annotation.Resource;
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:bean4.xml")
public class Test5 {
@Resource
private BankAccountServiceImpl bankAccountService;
@Test
public void mytest() {
bankAccountService.change();
}
}
可以简化为
import com.demo.study_spring.bank.service.BankAccountServiceImpl;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import javax.annotation.Resource;
//@ExtendWith(SpringExtension.class)
//@ContextConfiguration("classpath:bean4.xml")
@SpringJUnitConfig(locations = "classpath:bean4.xml")
public class Test5 {
@Resource
private BankAccountServiceImpl bankAccountService;
@Test
public void mytest() {
bankAccountService.change();
}
}