只需4步,自己搞個(gè) Spring Boot Starter !
閱讀本文大概需要 4 分鐘。
引言
第一步 創(chuàng)建maven項(xiàng)目
<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.0modelVersion>
????<groupId>org.examplegroupId>
????<artifactId>aspectlog-spring-boot-starterartifactId>
????<version>1.0.2version>
????<parent>
????????<groupId>org.springframework.bootgroupId>
????????<artifactId>spring-boot-starter-parentartifactId>
????????<version>2.1.15.RELEASEversion>
????parent>
????
????<dependencies>
????????<dependency>
????????????<groupId>org.springframework.bootgroupId>
????????????<artifactId>spring-boot-autoconfigureartifactId>
????????dependency>
????????<dependency>
????????????<groupId>org.springframework.bootgroupId>
????????????<artifactId>spring-boot-starter-aopartifactId>
????????dependency>
????????<dependency>
????????????<groupId>org.springframework.bootgroupId>
????????????<artifactId>spring-boot-configuration-processorartifactId>
????????????<optional>trueoptional>
????????dependency>
????dependencies>
project>
true
第二步寫自動配置邏輯

2.1.定義AspectLog注解,該注解用于標(biāo)注需要打印執(zhí)行時(shí)間的方法。
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:???用于控制定時(shí)任務(wù)的開啟與關(guān)閉
?*?對應(yīng)切面
?*?creat_user:?wenl
?*?creat_time:??2018/11/10?18:45
?**/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public?@interface???AspectLog?{
}
2.2定義配置文件對應(yīng)類
package?com.shanyuan.autoconfiguration.aspectlog;
import?org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("aspectLog")
public?class?AspectLogProperties?{
????private?boolean?enable;
????public?boolean?isEnable()?{
????????return?enable;
????}
????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?{
????????//執(zhí)行方法名稱?
????????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()?{
????????//保證事務(wù)等切面先執(zhí)行
????????return?Integer.MAX_VALUE;
????}
}
@ConditionalOnProperty(prefix?=?"aspectLog",?name?=?"enable",havingValue?=?"true",?matchIfMissing?=?true)
aspectLog.enable=true時(shí)開啟,如果配置文件沒有設(shè)置aspectLog.enable也開啟。第三步META-INF/spring.factories
META-INF/spring.factories是spring的工廠機(jī)制,在這個(gè)文件中定義的類,都會被自動加載。多個(gè)配置使用逗號分割,換行用\@Enable驅(qū)動原理(設(shè)置連接)@EnableAutoConfiguration處理邏輯(設(shè)置連接)org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.shanyuan.autoconfiguration.aspectlog.AspectLogAutoConfiguration
第四步打包測試



https://docs.spring.io/spring-boot/docs/2.1.15.RELEASE/reference/html/boot-features-developing-auto-configuration.html#boot-features-custom-starter
推薦閱讀:
這 3 個(gè)學(xué)數(shù)據(jù)結(jié)構(gòu)和算法的網(wǎng)站,相信你也會喜歡的!
項(xiàng)目實(shí)踐:Spring Boot 三招組合拳,手把手教你打出優(yōu)雅的后端接口
微信掃描二維碼,關(guān)注我的公眾號
朕已閱?

