首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

编码形式注册及移除注册bean

2013-03-06 
编码方式注册及移除注册beanSpringBeanRegisterUtil.javapackage utilsimport org.springframework.beans

编码方式注册及移除注册bean
SpringBeanRegisterUtil.java

package utils;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;public class SpringBeanRegisterUtil {private static ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");private static ConfigurableApplicationContext configurableContext = (ConfigurableApplicationContext) context;  private static BeanDefinitionRegistry beanDefinitionRegistry =    (DefaultListableBeanFactory) configurableContext.getBeanFactory();  /** register the bean */public static void registerBean(String beanId,String className) {// get the BeanDefinitionBuilderBeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(className);  // get the BeanDefinitionBeanDefinition beanDefinition=beanDefinitionBuilder.getBeanDefinition();// register the beanbeanDefinitionRegistry.registerBeanDefinition(beanId,beanDefinition);  }/** unregister the bean */public static void unregisterBean(String beanId){beanDefinitionRegistry.removeBeanDefinition(beanId);}}

热点排行