美文网首页
spring 动态注册bean,动态删除bean,自动ioc

spring 动态注册bean,动态删除bean,自动ioc

作者: 团长plus | 来源:发表于2021-03-04 15:11 被阅读0次

    前言

    有一种情况当ioc的一个bean由于异常原因导致的不可用,必须需要重新注册一个bean,来替换当前的bean.

    package cn.freemud.order.settlement.core.config;
    
    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.stereotype.Component;
    
    /**
     * @Author hongwang.zhang
     * @Description
     * @Date 上午11:00 21-3-4
     * @Param
     * @return
     **/
    @Component
    public class BeanHolder {
    
        private final ConfigurableApplicationContext applicationContext;
    
    
        public BeanHolder(ConfigurableApplicationContext applicationContext) {
            this.applicationContext = applicationContext;
        }
    
        public ApplicationContext getApplicationContext() {
            return applicationContext;
        }
    
        /**
         * 根据构造参数注入
         *
         * @param beanName
         * @param clazz
         * @param args
         * @param <T>
         * @return
         */
        public <T> T registerBeanConstruct(String beanName, Class<T> clazz, Object... args) {
            // 创建bean信息.
            BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
            if (args.length > 0) {
                for (Object arg : args) {
                    beanDefinitionBuilder.addConstructorArgValue(arg);
                }
            }
            BeanDefinition beanDefinition = beanDefinitionBuilder.getRawBeanDefinition();
            BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry) applicationContext.getBeanFactory();
            beanFactory.registerBeanDefinition(beanName, beanDefinition);
            return applicationContext.getBean(beanName, clazz);
        }
    
    
        /**
         * 删除beandefinition
         * @param beanName
         */
        public void removeBeanDefinition(String beanName) {
            // 获取BeanFactory
            DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) applicationContext
                    .getAutowireCapableBeanFactory();
            defaultListableBeanFactory.removeBeanDefinition(beanName);
        }
    
    
    }
    
    
    • 替换老的bean重新注册一个对象,TestsDao 依赖这个对象的会自动获取最新的bean ioc
     ApplicationContext applicationContext = beanHolder.getApplicationContext();
            System.out.println(testsDao);
            beanHolder.removeBeanDefinition("testdao");
            TestsDao testsDao11 = beanHolder.registerBeanConstruct("testdao", TestsDao.class, "ZHW");
    
    @Getter
    @Setter
    @ToString
    @Component("testdao")
    public class TestsDao {
    
        private  String name="default";
    
        public TestsDao(@Autowired(required =  false)String name) {
            System.out.println("动态注入"+name);
            this.name = name;
        }
    }
    

    相关文章

      网友评论

          本文标题:spring 动态注册bean,动态删除bean,自动ioc

          本文链接:https://www.haomeiwen.com/subject/ntdkqltx.html