一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - SpringBoot2.1.x,創建自己的spring-boot-starter自動配置模塊操作

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

2020-09-16 00:36ouyangjun__ JAVA教程

這篇文章主要介紹了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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<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注解類

該注解作用于方法上,并在運行時啟用

?
1
2
3
4
5
6
7
8
9
10
11
12
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中的屬性

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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打印日志自動配置類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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", name = "enable",havingValue = "true", 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<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啟動類

?
1
2
3
4
5
6
7
8
9
10
11
12
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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自動配置模塊操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/p812438109/article/details/108558345

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久中文字幕乱码免费 | 福利姬 magnet | 国产91精品久久久久久久 | 女同69式互添在线观看免费 | 好大好硬好深好爽想要之黄蓉 | 国产色网址 | 交换朋友夫妇3中文字幕 | 国产欧美日韩精品一区二区三区 | 91看片淫黄大片.在线天堂 | 国产精品视频在这里有精品 | 久久99re热在线观看视频 | 国产欧美国产综合第一区 | 亚洲欧美日韩另类精品一区二区三区 | 国产精品女同久久免费观看 | 日本强不卡在线观看 | 男女男精品视频网站 | 无码国产成人午夜在线观看不卡 | 2012年中文字幕在线看 | 国产亚洲欧美一区二区三区 | 国产精品久久久久影视不卡 | 欧美草逼网 | 黄瓜污视频 | 韩剧消失的眼角膜免费完整版 | 美女把腿开让我 | 国产精品一区二区不卡的视频 | 黑人巨大精品战中国美女 | 国产精品色片 | 国产精品美女福利视频免费专区 | 免费观看俄罗斯特黄特色 | 日韩在线二区全免费 | 9966久久精品免费看国产 | 亚洲国产精品免费在线观看 | 青春学堂在线观看 | 亚洲 综合 自拍 精品 在线 | 无人在线视频高清免费观看动漫 | 国产亚洲精品美女久久久 | 手机能看的黄色网站 | 日韩香蕉视频 | 欧美成人另类人妖 | 亚洲国产成人精品不卡青青草原 | 国产小视频网站 |