Spring5新功能
整个Spring5框架代码基于Java8,运行时兼容JDK9,许多不建议使用的类和方法在代码库中删除。
1.整合日志框架
Spring5框架整合Log4j2
- 第一步:引入jar包
- 第二步:创建log4j2.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--Configuration后面的status用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,可以看到log4j2内部各种详细输出-->
<configuration status="INFO">
<!--先定义所有的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">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>
2.支持@Nullable注解
@Nullable
注解可以使用在方法上面,属性上面,参数上面,表示方法返回可以为空,属性值可以 为空,参数值可以为空。
- 注解用在方法上面,方法返回值可以为空
@Nullable
public String getId();
- 注解使用在方法参数里面,方法参数可以为空
public <T> void registerBean(@Nullable String beanName,..){}
- 注解使用在属性上面,属性值可以为空
@Nullable
private String bookName;
3. 核心容器支持函数式风格
函数式风格自己创建对象,注册到IOC容器中,交给Spring进行管理
public void testGenericApplicationContext() {
//1 创建 GenericApplicationContext 对象
GenericApplicationContext context = new GenericApplicationContext();
//2 调用 context 的方法对象注册
context.refresh();
context.registerBean("user1",User.class,() -> new User());
//3 获取在 spring 注册的对象
// User user = (User)context.getBean("com.atguigu.spring5.test.User");
User user = (User)context.getBean("user1");
System.out.println(user);
}
4. 整合JUnit5
第一步:引入JUnit的jar包
第二步:创建测试类,使用注解完成
- 其中
@SpringJUnitConfig
注解相当于引入单元测试框架和加载配置文件等效于:
ApplicationContext context =
new ClassPathXmlApplicationContext("jdbc.xml");
因此可以通过注入方式获取userService对象
@SpringJUnitConfig(locations = "classpath:bean1.xml")
public class JTest5 {
@Autowired
private UserService userService;
@Test
public void test1() {
userService.accountMoney();
}
}
5.新功能-Webflux
是 Spring5 添加新的模块,用于 web 开发的,功能和 SpringMVC 类似的
先学完Springboot再回来吧