动态添加类到spring容易中,其中AppUtils.getCtx() 是指ApplicationContext 以下是知识点:
/**
* 动态注册bean
* @param beanName 注册的bean 名称
* @param fullClass 注册的bean 类完整路径
*/
public static void registeBean(String beanName,String fullClass){
// 将applicationContext转换为ConfigurableApplicationContext
ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) AppUtils
.getCtx();
// 获取bean工厂并转换为DefaultListableBeanFactory
DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext
.getBeanFactory();
BeanDefinitionBuilder userBeanDefinitionBuilder = BeanDefinitionBuilder
.genericBeanDefinition(fullClass);
defaultListableBeanFactory.registerBeanDefinition(beanName,
userBeanDefinitionBuilder.getRawBeanDefinition());
}