SpringBoot動態注入及操作Bean

寫在前面:2020年面試必備的Java後端進階面試題總結了一份複習指南在Github上,內容詳細,圖文並茂,有需要學習的朋友可以Star一下!
GitHub地址:https://github.com/abel-max/Java-Study-Note/tree/master

在Spring中我們通過getBean來獲得對象,但這些對象都是事先定義好的,如果需要操作事先未定義的Bean就需要動態注入、修改和刪除Bean

思路
在Spring中,BeanFactory負責管理Bean,具體來說是DefaultListableBeanFactory,我們只需要獲取到當前上下文中的BeanFactory,就能執行其中的註冊Bean的方法registerBeanDefinition,註冊Bean時需要Bean的信息,Spring提供了BeanDefinitionBuilder.genericBeanDefinition()方法來構建BeanDefinition,用這些方法就可以實現Bean的動態注入

代碼實現
測試服務類TestService
創建一個測試類來測試Bean的操作,這個類上沒有加註解,Spring在加載時不會自動注入

package com.mantis.bean.service;
/**
 * @Description:
 * @author: wei.wang
 * @since: 2020/9/17 12:53
 * @history: 1.2020/9/17 created by wei.wang
 */
public class TestService {
    private String name;
    private String id;
    public String getId() {
        return id;
    }    public void setId(String id) {
        this.id = id;
    }    public String getName() {
        return name;
    }    public void setName(String name) {
        this.name = name;
    }    public void print() {
        System.out.println("獲取bean,name=" + name + ",id=" + id);
    }    @Override
    public String toString() {
        return "TestService{" +
                "name='" + name + '\'' +
                ", id='" + id + '\'' +
                '}';
    }}

SpringContextUtil類
操作Bean的Util類,其中上線文ApplicationContext在SpringBoot啓動時設置,也可以直接使用Autowired注入或者根據具體項目具體實現

package com.mantis.bean.util;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
/**
 * @Description:
 * @author: wei.wang
 * @since: 2020/9/17 12:52
 * @history: 1.2020/9/17 created by wei.wang
 */
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) {
        try {
            return applicationContext.getBean(name);
        } catch (NoSuchBeanDefinitionException ex) {
            return null;
        }
    }
    //通過類型獲取上下文中的bean
    public static Object getBean(Class<?> requiredType) {
        return applicationContext.getBean(requiredType);
    }
}

啓動類SpringBootBeanApplication
在啓動類上設置上下文

package com.mantis.bean;
import com.mantis.bean.util.SpringContextUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class SpringBootBeanApplication {
    public static void main(String[] args) {
        ApplicationContext app = SpringApplication.run(SpringBootBeanApplication.class, args);
        SpringContextUtil.setApplicationContext(app);    }}
注入Bean
向Spring上下文中注入Bean,注入前先檢查Bean是否已經存在

/**
     * 註冊Bean     *     * @return
     */    @GetMapping("/bean/register/{beanName}")
    public String registerBean(@PathVariable String beanName) {        //獲取context        ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) SpringContextUtil.getApplicationContext();        //獲取BeanFactory        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();        //創建bean信息.        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(TestService.class);        beanDefinitionBuilder.addPropertyValue("id", "1");
        beanDefinitionBuilder.addPropertyValue("name", "張三");
        //判斷Bean是否已經註冊        Object beanObject = SpringContextUtil.getBean(beanName);        if (beanObject != null) {
            System.out.println(String.format("Bean %s 已註冊", beanName));
        } else {
            //動態註冊bean.            defaultListableBeanFactory.registerBeanDefinition(beanName, beanDefinitionBuilder.getBeanDefinition());            //獲取動態註冊的bean.            beanObject = SpringContextUtil.getBean(beanName);            if (beanObject != null) {
                System.out.println(String.format("Bean %s 註冊成功", beanName));
                return beanObject.toString();
            } else {
                return "register Bean Error";
            }        }        return "register Bean Success";
    }

