spring boot 动态注入bean

方法一

SpringContextUtil

public class SpringContextUtil {
    private static ApplicationContext applicationContext;
    //获取上下文
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
    //设置上下文
    public static void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextUtil.applicationContext = applicationContext;
    }
    //通过名字获取上下文中的bean
    public static Object getBean(String name){
        return applicationContext.getBean(name);
    }
    //通过类型获取上下文中的bean
    public static Object getBean(Class<?> requiredType){
        return applicationContext.getBean(requiredType);
    }
}

启动类

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        ApplicationContext app =  SpringApplication.run(Application.class, args);
        SpringContextUtil.setApplicationContext(app);
    }
}

测试bean

@Component
public class TestService {
    public String doService(String contxt){
        System.err.printf(contxt+"hello service");
        return  "hello service";
    }

}

//无注入

public class TestController implements InitializingBean {

    @Autowired
    private TestService testService;

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("我是动态注册的你,不是容器启动的时候注册的你");
    }

    public String toAction(String content){
        return "-->" +  testService.doService(content);
    }
}

测试

@RestController
public class CallCSBController {

    @GetMapping("/bean")
    public String registerBean() {
        //将applicationContext转换为ConfigurableApplicationContext
        ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) SpringContextUtil.getApplicationContext();

        // 获取bean工厂并转换为DefaultListableBeanFactory
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();

        // 通过BeanDefinitionBuilder创建bean定义
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(TestController.class);

        // 设置属性userService,此属性引用已经定义的bean:userService,这里userService已经被spring容器管理了.
//        beanDefinitionBuilder.addPropertyReference("testService", "testService");

        // 注册bean
        defaultListableBeanFactory.registerBeanDefinition("testController", beanDefinitionBuilder.getRawBeanDefinition());


        TestController userController = (TestController) SpringContextUtil.getBean("testController");

        return userController.toAction("动态注册生成调用");

        //删除bean.
        //defaultListableBeanFactory.removeBeanDefinition("testService");
    }
}

以上参考
链接:https://www.jianshu.com/p/41c716e7c31b

方法二(略有不同)

工具类

package com.theeternity.beans.applicationContextRegister;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @program: apiboot
 * @description: 获取ApplicationContext, 实现动态注入bean
 * @author: TheEternity Zhang
 * @create: 2019-06-22 12:15
 */
@Component
@Slf4j
public class ApplicationContextRegister implements ApplicationContextAware {
    private static ApplicationContext APPLICATION_CONTEXT;
    /**
     * 设置spring上下文
     * @param applicationContext spring上下文
     * @throws BeansException
     * */
    @Override  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.debug("ApplicationContext registed-->{}", applicationContext);
        APPLICATION_CONTEXT = applicationContext;
    }

    /**
     * 获取容器
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return APPLICATION_CONTEXT;
    }

    /**
     * 获取容器对象
     * @param type
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> type) {
        return APPLICATION_CONTEXT.getBean(type);
    }

    public static <T> T getBean(String name,Class<T> clazz){
        return APPLICATION_CONTEXT.getBean(name, clazz);
    }

    public static Object getBean(String name){
        return APPLICATION_CONTEXT.getBean(name);
    }
}

测试bean

@Component
public class TestService {
    public String doService(String contxt){
        System.err.printf(contxt+"hello service");
        return  "hello service";
    }
}

//无注入
public class TestController implements InitializingBean {

    @Autowired
    private TestService testService;

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("我是动态注册的你,不是容器启动的时候注册的你");
    }

    public String toAction(String content){
        return "-->" +  testService.doService(content);
    }
}

测试

@RestController
public class CallCSBController {

    @GetMapping("/bean")
    public String registerBean() {
        //将applicationContext转换为ConfigurableApplicationContext
        ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) ApplicationContextRegister.getApplicationContext();

        // 获取bean工厂并转换为DefaultListableBeanFactory
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();

        // 通过BeanDefinitionBuilder创建bean定义
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(TestController.class);

        // 设置属性userService,此属性引用已经定义的bean:userService,这里userService已经被spring容器管理了.
//        beanDefinitionBuilder.addPropertyReference("testService", "testService");

        // 注册bean
        defaultListableBeanFactory.registerBeanDefinition("testController", beanDefinitionBuilder.getRawBeanDefinition());


        TestController userController = (TestController) ApplicationContextRegister.getBean("testController");

        return userController.toAction("动态注册生成调用");

        //删除bean.
        //defaultListableBeanFactory.removeBeanDefinition("testService");
    }
    第一种方法的另外一种形式
    /**
    //获取ApplicationContext
    ApplicationContext ctx=ApplicationContextRegister.getApplicationContext();
    //获取BeanFactory  
    DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) ctx.getAutowireCapableBeanFactory();       
    //创建bean信息.  
    BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(TestService.class);  
    beanDefinitionBuilder.addPropertyValue("name","张三");          
    //动态注册bean.  
    defaultListableBeanFactory.registerBeanDefinition("testService", beanDefinitionBuilder.getBeanDefinition());  
    //获取动态注册的bean.  
    TestService testService =ctx.getBean(TestService.class);、testService.print();  
    */
    
    @GetMapping("/bean2")
    public String registerBean2() {

        TestController userController = (TestController) ApplicationContextRegister.getBean(TestController.class);

        return userController.toAction("动态注册生成调用");

    }
}

