第一章:简介
- 整个 Spring5 框架的代码基于 Java8,运行时兼容 JDK9,许多不建议使用的类和方法在代码库中删除。
- Spring 5.0 框架自带了通用的日志封装。
- 支持
@Nullable
注解。 - Spring5 支持整合 JUnit5。
第二章:Spring5 整合 Log4j2
2.1 导入相关依赖
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.13.3</version>
</dependency>
<!-- junit单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.12</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.13.3</version>
</dependency>
2.2 创建 log4j2 的配置文件
<?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.3 测试
package com.github.fairy.era.bean;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author 许大仙
* @version 1.0
* @since 2021-11-05 11:02
*/
public class SpringTest {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringTest.class);
@Test
public void test() {
LOGGER.info("info....");
LOGGER.debug("debug...");
}
}
第三章:JSR305 标准相关注解
3.1 从 JSR 说起
3.1.1 JCP
- JCP(Java Community Process) 是一个由SUN公司发起的,开放的国际组织。
- 主要由Java开发者以及被授权者组成,负责Java技术规范维护,Java技术发展和更新。
- 官网。
3.1.2 JSR
- JSR 的全称是:Java Specification Request,意思是 Java 规范提案。
- 谁向谁提案呢?任何人都可以向 JCP (Java Community Process) 提出新增一个标准化技术规范的正式请求。
- JSR已成为Java界的一个重要标准。登录 JCP 官网 可以查看 所有 JSR 标准 。
3.2 JSR305
- JSR 305: Annotations for Software Defect Detection。
- This JSR will work to develop standard annotations (such as @NonNull) that can be applied to Java programs to assist tools that detect software defects。
- 主要功能:使用注解(例如 @NonNull 等等)协助开发者侦测软件缺陷。
- Spring 从 5.0 版本开始支持了 JSR 305 规范中涉及到的相关注解。
package org.springframework.lang;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.annotation.Nonnull;
import javax.annotation.meta.TypeQualifierNickname;
/**
* A common Spring annotation to declare that annotated elements cannot be {@code null}.
*
* <p>Leverages JSR-305 meta-annotations to indicate nullability in Java to common
* tools with JSR-305 support and used by Kotlin to infer nullability of Spring API.
*
* <p>Should be used at parameter, return value, and field level. Method overrides should
* repeat parent {@code @NonNull} annotations unless they behave differently.
*
* <p>Use {@code @NonNullApi} (scope = parameters + return values) and/or {@code @NonNullFields}
* (scope = fields) to set the default behavior to non-nullable in order to avoid annotating
* your whole codebase with {@code @NonNull}.
*
* @author Sebastien Deleuze
* @author Juergen Hoeller
* @since 5.0
* @see NonNullApi
* @see NonNullFields
* @see Nullable
*/
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Nonnull
@TypeQualifierNickname
public @interface NonNull {
}
3.3 相关注解
注解名称 |
含义 |
可标记位置 |
@Nullable |
可以为空 |
@Target({ElementType.METHOD,ElementType.PARAMETER, ElementType.FIELD}) |
@NonNull |
不应为空 |
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD}) |
@NonNullFields |
在特定包下的字段不应为空 |
@Target(ElementType.PACKAGE),@TypeQualifierDefault(ElementType.FIELD) |
@NonNullApi |
参数和方法返回值不应为空 |
@Target(ElementType.PACKAGE),@TypeQualifierDefault({ElementType.METHOD, ElementType.PARAMETER}) |
第四章:Spring5 整合 Junit5
4.1 导入相关依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.12</version>
</dependency>
<!-- junit单元测试 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
4.2 Spring 的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
4.3 测试
package com.github.fairy.era.bean;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
/**
* @author 许大仙
* @version 1.0
* @since 2021-11-05 11:02
*/
@ExtendWith(SpringExtension.class) // 表示使用 Spring 提供的扩展功能
@ContextConfiguration(value = {"classpath:applicationContext.xml"}) // 指定 Spring 配置文件位置
public class SpringTest {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringTest.class);
@Test
public void test() {
LOGGER.info("info....");
LOGGER.debug("debug...");
}
}
4.3 使用复合注解
@SpringJUnitConfig
注解综合了前面两个注解的功能,此时只需要知道 Spring
的配置文件即可,但是注意要使用 locations
属性。
package com.github.fairy.era.bean;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
/**
* @author 许大仙
* @version 1.0
* @since 2021-11-05 11:02
*/
//@ExtendWith(SpringExtension.class) // 表示使用 Spring 提供的扩展功能
//@ContextConfiguration(value = {"classpath:applicationContext.xml"}) // 指定 Spring 配置文件位置
@SpringJUnitConfig(locations = {"classpath:applicationContext.xml"})
public class SpringTest {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringTest.class);
@Test
public void test() {
LOGGER.info("info....");
LOGGER.debug("debug...");
}
}