废话不说,首先须要得到spring的上下文ApplicationContext的实现对象:ApplicationContext context。java
而后在 context对象中找到RequestMappingHandlerMapping对象如代码:spring
RequestMappingHandlerMapping requestMappingHandlerMapping=Context.getBean(RequestMappingHandlerMapping.class);
你们查看这个对象能够看出有一个方法叫作:registerMapping。没错这个就是注册requestmapping的方法。但是参数真的很难写。因此我查看源码后得出以下代码:springboot
Method getMappingForMethod =ReflectionUtils.findMethod(RequestMappingHandlerMapping.class, "getMappingForMethod",Method.class,Class.class); getMappingForMethod.setAccessible(true);
经过spring的反射工具类得到getMappingForMethod方法,因为这个方法是个私有的因此我须要设置一下使用权限。这时候咱们就能够经过反射调用这个方法了,如代码:app
RequestMappingInfo mapping_info = (RequestMappingInfo) getMappingForMethod.invoke(requestMappingHandlerMapping, m_d,entry.getValue());
经过查看源码咱们能够看出getMappingForMethod方法须要2个参数,分别是Method method, Class<?> handlerType。也就是须要注册进去的方法(method)和这个方法所在类的类型(class),按照参数传入便可。而后咱们就能够调用registerMapping方法进行注册了,如代码:spring-boot
requestMappingHandlerMapping.registerMapping(mapping_info, entry.getValue().newInstance(),m_d);
第一个参数是刚才咱们经过反射实例化的RequestMappingInfo对象,第二个RequestMappingInfo中第二个参数的实例化,第三个仍是RequestMappingInfo的第一个method类型对象。搞定!!可是这样的实现主要是为了热加载外部jar包,将外部jar包的requestmapping注册进spring,因此剩余的代码之后我会贴出来的工具
源码如今已经整理完毕,一共分为三个jar包,主jar包springboot-framework.jar是spring-boot项目,引入和启动主要代码包z-jarload.jar,test-mapping.jar是用于测试的须要被热加载的包。测试
配置工做:在application.properties配置文件中配置framework.z.jarurl=E\:/jar1 选择合适的路径,在此路径下是须要热加载的jar包。jar包名称必须用-mapping.jar结尾。jar包内带有@RestController注解的类必须在com.z.mapping包下。url
https://pan.baidu.com/s/1OrhLr98pSIW_c2ZcCf-hwg 密码:3aeicode