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条件注解
该文章使用@ConditionalOnProperty注解实现。
三)创建自己的aspectlog-spring-boot-starter日志打印自动配置模块
第一步:创建一个aspectlog-spring-boot-starter名称的maven项目
在pom.xml文件中引入springboot相应jar
4.0.0 com.oysept aspectlog-spring-boot-starter 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE 1.8 org.springframework.boot spring-boot-autoconfigure org.springframework.boot spring-boot-starter-aop org.springframework.boot spring-boot-configuration-processor true
spring-boot-configuration-processor作用:会在源数据文件(META-INF/spring-autoconfigure-metadata.properties)中自动扫描加载和自动配置有关的条件。也就是说,当编写starter时,会读取自动配置的条件,写入源数据文件中。
第二步:定义一个AspectLog注解类
该注解作用于方法上,并在运行时启用
packagecom.oysept.autoconfiguration.aspectlog; importjava.lang.annotation.ElementType; importjava.lang.annotation.Retention; importjava.lang.annotation.RetentionPolicy; importjava.lang.annotation.Target; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public@interfaceAspectLog{ }
第三步:创建一个AspectLogProperties类,用于加载yml或properties中的属性
packagecom.oysept.autoconfiguration.aspectlog; importorg.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties("aspectlog") publicclassAspectLogProperties{ privatebooleanenable; publicbooleanisEnable(){ returnenable; } publicvoidsetEnable(booleanenable){ this.enable=enable; } }
第四步:创建一个AspectLogAutoConfiguration打印日志自动配置类
packagecom.oysept.autoconfiguration.aspectlog; importorg.aspectj.lang.ProceedingJoinPoint; importorg.aspectj.lang.annotation.Around; importorg.aspectj.lang.annotation.Aspect; importorg.slf4j.Logger; importorg.slf4j.LoggerFactory; importorg.springframework.boot.autoconfigure.condition.ConditionalOnProperty; importorg.springframework.context.annotation.Configuration; importorg.springframework.context.annotation.EnableAspectJAutoProxy; importorg.springframework.core.PriorityOrdered; @Aspect @EnableAspectJAutoProxy(exposeProxy=true,proxyTargetClass=true) @Configuration @ConditionalOnProperty(prefix="aspectlog",name="enable",havingValue="true",matchIfMissing=true) publicclassAspectLogAutoConfigurationimplementsPriorityOrdered{ protectedLoggerlogger=LoggerFactory.getLogger(getClass()); @Around("@annotation(com.oysept.autoconfiguration.aspectlog.AspectLog)") publicObjectisOpen(ProceedingJoinPointthisJoinPoint)throwsThrowable{ //执行方法名称 StringtaskName=thisJoinPoint.getSignature() .toString().substring( thisJoinPoint.getSignature() .toString().indexOf(""), thisJoinPoint.getSignature().toString().indexOf("(")); taskName=taskName.trim(); longtime=System.currentTimeMillis(); Objectresult=thisJoinPoint.proceed(); logger.info("==>aspectlogmethod:{}run:{}ms",taskName,(System.currentTimeMillis()-time)); returnresult; } @Override publicintgetOrder(){ //保证事务等切面先执行 returnInteger.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的工厂机制,在这个文件中定义的类,都会被自动加载。多个配置使用逗号分割,换行用\
第六步:使用mvninstall方式把该模块自动打包
四)测试aspectlog-spring-boot-starter打印日志配置
另外创建一个springboot_starter_test名称的maven项目
在pom中引入aspectlog-spring-boot-starter的jar
4.0.0 com.oysept springboot_starter_test 0.0.1-SNAPSHOT jar org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE 1.8 org.springframework.boot spring-boot-starter-web com.oysept aspectlog-spring-boot-starter 0.0.1-SNAPSHOT org.springframework.boot spring-boot-maven-plugin
创建一个application.yml,配置启动的端口,默认是8080
server:
port:8080
创建application启动类
packagecom.oysept; importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication publicclassTestSpringBootStarterApplication{ publicstaticvoidmain(String[]args){ SpringApplication.run(TestSpringBootStarterApplication.class,args); } }
创建测试AspectLog功能controller
packagecom.oysept.controller; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RequestMethod; importorg.springframework.web.bind.annotation.RequestParam; importorg.springframework.web.bind.annotation.RestController; importcom.oysept.autoconfiguration.aspectlog.AspectLog; @RestController publicclassGetController{ /** *访问地址:http://localhost:8080/test/starter/aspectlog?param=TTTEEESSSTTT *@return */ @AspectLog @RequestMapping(value="/test/starter/aspectlog",method=RequestMethod.GET) publicStringtestStarterAspectLog(@RequestParam(value="param")Stringparam){ System.out.println("==>/test/starter/aspectlog,param:"+param); //处理业务逻辑 return"/test/starter/aspectlogSUCCESS!"; } }
在想要打印日志的方法上,使用@AspectLog注解
启动TestSpringBootStarterApplication中的main方法
在浏览器中输入:http://localhost:8080/test/starter/aspectlog?param=TTTEEESSSTTT
然后在控制台查看效果:
以上这篇SpringBoot2.1.x,创建自己的spring-boot-starter自动配置模块操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。