Java代碼註冊spring的bean
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
* @Author: yzhang
* @Date: 2021/2/18 13:07
*/
public class CustomSuperInitializer implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware, ApplicationRunner {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();
private final Set<String> security = Collections.synchronizedSet(new HashSet<>());
private static ApplicationContext applicationContext;
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
//自定義bean
// customRegisterBean(registry, "permission", PermissionService.class);
/*********************************web.controller 返回包含頁面**************************************/
// registerController(registry, Gat1400ConfigController.class);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
private void customRegisterBean(BeanDefinitionRegistry registry, String name, Class<?> beanClass) {
BeanDefinition bean = new RootBeanDefinition(beanClass);
registry.registerBeanDefinition(name, bean);
}
private String customRegisterDefaultBean(BeanDefinitionRegistry registry, Class<?> beanClass) {
BeanDefinition bean = new RootBeanDefinition(beanClass);
String beanName = defaultBeanName(registry, bean);
registry.registerBeanDefinition(beanName, bean);
return beanName;
}
private String defaultBeanName(BeanDefinitionRegistry registry, BeanDefinition bean) {
return this.beanNameGenerator.generateBeanName(bean, registry);
}
private void registerController(BeanDefinitionRegistry registry, Class<?> beanClass) {
String beanName = customRegisterDefaultBean(registry, beanClass);
security.add(beanName);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public void run(ApplicationArguments args) throws Exception {
loopRegistryController();
}
//註冊Controller
private void registerController(String controllerBeanName) throws Exception {
final RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
if (requestMappingHandlerMapping != null) {
String handler = controllerBeanName;
Object controller = applicationContext.getBean(handler);
if (controller == null) {
return;
}
unregisterController(controllerBeanName);
//註冊Controller
Method method = requestMappingHandlerMapping.getClass().getSuperclass().getSuperclass().
getDeclaredMethod("detectHandlerMethods", Object.class);
//將private改爲可使用
method.setAccessible(true);
method.invoke(requestMappingHandlerMapping, handler);
}
}
//去掉Controller的Mapping
private void unregisterController(String controllerBeanName) {
final RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping)
applicationContext.getBean("requestMappingHandlerMapping");
if (requestMappingHandlerMapping != null) {
String handler = controllerBeanName;
Object controller = applicationContext.getBean(handler);
if (controller == null) {
return;
}
final Class<?> targetClass = controller.getClass();
ReflectionUtils.doWithMethods(targetClass, new ReflectionUtils.MethodCallback() {
public void doWith(Method method) {
Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
try {
Method createMappingMethod = RequestMappingHandlerMapping.class.
getDeclaredMethod("getMappingForMethod", Method.class, Class.class);
createMappingMethod.setAccessible(true);
RequestMappingInfo requestMappingInfo = (RequestMappingInfo)
createMappingMethod.invoke(requestMappingHandlerMapping, specificMethod, targetClass);
if (requestMappingInfo != null) {
requestMappingHandlerMapping.unregisterMapping(requestMappingInfo);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, ReflectionUtils.USER_DECLARED_METHODS);
}
}
private void loopRegistryController() throws Exception {
for (String beanName : security) {
registerController(beanName);
logger.info("beanName is : {} controller registry finish", beanName);
}
}
public static <T> String getBeanName(Class<T> tClass) {
String clazzName = tClass.getName();
return StringUtils.uncapitalize(clazzName.substring(clazzName.lastIndexOf(".") + 1));
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.