|
| 1 | +package cn.jsmod2.core.plugin; |
| 2 | + |
| 3 | + |
| 4 | +import cn.jsmod2.core.utils.Utils; |
| 5 | +import org.apache.commons.lang3.StringUtils; |
| 6 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 7 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 8 | +import org.springframework.context.ApplicationContext; |
| 9 | +import org.springframework.context.ConfigurableApplicationContext; |
| 10 | +import org.springframework.stereotype.Component; |
| 11 | +import org.springframework.stereotype.Controller; |
| 12 | +import org.springframework.stereotype.Repository; |
| 13 | +import org.springframework.stereotype.Service; |
| 14 | +import org.springframework.util.ReflectionUtils; |
| 15 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 16 | +import org.springframework.web.bind.annotation.RestController; |
| 17 | +import org.springframework.web.servlet.mvc.method.RequestMappingInfo; |
| 18 | +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; |
| 19 | + |
| 20 | +import java.lang.reflect.Method; |
| 21 | +import java.lang.reflect.Modifier; |
| 22 | + |
| 23 | +public class SpringContextUtil { |
| 24 | + |
| 25 | + private static ApplicationContext applicationContext; |
| 26 | + |
| 27 | + public static void setApplicationContext(ApplicationContext applicationContext) { |
| 28 | + SpringContextUtil.applicationContext = applicationContext; |
| 29 | + } |
| 30 | + |
| 31 | + public static ApplicationContext getApplicationContext() { |
| 32 | + return applicationContext; |
| 33 | + } |
| 34 | + |
| 35 | + public static void loadBean(Class clz){ |
| 36 | + if(isSpringBean(clz)){ |
| 37 | + String beanName = StringUtils.uncapitalize(clz.getSimpleName()); |
| 38 | + ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) SpringContextUtil.getApplicationContext(); |
| 39 | + DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory)configurableApplicationContext.getBeanFactory(); |
| 40 | + BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clz); |
| 41 | + defaultListableBeanFactory.registerBeanDefinition(beanName, beanDefinitionBuilder.getRawBeanDefinition()); |
| 42 | + RequestMappingHandlerMapping mapping = configurableApplicationContext.getBean(RequestMappingHandlerMapping.class); |
| 43 | + Method method = ReflectionUtils.findMethod(RequestMappingHandlerMapping.class,"getMappingForMethod",Method.class,Class.class); |
| 44 | + method.setAccessible(true); |
| 45 | + Method[] methods = clz.getDeclaredMethods(); |
| 46 | + for(Method m:methods){ |
| 47 | + //RequestMapping("/aa")+RequestMapping("/name"); |
| 48 | + RequestMapping rm = m.getAnnotation(RequestMapping.class); |
| 49 | + String[] paths = rm.value(); |
| 50 | + for(String path:paths) { |
| 51 | + try { |
| 52 | + RequestMappingInfo mappingInfo = (RequestMappingInfo) method.invoke(mapping, m, path); |
| 53 | + mapping.registerMapping(mappingInfo, configurableApplicationContext.getBean(beanName), m); |
| 54 | + }catch (Exception e){ |
| 55 | + Utils.printException(e); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static boolean isSpringBean(Class clz){ |
| 64 | + if(clz == null)return false; |
| 65 | + if(clz.isInterface())return false; |
| 66 | + if(Modifier.isAbstract(clz.getModifiers()))return false; |
| 67 | + if (clz.getAnnotation(Controller.class)!=null)return true; |
| 68 | + if(clz.getAnnotation(RestController.class)!=null)return true; |
| 69 | + if(clz.getAnnotation(Component.class)!=null)return true; |
| 70 | + if(clz.getAnnotation(Repository.class)!=null)return true; |
| 71 | + if(clz.getAnnotation(Service.class)!=null)return true; |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments