spring手動註冊bean,移除bean,獲取bean
阿新 • • 發佈:2019-01-05
注意applicationContext.xml檔案的路徑配置,按自己專案spring配置檔案的路徑做相應的調整
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* springApplicationContext工具
*/
public class SpringBeanRegisterUtil {
private static ApplicationContext context=new
ClassPathXmlApplicationContext("applicationContext.xml");
private static ConfigurableApplicationContext configurableContext = (ConfigurableApplicationContext) context;
private static BeanDefinitionRegistry beanDefinitionRegistry = (DefaultListableBeanFactory) configurableContext.getBeanFactory();
/**
* 註冊bean
* @param beanId 所註冊bean的id
* @param className bean的className,
* 三種獲取方式:1、直接書寫,如:com.mvc.entity.User
* 2、User.class.getName
* 3.user.getClass().getName()
*/
public static void registerBean(String beanId,String className) {
// get the BeanDefinitionBuilder
BeanDefinitionBuilder beanDefinitionBuilder =
BeanDefinitionBuilder.genericBeanDefinition(className);
// get the BeanDefinition
BeanDefinition beanDefinition=beanDefinitionBuilder.getBeanDefinition();
// register the bean
beanDefinitionRegistry.registerBeanDefinition(beanId,beanDefinition);
}
/**
* 移除bean
* @param beanId bean的id
*/
public static void unregisterBean(String beanId){
beanDefinitionRegistry.removeBeanDefinition(beanId);
}
/**
* 獲取bean
* @param name bean的id
* @param <T>
* @return
*/
public static <T> T getBean(String name) {
return (T) context.getBean(name);
}
}