您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

Springboot插件如何开发

2024/3/28 21:15:18发布7次查看
一 背景项目新增监控系统,对各个系统进行监控接口调用情况,初期的时候是在各个项目公共引用的依赖包里面新增aop切面来完成对各个系统的接口调用进行监控,但是这样有缺点,一是不同项目的接口路径不同,导致aop切面要写多个切面路径,二是一些不需要进行监控的系统,因为引入了公共包也被监控了,这样侵入性就太强了。为了解决这个问题,就可以通过springboot的可插拔属性了。
二 监控日志插件开发1 新建aop切面执行类monitorloginterceptor@slf4jpublic class monitorloginterceptor extends midexpandspringmethodinterceptor<monitoraspectadviceproperties> { @override public object invoke(methodinvocation methodinvocation) throws throwable { object result = null; httpservletrequest request = ((servletrequestattributes) requestcontextholder.getrequestattributes()).getrequest(); //拿到请求的url string requesturi = request.getrequesturi(); if (stringutils.isempty(requesturi)) { return result; } try { result = methodinvocation.proceed(); } catch (exception e) { buildrecorddata(methodinvocation, result, requesturi, e); throw e; } //参数数组 buildrecorddata(methodinvocation, result, requesturi, null); return result;
我们可以看到它实现了midexpandspringmethodinterceptor<t>
@slf4jpublic abstract class midexpandspringmethodinterceptor<t> implements methodinterceptor { @setter @getter protected t properties; /** * 主动注册,生成aop工厂类定义对象 */ protected string getexpression() { return null; } @suppresswarnings({"unchecked"}) public abstractbeandefinition doinitiativeregister(properties properties) { string expression = stringutils.isnotblank(this.getexpression()) ? this.getexpression() : properties.getproperty("expression"); if (stringutils.isblank(expression)) { log.warn("中台springaop插件 " + this.getclass().getsimplename() + " 缺少对应的配置文件 或者 是配置的拦截路径为空 导致初始化跳过"); return null; } beandefinitionbuilder definition = beandefinitionbuilder.genericbeandefinition(aspectjexpressionpointcutadvisor.class); this.setproperties((t) jsonutil.tobean(jsonutil.tojson(properties), getproxyclasst())); definition.addpropertyvalue("advice", this); definition.addpropertyvalue("expression", expression); return definition.getbeandefinition(); } /** * 获取代理类上的泛型t * 单泛型 不支持多泛型嵌套 */ private class<?> getproxyclasst() { type genericsuperclass = this.getclass().getgenericsuperclass(); parameterizedtype parameterizedtype = (parameterizedtype) genericsuperclass; return (class<?>) parameterizedtype.getactualtypearguments()[0]; }}
而最终是实现了methodinterceptor,这个接口是 方法拦截器,用于spring aop编程中的动态代理.实现该接口可以对需要增强的方法进行增强.
我们注意到我的切面执行类并没有增加任何@compont和@service等将类注入到spring的bean中的方法,那他是怎么被注入到bean中的呢,因为使用了spi机制
spi机制的实现在项目的资源文件目录中,增加spring.factories文件,内容为
com.dst.mid.common.expand.springaop.midexpandspringmethodinterceptor=\
com.dst.mid.monitor.intercept.monitorloginterceptor
这样就可以在启动过程直接被注册,并且被放到spring容器中了。还有一个问题就是,切面执行类有了,切面在哪里呢。
@configuration@slf4j@import(midexpandspringaopautostarter.class)public class midexpandspringaopautostarter implements importbeandefinitionregistrar { private static final string bean_name_format = "%s%sadvisor"; private static final string os = "os.name"; private static final string windows = "windows"; @sneakythrows @suppresswarnings({"rawtypes"}) @override public void registerbeandefinitions(annotationmetadata importingclassmetadata, beandefinitionregistry registry) { // 1 获取midexpandspringmethodinterceptor类的所有实现集合 list<midexpandspringmethodinterceptor> list = springfactoriesloader.loadfactories(midexpandspringmethodinterceptor.class, null); if (!collectionutils.isempty(list)) { string expandpath; properties properties; beandefinition beandefinition; // 2 遍历类的所有实现集合 for (midexpandspringmethodinterceptor item : list) { // 3 获取资源文件名称 资源文件中存储需要加入配置的 expandpath = getexpandpath(item.getclass()); // 4 加载资源文件 properties = propertiesloaderutils.loadallproperties(expandpath + ".properties"); // 5 赋值beandefinition为aspectjexpressionpointcutadvisor if (objects.nonnull(beandefinition = item.doinitiativeregister(properties))) { // 6 向容器中注册类 注意这个beanname是不存在的,但是他赋值beandefinition为aspectjexpressionpointcutadvisor是动态代理动态生成代理类所以不会报错 registry.registerbeandefinition(string.format(bean_name_format, expandpath, item.getclass().getsimplename()), beandefinition); } } } } /** * 获取资源文件名称 */ private static string getexpandpath(class<?> clazz) { string[] split = clazz.getprotectiondomain().getcodesource().getlocation().getpath().split("/"); if (system.getproperty(os).touppercase().contains(windows)) { return split[split.length - 3]; } else { return string.join("-", arrays.aslist(split[split.length - 1].split("-")).sublist(0, 4)); } }}
这个就是切面注册类的处理,首先实现了importbeandefinitionregistrar,实现他的registerbeandefinitions方法可以将想要注册的类放入spring容器中,看下他的实现
1 获取midexpandspringmethodinterceptor类的所有实现集合
2 遍历类的所有实现集合
3 获取资源文件名称 资源文件中存储需要加入配置的
4 加载资源文件
5 赋值beandefinition为aspectjexpressionpointcutadvisor
6 向容器中注册类 注意这个beanname是不存在的,但是他赋值beandefinition为aspectjexpressionpointcutadvisor是动态代理动态生成代理类所以不会报错
看到这里,还有一个问题importbeandefinitionregistrar实际上是将类注册到容器中,但是还需要一个步骤就是他要被容器扫描才行,以往的方式是项目中通过路径扫描,但是我们是插件,不能依赖于项目,而是通过自己的方式处理,这时候就需要用@import(midexpandspringaopautostarter.class)来处理了。
通过以上处理就实现了监控插件的处理,然后再使用时,只需要将这个项目引入到不同需要监控的项目上就可以了。
以上就是springboot插件如何开发的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录