当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot2.1.x,创建自己的spring-boot-starter自动配置模块操作

SpringBoot2.1.x,创建自己的spring-boot-starter自动配置模块操作

2020年09月16日  | 移动技术网IT编程  | 我要评论
一)spring-boot-starter命名规则自动配置模块命名规则:xxx-spring-boot,如:aspectlog-spring-boot启动器命名规则:xxx-spring-boot-s

一)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", 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方式把该模块自动打包

四)测试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://www.lhsxpumps.com/_localhost:8080/test/starter/aspectlog?param=ttteeesssttt

然后在控制台查看效果:

以上这篇springboot2.1.x,创建自己的spring-boot-starter自动配置模块操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网