|
|
|
@ -1,12 +1,19 @@
|
|
|
|
|
package com.docus.server.tool;
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
|
|
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
|
|
|
|
|
import org.springframework.beans.factory.config.BeanDefinition;
|
|
|
|
|
import org.springframework.beans.factory.config.SingletonBeanRegistry;
|
|
|
|
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
|
|
|
|
import org.springframework.beans.factory.support.GenericBeanDefinition;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.util.ReflectionUtils;
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -65,6 +72,39 @@ public class SpringUtils implements ApplicationContextAware {
|
|
|
|
|
return applicationContext.getBeansOfType(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static <T> T getBean(String beanName, Class<T> beanClass) {
|
|
|
|
|
return applicationContext.getBean(beanName, beanClass);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static <T> T createBean(Class<T> beanClass) {
|
|
|
|
|
return applicationContext.getAutowireCapableBeanFactory().createBean(beanClass);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static <T> List<T> getBeans(Class<T> beanClass) {
|
|
|
|
|
return new ArrayList<>(applicationContext.getBeansOfType(beanClass).values());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public static <T> T initializeBean(T bean) {
|
|
|
|
|
AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
|
|
|
|
|
beanFactory.autowireBean(bean);
|
|
|
|
|
return (T) beanFactory.initializeBean(bean, bean.getClass().getName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void registerSingletonBean(String beanName, Class beanClass) {
|
|
|
|
|
GenericBeanDefinition definition = new GenericBeanDefinition();
|
|
|
|
|
definition.setBeanClass(beanClass);
|
|
|
|
|
definition.setScope(BeanDefinition.SCOPE_SINGLETON);
|
|
|
|
|
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) applicationContext.getAutowireCapableBeanFactory();
|
|
|
|
|
registry.registerBeanDefinition(beanName, definition);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void registerSingletonBean(String beanName, Object bean) {
|
|
|
|
|
SingletonBeanRegistry registry = (SingletonBeanRegistry) applicationContext.getAutowireCapableBeanFactory();
|
|
|
|
|
registry.registerSingleton(beanName, bean);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Object invokeMethod(Class<?> requiredType, String methodName, Object[] params) {
|
|
|
|
|
Object service = getBean(requiredType);
|
|
|
|
|
Class<? extends Object>[] paramClass = null;
|
|
|
|
|