1. 程式人生 > >spring實現動態註冊bean

spring實現動態註冊bean

spring3.2.9,手動向applicationcontext中註冊bean

public void addBean(){
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:conf/springContext.xml"
            ,"classpath:conf/springContext-dataSource.xml"});
    
    BeanDefinitionRegistry b = (BeanDefinitionRegistry)(context.getBeanFactory());
    b.registerBeanDefinition("啊啊啊", BeanDefinitionBuilder.genericBeanDefinition(new Date().getClass()).getBeanDefinition());
    Date bean = (Date) context.getBean("啊啊啊");
    
}