1. 程式人生 > 程式設計 >SpringBoot2.1.x,建立自己的spring-boot-starter自動配置模組操作

SpringBoot2.1.x,建立自己的spring-boot-starter自動配置模組操作

一)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>
 
 <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-configuration-processor作用:會在源資料檔案(META-INF/spring-autoconfigure-metadata.properties)中自動掃描載入和自動配置有關的條件。也就是說,當編寫starter時,會讀取自動配置的條件,寫入源資料檔案中。

第二步:定義一個AspectLog註解類

該註解作用於方法上,並在執行時啟用

package com.oysept.autoconfiguration.aspectlog;
 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AspectLog {
	
}

第三步:建立一個AspectLogProperties類,用於載入yml或properties中的屬性

package com.oysept.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;
 }
}

第四步:建立一個AspectLogAutoConfiguration列印日誌自動配置類

package com.oysept.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.ConditionalOnProperty;
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.oysept.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("==>aspectlog method:{} run :{} ms",taskName,(System.currentTimeMillis() - time));
  return result;
 }
 
 @Override
 public int getOrder() {
  //保證事務等切面先執行
  return Integer.MAX_VALUE;
 }
}

註解說明:

@ConditionalOnProperty(prefix = "aspectLog",matchIfMissing = true)

當yml或properties配置檔案中有aspectLog.enable=true時開啟,如果配置檔案沒有設定aspectLog.enable也開啟。

第五步:建立spring.factories檔案,該檔案是springboot規定的配置檔案,把自動配置類按規則配置

先在src/main/resources下建立一個META-INF資料夾,然後在資料夾下建立spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

com.oysept.autoconfiguration.aspectlog.AspectLogAutoConfiguration

META-INF/spring.factories是spring的工廠機制,在這個檔案中定義的類,都會被自動載入。多個配置使用逗號分割,換行用\

第六步:使用mvn install方式把該模組自動打包

SpringBoot2.1.x,建立自己的spring-boot-starter自動配置模組操作

四)測試aspectlog-spring-boot-starter列印日誌配置

另外建立一個springboot_starter_test名稱的maven專案

在pom中引入aspectlog-spring-boot-starter的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>springboot_starter_test</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>
 
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.4.RELEASE</version>
  <relativePath/>
 </parent>
 
 <properties>
  <java.version>1.8</java.version>
 </properties>
 
 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  
  <!-- 引入自定義aspectlog-spring-boot-starter 列印日誌jar -->
  <dependency>
   <groupId>com.oysept</groupId>
   <artifactId>aspectlog-spring-boot-starter</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </dependency>
 </dependencies>
 
 <!-- maven打包外掛,在cmd命令視窗執行,如: mvn install -U -->
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
</project>

建立一個application.yml,配置啟動的埠,預設是8080

server:

port: 8080

建立application啟動類

package com.oysept;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class TestSpringBootStarterApplication {
 
 public static void main(String[] args) {
  SpringApplication.run(TestSpringBootStarterApplication.class,args);
 }
}

建立測試AspectLog功能controller

package com.oysept.controller; 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; 
import com.oysept.autoconfiguration.aspectlog.AspectLog; 
@RestController
public class GetController {
 
 /**
  * 訪問地址: http://localhost:8080/test/starter/aspectlog?param=TTTEEESSSTTT
  * @return
  */
 @AspectLog
 @RequestMapping(value="/test/starter/aspectlog",method = RequestMethod.GET)
 public String testStarterAspectLog(@RequestParam(value = "param") String param) {
  System.out.println("==>/test/starter/aspectlog,param: " + param);
 
  // 處理業務邏輯
  return "/test/starter/aspectlog SUCCESS!";
 }
}

在想要列印日誌的方法上,使用@AspectLog註解

啟動TestSpringBootStarterApplication中的main方法

在瀏覽器中輸入:http://localhost:8080/test/starter/aspectlog?param=TTTEEESSSTTT

然後在控制檯檢視效果:

SpringBoot2.1.x,建立自己的spring-boot-starter自動配置模組操作

以上這篇SpringBoot2.1.x,建立自己的spring-boot-starter自動配置模組操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。