springboot运行时新增/更新外部接口的实现方法
最近有个需求:需要让现有springboot项目可以加载外部的jar包实现新增、更新接口逻辑。本着拿来主义的思维网上找了半天没有找到类似的东西,唯一有点相似的还是spring-loaded但是这个东西据我网上了解有如下缺点:
1、使用javaagent启动,个人倾向于直接使用pom依赖的方式
2、不支持新增字段,新增方法,估计也不支持mybatis的xml加载那些吧,没了解过
3、只适合在开发环境IDE中使用,没法生产使用
无奈之下,我只能自己实现一个了,我需要实现的功能如下
1、加载外部扩展jar包中的新接口,多次加载需要能完全更新
2、应该能加载mybatis、mybatis-plus中放sql的xml文件
3、应该能加载@Mapper修饰的mybatis的接口资源
4、需要能加载其它被spring管理的Bean资源
5、需要能在加载完成后更新swagger文档
总而言之就是要实现一个能够扩展完整接口的容器,其实类似于热加载也不同于热加载,热部署是监控本地的class文件的改变,然后使用自动重启或者重载,热部署领域比较火的就是devtools和jrebel,前者使用自动重启的方式,监控你的classes改变了,然后使用反射调用你的main方法重启一下,后者使用重载的方式,因为收费,具体原理也没了解过,估计就是不重启,只加载变过的class吧。而本文实现的是加载外部的jar包,这个jar包只要是个可访问的URL资源就可以了。虽然和热部署不一样,但是从方案上可以借鉴,本文就是使用重载的方式,也就是只会更新扩展包里的资源。
先来一个自定义的模块类加载器
packagecom.rdpaas.dynamic.core;
importorg.apache.commons.lang3.StringUtils;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importjava.io.ByteArrayOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.lang.reflect.InvocationTargetException;
importjava.lang.reflect.Method;
importjava.net.URL;
importjava.net.URLClassLoader;
importjava.security.AccessController;
importjava.security.PrivilegedExceptionAction;
importjava.util.Enumeration;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.jar.JarEntry;
importjava.util.jar.JarFile;
/**
*动态加载外部jar包的自定义类加载器
*@authorrongdi
*@date2021-03-06
*@bloghttps://www.cnblogs.com/rongdi
*/
publicclassModuleClassLoaderextendsURLClassLoader{
privateLoggerlogger=LoggerFactory.getLogger(ModuleClassLoader.class);
privatefinalstaticStringCLASS_SUFFIX=".class";
privatefinalstaticStringXML_SUFFIX=".xml";
privatefinalstaticStringMAPPER_SUFFIX="mapper/";
//属于本类加载器加载的jar包
privateJarFilejarFile;
privateMapclassBytesMap=newHashMap<>();
privateMap>classesMap=newHashMap<>();
privateMapxmlBytesMap=newHashMap<>();
publicModuleClassLoader(ClassLoaderclassLoader,URL...urls){
super(urls,classLoader);
URLurl=urls[0];
Stringpath=url.getPath();
try{
jarFile=newJarFile(path);
}catch(IOExceptione){
e.printStackTrace();
}
}
@Override
protectedClass>findClass(Stringname)throwsClassNotFoundException{
byte[]buf=classBytesMap.get(name);
if(buf==null){
returnsuper.findClass(name);
}
if(classesMap.containsKey(name)){
returnclassesMap.get(name);
}
/**
*这里应该算是骚操作了,我不知道市面上有没有人这么做过,反正我是想了好久,遇到各种因为spring要生成代理对象
*在他自己的AppClassLoader找不到原对象导致的报错,注意如果你限制你的扩展包你不会有AOP触碰到的类或者@Transactional这种
*会产生代理的类,那么其实你不用这么骚,直接在这里调用defineClass把字节码装载进去就行了,不会有什么问题,最多也就是
*在加载mybatis的xml那里前后加三句话,
*1、获取并使用一个变量保存当前线程类加载器
*2、将自定义类加载器设置到当前线程类加载器
*3、还原当前线程类加载器为第一步保存的类加载器
*这样之后mybatis那些xml里resultType,resultMap之类的需要访问扩展包的Class的就不会报错了。
*不过直接用现在这种骚操作,更加一劳永逸,不会有mybatis的问题了
*/
returnloadClass(name,buf);
}
/**
*使用反射强行将类装载的归属给当前类加载器的父类加载器也就是AppClassLoader,如果报ClassNotFoundException
*则递归装载
*@paramname
*@parambytes
*@return
*/
privateClass>loadClass(Stringname,byte[]bytes)throwsClassNotFoundException{
Object[]args=newObject[]{name,bytes,0,bytes.length};
try{
/**
*拿到当前类加载器的parent加载器AppClassLoader
*/
ClassLoaderparent=this.getParent();
/**
*首先要明确反射是万能的,仿造org.springframework.cglib.core.ReflectUtils的写法,强行获取被保护
*的方法defineClass的对象,然后调用指定类加载器的加载字节码方法,强行将加载归属塞给它,避免被spring的AOP或者@Transactional
*触碰到的类需要生成代理对象,而在AppClassLoader下加载不到外部的扩展类而报错,所以这里强行将加载外部扩展包的类的归属给
*AppClassLoader,让spring的cglib生成代理对象时可以加载到原对象
*/
MethodclassLoaderDefineClass=(Method)AccessController.doPrivileged(newPrivilegedExceptionAction(){
@Override
publicObjectrun()throwsException{
returnClassLoader.class.getDeclaredMethod("defineClass",
String.class,byte[].class,Integer.TYPE,Integer.TYPE);
}
});
if(!classLoaderDefineClass.isAccessible()){
classLoaderDefineClass.setAccessible(true);
}
return(Class>)classLoaderDefineClass.invoke(parent,args);
}catch(Exceptione){
if(einstanceofInvocationTargetException){
Stringmessage=((InvocationTargetException)e).getTargetException().getCause().toString();
/**
*无奈,明明ClassNotFoundException是个异常,非要抛个InvocationTargetException,导致
*我这里一个不太优雅的判断
*/
if(message.startsWith("java.lang.ClassNotFoundException")){
StringnotClassName=message.split(":")[1];
if(StringUtils.isEmpty(notClassName)){
thrownewClassNotFoundException(message);
}
notClassName=notClassName.trim();
byte[]bytes1=classBytesMap.get(notClassName);
if(bytes1==null){
thrownewClassNotFoundException(message);
}
/**
*递归装载未找到的类
*/
Class>notClass=loadClass(notClassName,bytes1);
if(notClass==null){
thrownewClassNotFoundException(message);
}
classesMap.put(notClassName,notClass);
returnloadClass(name,bytes);
}
}else{
logger.error("",e);
}
}
returnnull;
}
publicMapgetXmlBytesMap(){
returnxmlBytesMap;
}
/**
*方法描述初始化类加载器,保存字节码
*/
publicMapload(){
MapcacheClassMap=newHashMap<>();
//解析jar包每一项
Enumerationen=jarFile.entries();
InputStreaminput=null;
try{
while(en.hasMoreElements()){
JarEntryje=en.nextElement();
Stringname=je.getName();
//这里添加了路径扫描限制
if(name.endsWith(CLASS_SUFFIX)){
StringclassName=name.replace(CLASS_SUFFIX,"").replaceAll("/",".");
input=jarFile.getInputStream(je);
ByteArrayOutputStreambaos=newByteArrayOutputStream();
intbufferSize=4096;
byte[]buffer=newbyte[bufferSize];
intbytesNumRead=0;
while((bytesNumRead=input.read(buffer))!=-1){
baos.write(buffer,0,bytesNumRead);
}
byte[]classBytes=baos.toByteArray();
classBytesMap.put(className,classBytes);
}elseif(name.endsWith(XML_SUFFIX)&&name.startsWith(MAPPER_SUFFIX)){
input=jarFile.getInputStream(je);
ByteArrayOutputStreambaos=newByteArrayOutputStream();
intbufferSize=4096;
byte[]buffer=newbyte[bufferSize];
intbytesNumRead=0;
while((bytesNumRead=input.read(buffer))!=-1){
baos.write(buffer,0,bytesNumRead);
}
byte[]xmlBytes=baos.toByteArray();
xmlBytesMap.put(name,xmlBytes);
}
}
}catch(IOExceptione){
logger.error("",e);
}finally{
if(input!=null){
try{
input.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
//将jar中的每一个class字节码进行Class载入
for(Map.Entryentry:classBytesMap.entrySet()){
Stringkey=entry.getKey();
Class>aClass=null;
try{
aClass=loadClass(key);
}catch(ClassNotFoundExceptione){
logger.error("",e);
}
cacheClassMap.put(key,aClass);
}
returncacheClassMap;
}
publicMapgetClassBytesMap(){
returnclassBytesMap;
}
}
然后再来个加载mybatis的xml资源的类,本类解析xml部分是参考网上资料
packagecom.rdpaas.dynamic.core;
importorg.apache.ibatis.builder.xml.XMLMapperBuilder;
importorg.apache.ibatis.builder.xml.XMLMapperEntityResolver;
importorg.apache.ibatis.executor.ErrorContext;
importorg.apache.ibatis.executor.keygen.SelectKeyGenerator;
importorg.apache.ibatis.io.Resources;
importorg.apache.ibatis.mapping.MappedStatement;
importorg.apache.ibatis.parsing.XNode;
importorg.apache.ibatis.parsing.XPathParser;
importorg.apache.ibatis.session.Configuration;
importorg.apache.ibatis.session.SqlSessionFactory;
importorg.mybatis.spring.mapper.MapperFactoryBean;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importjava.io.ByteArrayInputStream;
importjava.lang.reflect.Field;
importjava.util.*;
/**
*mybatis的mapper.xml和@Mapper加载类
*@authorrongdi
*@date2021-03-06
*@bloghttps://www.cnblogs.com/rongdi
*/
publicclassMapperLoader{
privateLoggerlogger=LoggerFactory.getLogger(MapperLoader.class);
privateConfigurationconfiguration;
/**
*刷新外部mapper,包括文件和@Mapper修饰的接口
*@paramsqlSessionFactory
*@paramxmlBytesMap
*@return
*/
publicMaprefresh(SqlSessionFactorysqlSessionFactory,MapxmlBytesMap){
Configurationconfiguration=sqlSessionFactory.getConfiguration();
this.configuration=configuration;
/**
*这里用来区分mybatis-plus和mybatis,mybatis-plus的Configuration是继承自mybatis的子类
*/
booleanisSupper=configuration.getClass().getSuperclass()==Configuration.class;
MapmapperMap=newHashMap<>();
try{
/**
*遍历外部传入的xml字节码map
*/
for(Map.Entryentry:xmlBytesMap.entrySet()){
Stringresource=entry.getKey();
byte[]bytes=entry.getValue();
/**
*使用反射强行拿出configuration中的loadedResources属性
*/
FieldloadedResourcesField=isSupper
?configuration.getClass().getSuperclass().getDeclaredField("loadedResources")
:configuration.getClass().getDeclaredField("loadedResources");
loadedResourcesField.setAccessible(true);
SetloadedResourcesSet=((Set)loadedResourcesField.get(configuration));
/**
*加载mybatis中的xml
*/
XPathParserxPathParser=newXPathParser(newByteArrayInputStream(bytes),true,configuration.getVariables(),
newXMLMapperEntityResolver());
/**
*解析mybatis的xml的根节点,
*/
XNodecontext=xPathParser.evalNode("/mapper");
/**
*拿到namespace,namespace就是指Mapper接口的全限定名
*/
Stringnamespace=context.getStringAttribute("namespace");
Fieldfield=configuration.getMapperRegistry().getClass().getDeclaredField("knownMappers");
field.setAccessible(true);
/**
*拿到存放Mapper接口和对应代理子类的映射map,
*/
MapmapConfig=(Map)field.get(configuration.getMapperRegistry());
/**
*拿到Mapper接口对应的class对象
*/
ClassnsClass=Resources.classForName(namespace);
/**
*先删除各种
*/
mapConfig.remove(nsClass);
loadedResourcesSet.remove(resource);
configuration.getCacheNames().remove(namespace);
/**
*清掉namespace下各种缓存
*/
cleanParameterMap(context.evalNodes("/mapper/parameterMap"),namespace);
cleanResultMap(context.evalNodes("/mapper/resultMap"),namespace);
cleanKeyGenerators(context.evalNodes("insert|update|select|delete"),namespace);
cleanSqlElement(context.evalNodes("/mapper/sql"),namespace);
/**
*加载并解析对应xml
*/
XMLMapperBuilderxmlMapperBuilder=newXMLMapperBuilder(newByteArrayInputStream(bytes),
sqlSessionFactory.getConfiguration(),resource,
sqlSessionFactory.getConfiguration().getSqlFragments());
xmlMapperBuilder.parse();
/**
*构造MapperFactoryBean,注意这里一定要传入sqlSessionFactory,
*这块逻辑通过debug源码试验了很久
*/
MapperFactoryBeanmapperFactoryBean=newMapperFactoryBean(nsClass);
mapperFactoryBean.setSqlSessionFactory(sqlSessionFactory);
/**
*放入map,返回出去给ModuleApplication去加载
*/
mapperMap.put(namespace,mapperFactoryBean);
logger.info("refresh:'"+resource+"',success!");
}
returnmapperMap;
}catch(Exceptione){
logger.error("refresherror",e.getMessage());
}finally{
ErrorContext.instance().reset();
}
returnnull;
}
/**
*清理parameterMap
*
*@paramlist
*@paramnamespace
*/
privatevoidcleanParameterMap(Listlist,Stringnamespace){
for(XNodeparameterMapNode:list){
Stringid=parameterMapNode.getStringAttribute("id");
configuration.getParameterMaps().remove(namespace+"."+id);
}
}
/**
*清理resultMap
*
*@paramlist
*@paramnamespace
*/
privatevoidcleanResultMap(Listlist,Stringnamespace){
for(XNoderesultMapNode:list){
Stringid=resultMapNode.getStringAttribute("id",resultMapNode.getValueBasedIdentifier());
configuration.getResultMapNames().remove(id);
configuration.getResultMapNames().remove(namespace+"."+id);
clearResultMap(resultMapNode,namespace);
}
}
privatevoidclearResultMap(XNodexNode,Stringnamespace){
for(XNoderesultChild:xNode.getChildren()){
if("association".equals(resultChild.getName())||"collection".equals(resultChild.getName())
||"case".equals(resultChild.getName())){
if(resultChild.getStringAttribute("select")==null){
configuration.getResultMapNames()
.remove(resultChild.getStringAttribute("id",resultChild.getValueBasedIdentifier()));
configuration.getResultMapNames().remove(namespace+"."
+resultChild.getStringAttribute("id",resultChild.getValueBasedIdentifier()));
if(resultChild.getChildren()!=null&&!resultChild.getChildren().isEmpty()){
clearResultMap(resultChild,namespace);
}
}
}
}
}
/**
*清理selectKey
*
*@paramlist
*@paramnamespace
*/
privatevoidcleanKeyGenerators(Listlist,Stringnamespace){
for(XNodecontext:list){
Stringid=context.getStringAttribute("id");
configuration.getKeyGeneratorNames().remove(id+SelectKeyGenerator.SELECT_KEY_SUFFIX);
configuration.getKeyGeneratorNames().remove(namespace+"."+id+SelectKeyGenerator.SELECT_KEY_SUFFIX);
CollectionmappedStatements=configuration.getMappedStatements();
Listobjects=newArrayList<>();
Iteratorit=mappedStatements.iterator();
while(it.hasNext()){
Objectobject=it.next();
if(objectinstanceofMappedStatement){
MappedStatementmappedStatement=(MappedStatement)object;
if(mappedStatement.getId().equals(namespace+"."+id)){
objects.add(mappedStatement);
}
}
}
mappedStatements.removeAll(objects);
}
}
/**
*清理sql节点缓存
*
*@paramlist
*@paramnamespace
*/
privatevoidcleanSqlElement(Listlist,Stringnamespace){
for(XNodecontext:list){
Stringid=context.getStringAttribute("id");
configuration.getSqlFragments().remove(id);
configuration.getSqlFragments().remove(namespace+"."+id);
}
}
}
上面需要注意的是,处理好xml还需要将XXMapper接口也放入spring容器中,但是接口是没办法直接转成spring的BeanDefinition的,因为接口没办法实例化,而BeanDefinition作为对象的模板,肯定不允许接口直接放进去,通过看mybatis-spring源码,可以看出这些接口都会被封装成MapperFactoryBean放入spring容器中实例化时就调用getObject方法生成Mapper的代理对象。下面就是将各种资源装载spring容器的代码了
packagecom.rdpaas.dynamic.core;
importcom.rdpaas.dynamic.utils.ReflectUtil;
importcom.rdpaas.dynamic.utils.SpringUtil;
importorg.apache.ibatis.session.SqlSessionFactory;
importorg.springframework.beans.factory.config.BeanDefinition;
importorg.springframework.beans.factory.support.BeanDefinitionBuilder;
importorg.springframework.beans.factory.support.DefaultListableBeanFactory;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.ConfigurableApplicationContext;
importorg.springframework.plugin.core.PluginRegistry;
importorg.springframework.util.MultiValueMap;
importorg.springframework.util.StringUtils;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.method.HandlerMethod;
importorg.springframework.web.servlet.mvc.method.RequestMappingInfo;
importorg.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
importspringfox.documentation.builders.ApiInfoBuilder;
importspringfox.documentation.builders.PathSelectors;
importspringfox.documentation.builders.RequestHandlerSelectors;
importspringfox.documentation.builders.ResponseMessageBuilder;
importspringfox.documentation.schema.ModelRef;
importspringfox.documentation.service.ApiInfo;
importspringfox.documentation.service.Contact;
importspringfox.documentation.service.ResponseMessage;
importspringfox.documentation.spi.DocumentationType;
importspringfox.documentation.spi.service.DocumentationPlugin;
importspringfox.documentation.spring.web.plugins.Docket;
importspringfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper;
importspringfox.documentation.spring.web.plugins.DocumentationPluginsManager;
importjava.lang.reflect.Field;
importjava.lang.reflect.Method;
importjava.net.URL;
importjava.util.*;
/**
*基于spring的应用上下文提供一些工具方法
*@authorrongdi
*@date2021-03-06
*@bloghttps://www.cnblogs.com/rongdi
*/
publicclassModuleApplication{
privatefinalstaticStringSINGLETON="singleton";
privatefinalstaticStringDYNAMIC_DOC_PACKAGE="dynamic.swagger.doc.package";
privateSetextMappingInfos=newHashSet<>();
privateApplicationContextapplicationContext;
/**
*使用spring上下文拿到指定beanName的对象
*/
publicTgetBean(StringbeanName){
return(T)((ConfigurableApplicationContext)applicationContext).getBeanFactory().getBean(beanName);
}
/**
*使用spring上下文拿到指定类型的对象
*/
publicTgetBean(Classclazz){
return(T)((ConfigurableApplicationContext)applicationContext).getBeanFactory().getBean(clazz);
}
/**
*加载一个外部扩展jar,包括springmvc接口资源,mybatis的@mapper和mapper.xml和springbean等资源
*@paramurljarurl
*@paramapplicationContextspringcontext
*@paramsqlSessionFactorymybatis的session工厂
*/
publicvoidreloadJar(URLurl,ApplicationContextapplicationContext,SqlSessionFactorysqlSessionFactory)throwsException{
this.applicationContext=applicationContext;
URL[]urls=newURL[]{url};
/**
*这里实际上是将spring的ApplicationContext的类加载器当成parent传给了自定义类加载器,很明自定义的子类加载器自己加载
*的类,parent类加载器直接是获取不到的,所以在自定义类加载器做了特殊的骚操作
*/
ModuleClassLoadermoduleClassLoader=newModuleClassLoader(applicationContext.getClassLoader(),urls);
/**
*使用模块类加载器加载url资源的jar包,直接返回类的全限定名和Class对象的映射,这些Class对象是
*jar包里所有.class结尾的文件加载后的结果,同时mybatis的xml加载后,无奈的放入了
*moduleClassLoader.getXmlBytesMap(),不是很优雅
*/
MapclassMap=moduleClassLoader.load();
MapperLoadermapperLoader=newMapperLoader();
/**
*刷新mybatis的xml和Mapper接口资源,Mapper接口其实就是xml的namespace
*/
MapextObjMap=mapperLoader.refresh(sqlSessionFactory,moduleClassLoader.getXmlBytesMap());
/**
*将各种资源放入spring容器
*/
registerBeans(applicationContext,classMap,extObjMap);
}
/**
*装载bean到spring中
*
*@paramapplicationContext
*@paramcacheClassMap
*/
publicvoidregisterBeans(ApplicationContextapplicationContext,MapcacheClassMap,MapextObjMap)throwsException{
/**
*将applicationContext转换为ConfigurableApplicationContext
*/
ConfigurableApplicationContextconfigurableApplicationContext=(ConfigurableApplicationContext)applicationContext;
/**
*获取bean工厂并转换为DefaultListableBeanFactory
*/
DefaultListableBeanFactorydefaultListableBeanFactory=(DefaultListableBeanFactory)configurableApplicationContext.getBeanFactory();
/**
*有一些对象想给spring管理,则放入spring中,如mybatis的@Mapper修饰的接口的代理类
*/
if(extObjMap!=null&&!extObjMap.isEmpty()){
extObjMap.forEach((beanName,obj)->{
/**
*如果已经存在,则销毁之后再注册
*/
if(defaultListableBeanFactory.containsSingleton(beanName)){
defaultListableBeanFactory.destroySingleton(beanName);
}
defaultListableBeanFactory.registerSingleton(beanName,obj);
});
}
for(Map.Entryentry:cacheClassMap.entrySet()){
StringclassName=entry.getKey();
Class>clazz=entry.getValue();
if(SpringUtil.isSpringBeanClass(clazz)){
//将变量首字母置小写
StringbeanName=StringUtils.uncapitalize(className);
beanName=beanName.substring(beanName.lastIndexOf(".")+1);
beanName=StringUtils.uncapitalize(beanName);
/**
*已经在spring容器就删了
*/
if(defaultListableBeanFactory.containsBeanDefinition(beanName)){
defaultListableBeanFactory.removeBeanDefinition(beanName);
}
/**
*使用spring的BeanDefinitionBuilder将Class对象转成BeanDefinition
*/
BeanDefinitionBuilderbeanDefinitionBuilder=BeanDefinitionBuilder.genericBeanDefinition(clazz);
BeanDefinitionbeanDefinition=beanDefinitionBuilder.getRawBeanDefinition();
//设置当前bean定义对象是单利的
beanDefinition.setScope(SINGLETON);
/**
*以指定beanName注册上面生成的BeanDefinition
*/
defaultListableBeanFactory.registerBeanDefinition(beanName,beanDefinition);
}
}
/**
*刷新springmvc,让新增的接口生效
*/
refreshMVC((ConfigurableApplicationContext)applicationContext);
}
/**
*刷新springMVC,这里花了大量时间调试,找不到开放的方法,只能取个巧,在更新RequestMappingHandlerMapping前先记录之前
*所有RequestMappingInfo,记得这里一定要copy一下,然后刷新后再记录一次,计算出差量存放在成员变量Set中,然后每次开头判断
*差量那里是否有内容,有就先unregiester掉
*/
privatevoidrefreshMVC(ConfigurableApplicationContextapplicationContext)throwsException{
Mapmap=applicationContext.getBeanFactory().getBeansOfType(RequestMappingHandlerMapping.class);
/**
*先拿到RequestMappingHandlerMapping对象
*/
RequestMappingHandlerMappingmappingHandlerMapping=map.get("requestMappingHandlerMapping");
/**
*重新注册mapping前先判断是否存在了,存在了就先unregister掉
*/
if(!extMappingInfos.isEmpty()){
for(RequestMappingInforequestMappingInfo:extMappingInfos){
mappingHandlerMapping.unregisterMapping(requestMappingInfo);
}
}
/**
*获取刷新前的RequestMappingInfo
*/
MappreMappingInfoHandlerMethodMap=mappingHandlerMapping.getHandlerMethods();
/**
*这里注意一定要拿到拷贝,不然刷新后内容就一致了,就没有差量了
*/
SetpreRequestMappingInfoSet=newHashSet(preMappingInfoHandlerMethodMap.keySet());
/**
*这里是刷新springmvc上下文
*/
applicationContext.getBeanFactory().getBeansOfType(RequestMappingHandlerMapping.class)
.forEach((key,value)->{
value.afterPropertiesSet();
});
/**
*获取刷新后的RequestMappingInfo
*/
MapafterMappingInfoHandlerMethodMap=mappingHandlerMapping.getHandlerMethods();
SetafterRequestMappingInfoSet=afterMappingInfoHandlerMethodMap.keySet();
/**
*填充差量部分RequestMappingInfo
*/
fillSurplusRequestMappingInfos(preRequestMappingInfoSet,afterRequestMappingInfoSet);
/**
*这里真的是不讲武德了,每次调用value.afterPropertiesSet();如下urlLookup都会产生重复,暂时没找到开放方法去掉重复,这里重复会导致
*访问的时候报错Ambiguoushandlermethodsmappedfor
*目标是去掉RequestMappingHandlerMapping->RequestMappingInfoHandlerMapping->AbstractHandlerMethodMapping
*->mappingRegistry->urlLookup重复的RequestMappingInfo,这里的.getClass().getSuperclass().getSuperclass()相信会
*很懵逼,如果单独通过getClass().getDeclaredMethod("getMappingRegistry",newClass[]{})是无论如何都拿不到父类的非public非
*protected方法的,因为这个方法不属于子类,只有父类才可以访问到,只有你拿得到你才有资格不讲武德的使用method.setAccessible(true)强行
*访问
*/
Methodmethod=ReflectUtil.getMethod(mappingHandlerMapping,"getMappingRegistry",newClass[]{});
method.setAccessible(true);
ObjectmappingRegistryObj=method.invoke(mappingHandlerMapping,newObject[]{});
Fieldfield=mappingRegistryObj.getClass().getDeclaredField("urlLookup");
field.setAccessible(true);
MultiValueMapmultiValueMap=(MultiValueMap)field.get(mappingRegistryObj);
multiValueMap.forEach((key,list)->{
clearMultyMapping(list);
});
}
/**
*填充差量的RequestMappingInfo,因为已经重写过hashCode和equals方法所以可以直接用对象判断是否存在
*@parampreRequestMappingInfoSet
*@paramafterRequestMappingInfoSet
*/
privatevoidfillSurplusRequestMappingInfos(SetpreRequestMappingInfoSet,SetafterRequestMappingInfoSet){
for(RequestMappingInforequestMappingInfo:afterRequestMappingInfoSet){
if(!preRequestMappingInfoSet.contains(requestMappingInfo)){
extMappingInfos.add(requestMappingInfo);
}
}
}
/**
*简单的逻辑,删除List里重复的RequestMappingInfo,已经写了toString,直接使用mappingInfo.toString()就可以区分重复了
*@parammappingInfos
*/
privatevoidclearMultyMapping(ListmappingInfos){
SetcontainsList=newHashSet<>();
for(Iteratoriter=mappingInfos.iterator();iter.hasNext();){
RequestMappingInfomappingInfo=iter.next();
Stringflag=mappingInfo.toString();
if(containsList.contains(flag)){
iter.remove();
}else{
containsList.add(flag);
}
}
}
}
上述有两个地方很虐心,第一个就是刷新springmvc那里,提供的刷新springmvc上下文的方式不友好不说,刷新上下文后RequestMappingHandlerMapping->RequestMappingInfoHandlerMapping->AbstractHandlerMethodMapping->mappingRegistry->urlLookup属性中会存在重复的路径如下
上述是我故意两次加载同一个jar包后第二次走到刷新springmvc之后,可以看到扩展包里的接口,由于unregister所以没有发现重复,那些重复的路径都是本身服务的接口,由于没有unregister所以出现了大把重复,如果这个时候访问重复的接口,会出现如下错误
java.lang.IllegalStateException:Ambiguoushandlermethodsmappedfor'/error':
意思就是匹配到了多个相同的路径解决方法有两种,第一种就是所有RequestMappingInfo都先unregister再刷新,第二种就是我调试很久确认就只有urlLookup会发生冲重复,所以如下使用万能的反射强行修改值,其实不要排斥使用反射,spring源码中大量使用反射去强行调用方法,比如org.springframework.cglib.core.ReflectUtils类摘抄如下:
classLoaderDefineClass=(Method)AccessController.doPrivileged(newPrivilegedExceptionAction(){
publicObjectrun()throwsException{
returnClassLoader.class.getDeclaredMethod("defineClass",
String.class,byte[].class,Integer.TYPE,Integer.TYPE,ProtectionDomain.class);
}
});
classLoaderDefineClassMethod=classLoaderDefineClass;
//Classicoption:protectedClassLoader.defineClassmethod
if(c==null&&classLoaderDefineClassMethod!=null){
if(protectionDomain==null){
protectionDomain=PROTECTION_DOMAIN;
}
Object[]args=newObject[]{className,b,0,b.length,protectionDomain};
try{
if(!classLoaderDefineClassMethod.isAccessible()){
classLoaderDefineClassMethod.setAccessible(true);
}
c=(Class)classLoaderDefineClassMethod.invoke(loader,args);
}
catch(InvocationTargetExceptionex){
thrownewCodeGenerationException(ex.getTargetException());
}
catch(Throwableex){
//FallthroughifsetAccessiblefailswithInaccessibleObjectExceptiononJDK9+
//(onthemodulepathand/orwithaJVMbootstrappedwith--illegal-access=deny)
if(!ex.getClass().getName().endsWith("InaccessibleObjectException")){
thrownewCodeGenerationException(ex);
}
}
}
如上可以看出来像spring这样的名家也一样也很不讲武德,个人认为反射本身就是用来给我们打破规则用的,只有打破规则才会有创新,所以大胆使用反射吧。只要不遇到final的属性,反射是万能的,哈哈!所以我使用反射强行删除重复的代码如下:
/**
*这里真的是不讲武德了,每次调用value.afterPropertiesSet();如下urlLookup都会产生重复,暂时没找到开放方法去掉重复,这里重复会导致
*访问的时候报错Ambiguoushandlermethodsmappedfor
*目标是去掉RequestMappingHandlerMapping->RequestMappingInfoHandlerMapping->AbstractHandlerMethodMapping
*->mappingRegistry->urlLookup重复的RequestMappingInfo,这里的.getClass().getSuperclass().getSuperclass()相信会
*很懵逼,如果单独通过getClass().getDeclaredMethod("getMappingRegistry",newClass[]{})是无论如何都拿不到父类的非public非
*protected方法的,因为这个方法不属于子类,只有父类才可以访问到,只有你拿得到你才有资格不讲武德的使用method.setAccessible(true)强行
*访问
*/
Methodmethod=ReflectUtil.getMethod(mappingHandlerMapping,"getMappingRegistry",newClass[]{});
method.setAccessible(true);
ObjectmappingRegistryObj=method.invoke(mappingHandlerMapping,newObject[]{});
Fieldfield=mappingRegistryObj.getClass().getDeclaredField("urlLookup");
field.setAccessible(true);
MultiValueMapmultiValueMap=(MultiValueMap)field.get(mappingRegistryObj);
multiValueMap.forEach((key,list)->{
clearMultyMapping(list);
});
/**
*简单的逻辑,删除List里重复的RequestMappingInfo,已经写了toString,直接使用mappingInfo.toString()就可以区分重复了
*@parammappingInfos
*/
privatevoidclearMultyMapping(ListmappingInfos){
SetcontainsList=newHashSet<>();
for(Iteratoriter=mappingInfos.iterator();iter.hasNext();){
RequestMappingInfomappingInfo=iter.next();
Stringflag=mappingInfo.toString();
if(containsList.contains(flag)){
iter.remove();
}else{
containsList.add(flag);
}
}
}
还有个虐心的地方是刷新swagger文档的地方,这个swagger只有需要做这个需求时才知道,他封装的有多菜,根本没有刷新相关的方法,也没有可以控制的入口,真的是没办法。下面贴出我解决刷新swagger文档的调试过程,使用过swagger2的朋友们都知道,要想在springboot集成swagger2主要需要编写的配置代码如下
@Configuration
@EnableSwagger2
publicclassSwaggerConfig{
//swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
@Bean
publicDocketcreateRestApi(){
ListresponseMessageList=newArrayList<>();
responseMessageList.add(newResponseMessageBuilder().code(200).message("成功").responseModel(newModelRef("Payload")).build());
Docketdocket=newDocket(DocumentationType.SWAGGER_2)
.globalResponseMessage(RequestMethod.GET,responseMessageList)
.globalResponseMessage(RequestMethod.DELETE,responseMessageList)
.globalResponseMessage(RequestMethod.POST,responseMessageList)
.apiInfo(apiInfo()).select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.xxx")).paths(PathSelectors.any()).build();
returndocket;
}
//构建api文档的详细信息函数,注意这里的注解引用的是哪个
privateApiInfoapiInfo(){
returnnewApiInfoBuilder()
//页面标题
.title("使用Swagger2构建RESTfulAPI")
//创建人
.contact(newContact("rongdi","https://www.cnblogs.com/rongdi","495194630@qq.com"))
//版本号
.version("1.0")
//描述
.description("api管理").build();
}
}
而访问swagger的文档请求的是如下接口/v2/api-docs
通过调试可以找到swagger2就是通过实现了SmartLifecycle接口的DocumentationPluginsBootstrapper类,当spring容器加载所有bean并完成初始化之后,会回调实现该接口的类(DocumentationPluginsBootstrapper)中对应的方法start()方法,下面会介绍怎么找到这里的。
接着循环DocumentationPlugin集合去处理文档
接着放入DocumentationCache中
然后再回到swagger接口的类那里,实际上就是从这个DocumentationCache里获取到Documention
‘如果找不到解决问题的入口,我们至少可以找到访问文档的上面这个接口地址(出口),发现接口返回的文档json内容是从DocumentationCache里获取,那么我们很明显可以想到肯定有地方存放数据到这个DocumentationCache里,然后其实我们可以直接在addDocumentation方法里打个断点,然后看调试左侧的运行方法栈信息,就可以很明确的看到调用链路了
再回看我们接入swagger2的时候写的配置代码
//swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
@Bean
publicDocketcreateRestApi(){
ListresponseMessageList=newArrayList<>();
responseMessageList.add(newResponseMessageBuilder().code(200).message("成功").responseModel(newModelRef("Payload")).build());
Docketdocket=newDocket(DocumentationType.SWAGGER_2)
.globalResponseMessage(RequestMethod.GET,responseMessageList)
.globalResponseMessage(RequestMethod.DELETE,responseMessageList)
.globalResponseMessage(RequestMethod.POST,responseMessageList)
.apiInfo(apiInfo()).select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.xxx")).paths(PathSelectors.any()).build();
returndocket;
}
然后再看看下图,应该终于知道咋回事了吧,其实Docket对象我们仅仅需要关心的是basePackage,我们扩展jar包大概率接口所在的包和现有包不一样,所以我们需要新增一个Docket插件,并加入DocumentationPlugin集合,然后调用DocumentationPluginsBootstrapper的stop()方法清掉缓存,再调用start()再次开始解析
具体实现代码如下
/**
*刷新springMVC,这里花了大量时间调试,找不到开放的方法,只能取个巧,在更新RequestMappingHandlerMapping前先记录之前
*所有RequestMappingInfo,记得这里一定要copy一下,然后刷新后再记录一次,计算出差量存放在成员变量Set中,然后每次开头判断
*差量那里是否有内容,有就先unregiester掉
*/
privatevoidrefreshMVC(ConfigurableApplicationContextapplicationContext)throwsException{
Mapmap=applicationContext.getBeanFactory().getBeansOfType(RequestMappingHandlerMapping.class);
/**
*先拿到RequestMappingHandlerMapping对象
*/
RequestMappingHandlerMappingmappingHandlerMapping=map.get("requestMappingHandlerMapping");
/**
*重新注册mapping前先判断是否存在了,存在了就先unregister掉
*/
if(!extMappingInfos.isEmpty()){
for(RequestMappingInforequestMappingInfo:extMappingInfos){
mappingHandlerMapping.unregisterMapping(requestMappingInfo);
}
}
/**
*获取刷新前的RequestMappingInfo
*/
MappreMappingInfoHandlerMethodMap=mappingHandlerMapping.getHandlerMethods();
/**
*这里注意一定要拿到拷贝,不然刷新后内容就一致了,就没有差量了
*/
SetpreRequestMappingInfoSet=newHashSet(preMappingInfoHandlerMethodMap.keySet());
/**
*这里是刷新springmvc上下文
*/
applicationContext.getBeanFactory().getBeansOfType(RequestMappingHandlerMapping.class)
.forEach((key,value)->{
value.afterPropertiesSet();
});
/**
*获取刷新后的RequestMappingInfo
*/
MapafterMappingInfoHandlerMethodMap=mappingHandlerMapping.getHandlerMethods();
SetafterRequestMappingInfoSet=afterMappingInfoHandlerMethodMap.keySet();
/**
*填充差量部分RequestMappingInfo
*/
fillSurplusRequestMappingInfos(preRequestMappingInfoSet,afterRequestMappingInfoSet);
/**
*这里真的是不讲武德了,每次调用value.afterPropertiesSet();如下urlLookup都会产生重复,暂时没找到开放方法去掉重复,这里重复会导致
*访问的时候报错Ambiguoushandlermethodsmappedfor
*目标是去掉RequestMappingHandlerMapping->RequestMappingInfoHandlerMapping->AbstractHandlerMethodMapping
*->mappingRegistry->urlLookup重复的RequestMappingInfo,这里的.getClass().getSuperclass().getSuperclass()相信会
*很懵逼,如果单独通过getClass().getDeclaredMethod("getMappingRegistry",newClass[]{})是无论如何都拿不到父类的非public非
*protected方法的,因为这个方法不属于子类,只有父类才可以访问到,只有你拿得到你才有资格不讲武德的使用method.setAccessible(true)强行
*访问
*/
Methodmethod=ReflectUtil.getMethod(mappingHandlerMapping,"getMappingRegistry",newClass[]{});
method.setAccessible(true);
ObjectmappingRegistryObj=method.invoke(mappingHandlerMapping,newObject[]{});
Fieldfield=mappingRegistryObj.getClass().getDeclaredField("urlLookup");
field.setAccessible(true);
MultiValueMapmultiValueMap=(MultiValueMap)field.get(mappingRegistryObj);
multiValueMap.forEach((key,list)->{
clearMultyMapping(list);
});
/**
*刷新swagger文档
*/
refreshSwagger(applicationContext);
}
/**
*刷新swagger文档
*@paramapplicationContext
*@throwsException
*/
privatevoidrefreshSwagger(ConfigurableApplicationContextapplicationContext)throwsException{
/**
*获取扩展包swagger的地址接口扫描包,如果有配置则执行文档刷新操作
*/
StringextSwaggerDocPackage=applicationContext.getEnvironment().getProperty(DYNAMIC_DOC_PACKAGE);
if(!StringUtils.isEmpty(extSwaggerDocPackage)){
/**
*拿到swagger解析文档的入口类,真的不想这样,主要是根本不提供刷新和重新加载文档的方法,只能不讲武德了
*/
DocumentationPluginsBootstrapperbootstrapper=applicationContext.getBeanFactory().getBean(DocumentationPluginsBootstrapper.class);
/**
*不管愿不愿意,强行拿到属性得到documentationPluginsManager对象
*/
Fieldfield1=bootstrapper.getClass().getDeclaredField("documentationPluginsManager");
field1.setAccessible(true);
DocumentationPluginsManagerdocumentationPluginsManager=(DocumentationPluginsManager)field1.get(bootstrapper);
/**
*继续往下层拿documentationPlugins属性
*/
Fieldfield2=documentationPluginsManager.getClass().getDeclaredField("documentationPlugins");
field2.setAccessible(true);
PluginRegistrypluginRegistrys=(PluginRegistry)field2.get(documentationPluginsManager);
/**
*拿到最关键的文档插件集合,所有逻辑文档解析逻辑都在插件中
*/
Listdockets=pluginRegistrys.getPlugins();
/**
*真的不能怪我,好端端,你还搞个不能修改的集合,强行往父类递归拿到unmodifiableList的list属性
*/
FieldunModList=ReflectUtil.getField(dockets,"list");
unModList.setAccessible(true);
ListmodifyerList=(List)unModList.get(dockets);
/**
*这下老实了吧,把自己的Docket加入进去,这里的groupName为dynamic
*/
modifyerList.add(createRestApi(extSwaggerDocPackage));
/**
*清空罪魁祸首DocumentationCache缓存,不然就算再加载一次,获取文档还是从这个缓存中拿,不会完成更新
*/
bootstrapper.stop();
/**
*手动执行重新解析swagger文档
*/
bootstrapper.start();
}
}
publicDocketcreateRestApi(StringbasePackage){
ListresponseMessageList=newArrayList<>();
responseMessageList.add(newResponseMessageBuilder().code(200).message("成功").responseModel(newModelRef("Payload")).build());
Docketdocket=newDocket(DocumentationType.SWAGGER_2)
.groupName("dynamic")
.globalResponseMessage(RequestMethod.GET,responseMessageList)
.globalResponseMessage(RequestMethod.DELETE,responseMessageList)
.globalResponseMessage(RequestMethod.POST,responseMessageList)
.apiInfo(apiInfo()).select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage(basePackage)).paths(PathSelectors.any()).build();
returndocket;
}
/**
*构建api文档的详细信息函数
*/
privateApiInfoapiInfo(){
returnnewApiInfoBuilder()
//页面标题
.title("SpringBoot动态扩展")
//创建人
.contact(newContact("rongdi","https://www.cnblogs.com/rongdi","495194630@qq.com"))
//版本号
.version("1.0")
//描述
.description("api管理").build();
}
好了,下面给一下整个扩展功能的入口吧
packagecom.rdpaas.dynamic.config;
importcom.rdpaas.dynamic.core.ModuleApplication;
importorg.apache.ibatis.session.SqlSessionFactory;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importorg.springframework.beans.BeansException;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
importorg.springframework.boot.context.event.ApplicationStartedEvent;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.ApplicationContextAware;
importorg.springframework.context.ApplicationListener;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importjava.net.URL;
/**
*一切配置的入口
*@authorrongdi
*@date2021-03-06
*@bloghttps://www.cnblogs.com/rongdi
*/
@Configuration
publicclassDynamicConfigimplementsApplicationContextAware{
privatestaticfinalLoggerlogger=LoggerFactory.getLogger(DynamicConfig.class);
@Autowired
privateSqlSessionFactorysqlSessionFactory;
privateApplicationContextapplicationContext;
@Value("${dynamic.jar:/}")
privateStringdynamicJar;
@Bean
publicModuleApplicationmoduleApplication()throwsException{
returnnewModuleApplication();
}
@Override
publicvoidsetApplicationContext(ApplicationContextapplicationContext)throwsBeansException{
this.applicationContext=applicationContext;
}
/**
*随便找个事件ApplicationStartedEvent,用来reload外部的jar,其实直接在moduleApplication()方法也可以做
*这件事,但是为了验证容器初始化后再加载扩展包还可以生效,所以故意放在了这里。
*@return
*/
@Bean
@ConditionalOnProperty(prefix="dynamic",name="jar")
publicApplicationListenerapplicationListener1(){
return(ApplicationListener)event->{
try{
/**
*加载外部扩展jar
*/
moduleApplication().reloadJar(newURL(dynamicJar),applicationContext,sqlSessionFactory);
}catch(Exceptione){
logger.error("",e);
}
};
}
}
再给个开关注解
packagecom.rdpaas.dynamic.anno;
importcom.rdpaas.dynamic.config.DynamicConfig;
importorg.springframework.context.annotation.Import;
importjava.lang.annotation.*;
/**
*开启动态扩展的注解
*@authorrongdi
*@date2021-03-06
*@bloghttps://www.cnblogs.com/rongdi
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({DynamicConfig.class})
public@interfaceEnableDynamic{
}
好了,至此核心代码和功能都分享完了,详细源码和使用说明见github:https://github.com/rongdi/springboot-dynamic
到此这篇关于springboot运行时新增/更新外部接口的实现方法的文章就介绍到这了,更多相关springboot外部接口内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。