以上参考:

主力:https://www.jianshu.com/p/41c716e7c31b
辅助:https://www.jb51.net/article/140157.htm

拓展理解

我们通过getBean来获得对象,但这些对象都是事先定义好的,我们有时候要在程序中动态的加入对象.因为如果采用配置文件或者注解,我们要加入对象的话,还要重启服务,如果我们想要避免这一情况就得采用动态处理bean,包括:动态注入,动态删除。

本节大纲 :

(1)动态注入bean思路;
(2)动态注入实现代码;
(3)多次注入同一个bean的情况;
(4)动态删除;

接下来我们看下具体的内容:

(1)动态注入bean思路;

​ 在具体进行代码实现的时候,我们要知道,Spring管理bean的对象是BeanFactory,具体的是DefaultListableBeanFactory,在这个类当中有一个注入bean的方法:registerBeanDefinition,在调用registerBeanDefinition方法时,需要BeanDefinition参数,那么这个参数怎么获取呢?Spring提供了BeanDefinitionBuilder可以构建一个BeanDefinition,那么我们的问题就是如何获取BeanFactory了,这个就很简单了,只要获取到ApplicationContext对象即可获取到BeanFacory了。

(2)动态注入实现代码;

综上所述,如果我们要编写一个简单里的例子的话,那么分以个几个步骤进行编码即可进行动态注入了:

<1>. 获取ApplicationContext;
<2>. 通过ApplicationContext获取到BeanFacotory;
<3>. 通过BeanDefinitionBuilder构建BeanDefiniton;
<4>. 调用beanFactory的registerBeanDefinition注入beanDefinition;
<5>. 使用ApplicationContext.getBean获取bean进行测试;

​很明显我们需要先定义个类进行测试,比如TestService代码如下:

 package com.kfit.demo.service;  
 public class TestService {  
     private String name;  
     public String getName() {  
        return name;  
     }  
     public void setName(String name) {  
        this.name = name;  
     }  
     public void print(){  
        System.out.println("动态载入bean,name="+name);  
     }  
 }  
注意:这里没有使用@Service和配置文件进行注入TestService。

那么下面我们的目标就是动态注入TestService了,根据以上的分析,我们进行编码,具体代码如下:

//获取context.  -- Angel -守护天使  
ApplicationContext ctx =  (ApplicationContext) SpringApplication.run(App.class, args);  
//获取BeanFactory  
DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) ctx.getAutowireCapableBeanFactory();       
//创建bean信息.  
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(TestService.class);  
beanDefinitionBuilder.addPropertyValue("name","张三");          
//动态注册bean.  
defaultListableBeanFactory.registerBeanDefinition("testService", beanDefinitionBuilder.getBeanDefinition());  
//获取动态注册的bean.  
TestService testService =ctx.getBean(TestService.class);
testService.print();  

执行代码我们会在控制台看到如下打印信息:

动态载入bean,name=张三

​ 到这里,就证明我们的代码很成功了。

(3)多次注入同一个bean的情况;

​ 多次注入同一个bean的,如果beanName不一样的话,那么会产生两个Bean;如果beanName一样的话,后面注入的会覆盖前面的。

第一种情况:beanName一样的代码:
beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(TestService.class);  
defaultListableBeanFactory.registerBeanDefinition("testService", beanDefinitionBuilder.getBeanDefinition());  
TestService testService =ctx.getBean(TestService.class);
testService.print();  

运行看控制台: 动态载入bean,name=李四

第二种情况:beanName不一样的代码:
beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(TestService.class);  
defaultListableBeanFactory.registerBeanDefinition("testService1", beanDefinitionBuilder.getBeanDefinition());  
TestService testService =ctx.getBean(TestService.class);
testService.print();  

此时如果没有更改别的代码直接运行的话,是会报如下错误的:

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.kfit.demo.service.TestService] is defined: expected single matching bean but found 2: testService1,testService 

​ 大体意思就是在getBean的时候,找到了两个bean,这时候就不知道要获取哪个了,所以在获取的时候,我们就要指定我们是要获取的testService还是testService1,只需要修改一句代码:

将代码:

TestService testService =ctx.getBean(TestService.class);

修改为:

TestService testService =ctx.getBean("testService");

(4)动态删除;

​ 相对于动态注入,动态删除就很简单了,直接奉上代码:

 //删除bean.  
 defaultListableBeanFactory.removeBeanDefinition("testService"); 

拓展参考:

https://412887952-qq-com.iteye.com/blog/2348445


原文:https://www.cnblogs.com/eternityz/p/12241143.html
作者: 未月廿三