在使用spring框架后,我们一般喜欢把Bean的管理交给Spring容器进行管理。最近项目中有个需求是我们需要根据配置文件在项目运行中动态实现某个接口,类似于工厂方法,而这个接口的实现类依赖了一大堆Spring容器中其他对象,所以我们想能不能借助Spring的工厂方法来管理我们这个类的实现呢。

一、通过实现ApplicationContextAware接口获取到ApplicationContext

二、通过BeanDefinitionBuilder定义Bean信息,并注入到Spring的AutowireCapableBeanFactory中。

具体代码实现如下:

@Component
public class BeanRegister implements ApplicationContextAware {
    private ApplicationContext ac;
    /**
     * Set the ApplicationContext that this object runs in.
     * Normally this call will be used to initialize the object.
     * <p>Invoked after population of normal bean properties but before an init callback such
     * as [email protected] InitializingBean#afterPropertiesSet()}
     * or a custom init-method. Invoked after [email protected] ResourceLoaderAware#setResourceLoader},
     * [email protected] ApplicationEventPublisherAware#setApplicationEventPublisher} and
     * [email protected] MessageSourceAware}, if applicable.
     *
     * @param applicationContext the ApplicationContext object to be used by this object
     * @throws ApplicationContextException in case of context initialization errors
     * @throws BeansException              if thrown by application context methods
     * @see BeanInitializationException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.ac = applicationContext;
    }

    /**
     * 注册Bean
     *
     * @param nodeId
     * @return
     */
    public XxxService register(String nodeId) throws ClassNotFoundException {
        // 获取BeanFactory
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) ac.getAutowireCapableBeanFactory();
        // 创建bean信息
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(XxxService.class);
        // 一些初始化参数
        beanDefinitionBuilder.addPropertyValue("nodeId", nodeId);
        // 动态注册bean
        defaultListableBeanFactory.registerBeanDefinition(nodeId + "Node", beanDefinitionBuilder.getBeanDefinition());

        return ac.getBean(nodeId + "Node", XxxService.class);
    }
}