第一章:简介

  • 整个 Spring5 框架的代码基于 Java8,运行时兼容 JDK9,许多不建议使用的类和方法在代码库中删除。
  • Spring 5.0 框架自带了通用的日志封装。
  • 支持@Nullable 注解。
  • Spring5 支持整合 JUnit5。

第二章:Spring5 整合 Log4j2

2.1 导入相关依赖

  • log4j 相关的依赖:
  1. <dependency>
  2. <groupId>org.apache.logging.log4j</groupId>
  3. <artifactId>log4j-core</artifactId>
  4. <version>2.13.3</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.logging.log4j</groupId>
  8. <artifactId>log4j-api</artifactId>
  9. <version>2.13.3</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.slf4j</groupId>
  13. <artifactId>slf4j-api</artifactId>
  14. <version>1.7.30</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.apache.logging.log4j</groupId>
  18. <artifactId>log4j-slf4j-impl</artifactId>
  19. <version>2.13.3</version>
  20. </dependency>
  • 完整的依赖:
  1. <!-- junit单元测试 -->
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>4.13.2</version>
  6. <scope>test</scope>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-test</artifactId>
  11. <version>5.3.12</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework</groupId>
  15. <artifactId>spring-context</artifactId>
  16. <version>5.3.12</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.apache.logging.log4j</groupId>
  20. <artifactId>log4j-core</artifactId>
  21. <version>2.13.3</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.apache.logging.log4j</groupId>
  25. <artifactId>log4j-api</artifactId>
  26. <version>2.13.3</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.slf4j</groupId>
  30. <artifactId>slf4j-api</artifactId>
  31. <version>1.7.30</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.apache.logging.log4j</groupId>
  35. <artifactId>log4j-slf4j-impl</artifactId>
  36. <version>2.13.3</version>
  37. </dependency>

2.2 创建 log4j2 的配置文件

  • log4j2.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
  3. <!--Configuration后面的status用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,可以看到log4j2内部各种详细输出-->
  4. <configuration status="INFO">
  5. <!--先定义所有的appender-->
  6. <appenders>
  7. <!--输出日志信息到控制台-->
  8. <console name="Console" target="SYSTEM_OUT">
  9. <!--控制日志输出的格式-->
  10. <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  11. </console>
  12. </appenders>
  13. <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效-->
  14. <!--root:用于指定项目的根日志,如果没有单独指定Logger,则会使用root作为默认的日志输出-->
  15. <loggers>
  16. <root level="info">
  17. <appender-ref ref="Console"/>
  18. </root>
  19. </loggers>
  20. </configuration>

2.3 测试

  1. package com.github.fairy.era.bean;
  2. import org.junit.Test;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. /**
  6. * @author 许大仙
  7. * @version 1.0
  8. * @since 2021-11-05 11:02
  9. */
  10. public class SpringTest {
  11. private static final Logger LOGGER = LoggerFactory.getLogger(SpringTest.class);
  12. @Test
  13. public void test() {
  14. LOGGER.info("info....");
  15. LOGGER.debug("debug...");
  16. }
  17. }

第三章: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 规范中涉及到的相关注解。
  1. package org.springframework.lang;
  2. import java.lang.annotation.Documented;
  3. import java.lang.annotation.ElementType;
  4. import java.lang.annotation.Retention;
  5. import java.lang.annotation.RetentionPolicy;
  6. import java.lang.annotation.Target;
  7. import javax.annotation.Nonnull;
  8. import javax.annotation.meta.TypeQualifierNickname;
  9. /**
  10. * A common Spring annotation to declare that annotated elements cannot be {@code null}.
  11. *
  12. * <p>Leverages JSR-305 meta-annotations to indicate nullability in Java to common
  13. * tools with JSR-305 support and used by Kotlin to infer nullability of Spring API.
  14. *
  15. * <p>Should be used at parameter, return value, and field level. Method overrides should
  16. * repeat parent {@code @NonNull} annotations unless they behave differently.
  17. *
  18. * <p>Use {@code @NonNullApi} (scope = parameters + return values) and/or {@code @NonNullFields}
  19. * (scope = fields) to set the default behavior to non-nullable in order to avoid annotating
  20. * your whole codebase with {@code @NonNull}.
  21. *
  22. * @author Sebastien Deleuze
  23. * @author Juergen Hoeller
  24. * @since 5.0
  25. * @see NonNullApi
  26. * @see NonNullFields
  27. * @see Nullable
  28. */
  29. @Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
  30. @Retention(RetentionPolicy.RUNTIME)
  31. @Documented
  32. @Nonnull
  33. @TypeQualifierNickname
  34. public @interface NonNull {
  35. }

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 导入相关依赖

  • pom.xml
  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-context</artifactId>
  4. <version>5.3.12</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework</groupId>
  8. <artifactId>spring-test</artifactId>
  9. <version>5.3.12</version>
  10. </dependency>
  11. <!-- junit单元测试 -->
  12. <dependency>
  13. <groupId>org.junit.jupiter</groupId>
  14. <artifactId>junit-jupiter-api</artifactId>
  15. <version>5.7.0</version>
  16. <scope>test</scope>
  17. </dependency>

4.2 Spring 的配置文件

  • applicationContext.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans"
  6. 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">
  7. </beans>

4.3 测试

  1. package com.github.fairy.era.bean;
  2. import org.junit.jupiter.api.Test;
  3. import org.junit.jupiter.api.extension.ExtendWith;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.test.context.ContextConfiguration;
  7. import org.springframework.test.context.junit.jupiter.SpringExtension;
  8. /**
  9. * @author 许大仙
  10. * @version 1.0
  11. * @since 2021-11-05 11:02
  12. */
  13. @ExtendWith(SpringExtension.class) // 表示使用 Spring 提供的扩展功能
  14. @ContextConfiguration(value = {"classpath:applicationContext.xml"}) // 指定 Spring 配置文件位置
  15. public class SpringTest {
  16. private static final Logger LOGGER = LoggerFactory.getLogger(SpringTest.class);
  17. @Test
  18. public void test() {
  19. LOGGER.info("info....");
  20. LOGGER.debug("debug...");
  21. }
  22. }

4.3 使用复合注解

  • @SpringJUnitConfig 注解综合了前面两个注解的功能,此时只需要知道 Spring 的配置文件即可,但是注意要使用 locations 属性。
  1. package com.github.fairy.era.bean;
  2. import org.junit.jupiter.api.Test;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
  6. /**
  7. * @author 许大仙
  8. * @version 1.0
  9. * @since 2021-11-05 11:02
  10. */
  11. //@ExtendWith(SpringExtension.class) // 表示使用 Spring 提供的扩展功能
  12. //@ContextConfiguration(value = {"classpath:applicationContext.xml"}) // 指定 Spring 配置文件位置
  13. @SpringJUnitConfig(locations = {"classpath:applicationContext.xml"})
  14. public class SpringTest {
  15. private static final Logger LOGGER = LoggerFactory.getLogger(SpringTest.class);
  16. @Test
  17. public void test() {
  18. LOGGER.info("info....");
  19. LOGGER.debug("debug...");
  20. }
  21. }