仅需四步,写一个springboot starter
引言
第一步 创建maven项目
Do not start your module names with spring-boot, even if you use a different Maven groupId. We may offer official support for the thing you auto-configure in the future.
As a rule of thumb, you should name a combined module after the starter.
<?xml version="1.0" encoding="UTF-8"?><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>org.example</groupId>
<artifactId>aspectlog-spring-boot-starter</artifactId>
<version>1.0.2</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.15.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Spring Boot uses an annotation processor to collect the conditions on auto-configurations in a metadata file ( META-INF/spring-autoconfigure-metadata.properties ). If that file is present, it is used to eagerly filter auto-configurations that do not match, which will improve startup time. It is recommended to add the following dependency in a module that contains auto-configurations:<dependency><groupId> org.springframework.boot </groupId><artifactId> spring-boot-autoconfigure-processor </artifactId><optional> true </optional></dependency>
在编译时会自动收集配置类的条件,写到一个META-INF/spring-autoconfigure-metadata.properties中。
第二步写自动配置逻辑
类型 | 注解 | 说明 |
Class Conditions 类条件注解 | @ConditionalOnClass | 当前classpath下 有指定类才加载 |
@ConditionalOnMissingClass | 当前classpath下 无指定类才加载 | |
Bean Conditions Bean条件注解 | @ConditionalOnBean | 当期容器内有 指定bean才加载 |
@ConditionalOnMissingBean | 当期容器内无 指定bean才加载 | |
Property Conditions 环境变量条件 注解(含配置文件) | @ConditionalOnProperty | prefix 前缀 name 名称 havingValue 用于匹配配置项值 matchIfMissing 没找指定配置项时 的默认值 |
Resource Conditions 资源条件注解 | @ConditionalOnResource | 有指定资源才加载 |
Web Application Conditions web条件注解 | @ConditionalOnWebApplication | 是web才加载 |
@ConditionalOnNotWebApplication | 不是web才加载 | |
SpEL Expression Conditions | @ConditionalOnExpression | 符合SpEL 表达式才加载 |
2.1.定义AspectLog注解,该注解用于标注需要打印执行时间的方法。
package com.shanyuan.autoconfiguration.aspectlog;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* class_name: ScheduleManage
* describe: 用于控制定时任务的开启与关闭
* 对应切面
* creat_user: wenl
* creat_time: 2018/11/10 18:45
**/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AspectLog {
}
2.2定义配置文件对应类
package com.shanyuan.autoconfiguration.aspectlog;import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("aspectLog")
public class AspectLogProperties {
private boolean enable;
public boolean isEnable() {
returnenable;
}
public void setEnable(boolean enable) {
this.enable = enable;
}
}
2.3定义自动配置类
package com.shanyuan.autoconfiguration.aspectlog;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.PriorityOrdered;
@Aspect
@EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true)
@Configuration
@ConditionalOnProperty(prefix = "aspectLog", name = "enable",
havingValue = "true", matchIfMissing = true)
public class AspectLogAutoConfiguration implements PriorityOrdered {
protected Logger logger = LoggerFactory.getLogger(getClass());
@Around("@annotation(com.shanyuan.autoconfiguration.aspectlog.AspectLog) ")
public Object isOpen(ProceedingJoinPoint thisJoinPoint)
throws Throwable {
//执行方法名称
String taskName = thisJoinPoint.getSignature()
.toString().substring(
thisJoinPoint.getSignature()
.toString().indexOf(" "),
thisJoinPoint.getSignature().toString().indexOf("("));
taskName = taskName.trim();
long time = System.currentTimeMillis();
Object result = thisJoinPoint.proceed();
logger.info("method:{} run :{} ms", taskName,
(System.currentTimeMillis() - time));
return result;
}
@Override
public int getOrder() {
//保证事务等切面先执行
return Integer.MAX_VALUE;
}
}
第三步META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.shanyuan.autoconfiguration.aspectlog.AspectLogAutoConfiguration
第四步打包测试
以上是 仅需四步,写一个springboot starter 的全部内容, 来源链接: utcz.com/a/35352.html