一)spring-boot-starter命名规则
自动配置模块命名规则:xxx-spring-boot,如:aspectlog-spring-boot
启动器命名规则:xxx-spring-boot-starter,如:aspectlog-spring-boot-starter
如两者只有一个模块:建议以xxx-spring-boot-starter方式命名。
springboot建议以xxx前缀的方式对自己的自动配置命名的。
二)spring-boot-starter条件注解
注解 | 说明 |
@ConditionalOnClass | 指定加载的类 |
@ConditionalOnMissingClass | 指定不加载的类 |
@ConditionalOnBean | 指定需要加载的bean |
@ConditionalOnMissingBean | 指定不需要加载的bean |
@ConditionalOnProperty | 指定加载配置文件中的属性,如yml或properties文件 |
@ConditionalOnResource | 检查特定的资源是否存在,如:file:/home/user/test.dat |
@ConditionalOnExpression | 使用SpEL表达式 |
该文章使用@ConditionalOnProperty注解实现。
三)创建自己的aspectlog-spring-boot-starter日志打印自动配置模块
第一步:创建一个aspectlog-spring-boot-starter名称的maven项目
在pom.xml文件中引入springboot相应jar
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.oysept</groupId>
<artifactId>aspectlog-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- springboot版本信息 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<de