欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2020年09月15日 14:38:35   作者:ouyangjun__  
這篇文章主要介紹了SpringBoot2.1.x,創(chuàng)建自己的spring-boot-starter自動配置模塊操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

一)spring-boot-starter命名規(guī)則

自動配置模塊命名規(guī)則:xxx-spring-boot,如:aspectlog-spring-boot

啟動器命名規(guī)則:xxx-spring-boot-starter,如:aspectlog-spring-boot-starter

如兩者只有一個(gè)模塊:建議以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表達(dá)式

該文章使用@ConditionalOnProperty注解實(shí)現(xiàn)。

三)創(chuàng)建自己的aspectlog-spring-boot-starter日志打印自動配置模塊

第一步:創(chuàng)建一個(gè)aspectlog-spring-boot-starter名稱的maven項(xiàng)目

在pom.xml文件中引入springboot相應(yīng)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作用:會在源數(shù)據(jù)文件(META-INF/spring-autoconfigure-metadata.properties)中自動掃描加載和自動配置有關(guān)的條件。也就是說,當(dāng)編寫starter時(shí),會讀取自動配置的條件,寫入源數(shù)據(jù)文件中。

第二步:定義一個(gè)AspectLog注解類

該注解作用于方法上,并在運(yùn)行時(shí)啟用

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 {
	
}

第三步:創(chuàng)建一個(gè)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;
 }
}

第四步:創(chuàng)建一個(gè)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 {
  //執(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("==>aspectlog 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)

當(dāng)yml或properties配置文件中有aspectLog.enable=true時(shí)開啟,如果配置文件沒有設(shè)置aspectLog.enable也開啟。

第五步:創(chuàng)建spring.factories文件,該文件是springboot規(guī)定的配置文件,把自動配置類按規(guī)則配置

先在src/main/resources下創(chuàng)建一個(gè)META-INF文件夾,然后在文件夾下創(chuàng)建spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

com.oysept.autoconfiguration.aspectlog.AspectLogAutoConfiguration

META-INF/spring.factories是spring的工廠機(jī)制,在這個(gè)文件中定義的類,都會被自動加載。多個(gè)配置使用逗號分割,換行用\

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

四)測試aspectlog-spring-boot-starter打印日志配置

另外創(chuàng)建一個(gè)springboot_starter_test名稱的maven項(xiàng)目

在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命令窗口執(zhí)行,如: mvn install -U -->
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
</project>

創(chuàng)建一個(gè)application.yml,配置啟動的端口,默認(rèn)是8080

server:

port: 8080

創(chuàng)建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);
 }
}

創(chuàng)建測試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);
 
  // 處理業(yè)務(wù)邏輯
  return "/test/starter/aspectlog SUCCESS!";
 }
}

在想要打印日志的方法上,使用@AspectLog注解

啟動TestSpringBootStarterApplication中的main方法

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

然后在控制臺查看效果:

以上這篇SpringBoot2.1.x,創(chuàng)建自己的spring-boot-starter自動配置模塊操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java并發(fā)編程之Semaphore詳解

    Java并發(fā)編程之Semaphore詳解

    這篇文章主要介紹了Java并發(fā)編程之concurrent包中的Semaphore詳解,信號量Semaphore一般用來表示可用資源的個(gè)數(shù),相當(dāng)于一個(gè)計(jì)數(shù)器,可類比生活中停車場牌子上面顯示的停車場剩余車位數(shù)量,需要的朋友可以參考下
    2023-12-12
  • Java文件寫入器FileWriter使用指南

    Java文件寫入器FileWriter使用指南

    在Java中,FileWriter類用于將字符寫入文件中,它繼承了Writer類,因此可以使用Writer類中的所有方法,下面我們就來深入探討一下FileWriter類的使用方法吧
    2023-10-10
  • Java開發(fā)中的volatile你必須要了解一下

    Java開發(fā)中的volatile你必須要了解一下

    這篇文章主要給大家介紹了關(guān)于Java開發(fā)中volatile的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • MyBatis Plus工具快速入門使用教程

    MyBatis Plus工具快速入門使用教程

    這篇文章主要介紹了MyBatis Plus工具快速入門使用教程,需要的朋友可以參考下
    2018-05-05
  • Java算法之冒泡排序?qū)嵗a

    Java算法之冒泡排序?qū)嵗a

    今天小編就為大家分享一篇關(guān)于Java算法之冒泡排序?qū)嵗a,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Java inputstream和outputstream使用詳解

    Java inputstream和outputstream使用詳解

    這篇文章主要介紹了Java inputstream和outputstream使用詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • java實(shí)現(xiàn)簡單的客戶信息管理系統(tǒng)

    java實(shí)現(xiàn)簡單的客戶信息管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡單的客戶信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • java 操作gis geometry類型數(shù)據(jù)方式

    java 操作gis geometry類型數(shù)據(jù)方式

    這篇文章主要介紹了java 操作gis geometry類型數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 實(shí)例解析Java設(shè)計(jì)模式編程中的適配器模式使用

    實(shí)例解析Java設(shè)計(jì)模式編程中的適配器模式使用

    適配器模式的主要作用是在新接口和老接口之間進(jìn)行適配,通過將一個(gè)類的接口轉(zhuǎn)換成客戶期望的另一個(gè)接口,讓原本不兼容的接口可以合作無間,本文以實(shí)例解析Java設(shè)計(jì)模式編程中的適配器模式使用,需要的朋友可以參考下
    2016-05-05
  • spring boot 注入 property的三種方式(推薦)

    spring boot 注入 property的三種方式(推薦)

    這篇文章主要介紹了spring boot 注入 property的三種方式,需要的朋友可以參考下
    2017-07-07

最新評論