关于spring:SpringBoot动态注入及操作Bean

40次阅读

共计 6366 个字符,预计需要花费 16 分钟才能阅读完成。

在 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 不存在

正文完
 0