修改Bean
從SpringContext中獲取到指定Bean的Class對象,獲取到之後就可以根據字段進行賦值

/**
     * 動態修改Bean     *     * @return
     */    @GetMapping("/bean/update/{beanName}")
    public String update(@PathVariable String beanName) {        ApplicationContext applicationContext = SpringContextUtil.getApplicationContext();        String[] beans = applicationContext.getBeanDefinitionNames();        for (String bean : beans) {
            // 拿到bean的Class對象            Class<?> beanType = applicationContext.getType(bean);            if (beanType == null) {
                continue;            }            // 拿到當前bean類型的所有字段            Field[] declaredFields = beanType.getDeclaredFields();            if (beanName.equals(bean)) {
                for (Field field : declaredFields) {
                    // 從spring容器中拿到這個具體的bean對象                    Object beanObject = applicationContext.getBean(bean);                    // 當前字段設置新的值                    try {                        String fieldName = field.getName();                        if ("name".equals(fieldName)) {
                            setFieldData(field, beanObject, "AL113A5");
                        } else if ("id".equals(fieldName)) {
                            setFieldData(field, beanObject, "12");
                        }                    } catch (Exception e) {                        e.printStackTrace();                    }                }            }        }        return "update Bean Success";
    }    private void setFieldData(Field field, Object bean, String data) throws Exception {        field.setAccessible(true);
        Class<?> type = field.getType();
        if (type.equals(String.class)) {
            field.set(bean, data);        } else if (type.equals(Integer.class)) {
            field.set(bean, Integer.valueOf(data));        } else if (type.equals(Long.class)) {
            field.set(bean, Long.valueOf(data));        } else if (type.equals(Double.class)) {
            field.set(bean, Double.valueOf(data));        } else if (type.equals(Short.class)) {
            field.set(bean, Short.valueOf(data));        } else if (type.equals(Byte.class)) {
            field.set(bean, Byte.valueOf(data));        } else if (type.equals(Boolean.class)) {
            field.set(bean, Boolean.valueOf(data));        } else if (type.equals(Date.class)) {
            field.set(bean, new Date(Long.parseLong(data)));        }    }

移除Bean
將Bean從Spring上下文中移除

/**
     * 移除Bean
     *
     * @return
     */
    @GetMapping("/bean/remove/{beanName}")
    public String removeBeanDefinition(@PathVariable String beanName) {
        Object beanObject = SpringContextUtil.getBean(beanName);
        if (beanObject == null) {
            System.out.println(String.format("Bean %s 不存在", beanName));
            return "remove Error";
        }        //獲取context.
        ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) SpringContextUtil.getApplicationContext();
        //獲取BeanFactory
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();
        defaultListableBeanFactory.removeBeanDefinition(beanName);
        System.out.println(String.format("Bean %s 已移除", beanName));
        return "remove Success";
    }

測試
寫一個操作Bean的方法

/**
     * 操作Bean
     *
     * @return
     */
    @GetMapping("/bean/print/{beanName}")
    public String print(@PathVariable String beanName) {
        Object beanObject = SpringContextUtil.getBean(beanName);
        if (beanObject != null) {
            ((TestService) beanObject).print();            return beanObject.toString();
        } else {
            System.out.println(String.format("Bean %s 不存在", beanName));
        }        return String.format("Bean %s 不存在", beanName);
    }

註冊
調用方法

localhost:9000/bean/register/TestService

結果

執行操作Bean的方法,返回結果如下

TestService{name='張三', id='1'}

修改
調用方法

localhost:9000/bean/update/TestService

結果

執行操作Bean的方法,返回結果如下

TestService{name='AL113A5', id='12'}

移除
調用方法

localhost:9000/bean/remove/TestService

結果

執行操作Bean的方法,返回結果如下

Bean TestService 不存在

來源:https://www.tuicool.com/articles/JzaIraN

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章