使用Spring注解实例化同一个类的多个bean

使用Spring注解实例化同一个类的多个bean

问题描述:

使用 XML 配置的 Spring bean 工厂,我可以轻松地实例化具有不同参数的同一类的多个实例.我怎样才能对注释做同样的事情?我想要这样的东西:

With an XML configured Spring bean factory, I can easily instantiate multiple instances of the same class with different parameters. How can I do the same with annotations? I would like something like this:

@Component(firstName="joe", lastName="smith")
@Component(firstName="mary", lastName="Williams")
public class Person { /* blah blah */ }

是的,您可以在自定义 BeanFactoryPostProcessor 实现的帮助下完成.

Yes, you can do it with a help of your custom BeanFactoryPostProcessor implementation.

这是一个简单的例子.

假设我们有两个组件.一个是对另一个的依赖.

Suppose we have two components. One is dependency for another.

第一个组件:

import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

 public class MyFirstComponent implements InitializingBean{

    private MySecondComponent asd;

    private MySecondComponent qwe;

    public void afterPropertiesSet() throws Exception {
        Assert.notNull(asd);
        Assert.notNull(qwe);
    }

    public void setAsd(MySecondComponent asd) {
        this.asd = asd;
    }

    public void setQwe(MySecondComponent qwe) {
        this.qwe = qwe;
    }
}

如您所见,该组件没有什么特别之处.它依赖于 MySecondComponent 的两个不同实例.

As you could see, there is nothing special about this component. It has dependency on two different instances of MySecondComponent.

第二部分:

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;


@Qualifier(value = "qwe, asd")
public class MySecondComponent implements FactoryBean {

    public Object getObject() throws Exception {
        return new MySecondComponent();
    }

    public Class getObjectType() {
        return MySecondComponent.class;
    }

    public boolean isSingleton() {
        return true;
    }
}

这有点棘手.这里有两件事要解释.第一个 - @Qualifier - 包含 MySecondComponent bean 名称的注释.这是一个标准的,但你可以*地实现你自己的.稍后你会看到为什么.

It's a bit more tricky. Here are two things to explain. First one - @Qualifier - annotation which contains names of MySecondComponent beans. It's a standard one, but you are free to implement your own. You'll see a bit later why.

第二件事要提到的是 FactoryBean 实现.如果 bean 实现了此接口,则它旨在创建一些其他实例.在我们的例子中,它创建了 MySecondComponent 类型的实例.

Second thing to mention is FactoryBean implementation. If bean implements this interface, it's intended to create some other instances. In our case it creates instances with MySecondComponent type.

最棘手的部分是 BeanFactoryPostProcessor 实现:

The trickiest part is BeanFactoryPostProcessor implementation:

import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;


public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        Map<String, Object> map =  configurableListableBeanFactory.getBeansWithAnnotation(Qualifier.class);
        for(Map.Entry<String,Object> entry : map.entrySet()){
            createInstances(configurableListableBeanFactory, entry.getKey(), entry.getValue());
        }

    }

    private void createInstances(
            ConfigurableListableBeanFactory configurableListableBeanFactory,
            String beanName,
            Object bean){
        Qualifier qualifier = bean.getClass().getAnnotation(Qualifier.class);
        for(String name : extractNames(qualifier)){
            Object newBean = configurableListableBeanFactory.getBean(beanName);
            configurableListableBeanFactory.registerSingleton(name.trim(), newBean);
        }
    }

    private String[] extractNames(Qualifier qualifier){
        return qualifier.value().split(",");
    }
}

它有什么作用?它遍历所有用@Qualifier 注释的bean,从注释中提取名称,然后手动创建具有指定名称的这种类型的bean.

What does it do? It goes through all beans annotated with @Qualifier, extract names from the annotation and then manually creates beans of this type with specified names.

这是一个 Spring 配置:

Here is a Spring config:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="MyBeanFactoryPostProcessor"/>

    <bean class="MySecondComponent"/>


    <bean name="test" class="MyFirstComponent">
        <property name="asd" ref="asd"/>
        <property name="qwe" ref="qwe"/>
    </bean>

</beans>

最后要注意的是,尽管您可以这样做,但除非必须这样做,否则您不应该,因为这是一种不太自然的配置方式.如果您有多个类的实例,最好坚持使用 XML 配置.

Last thing to notice here is although you can do it you shouldn't unless it is a must, because this is a not really natural way of configuration. If you have more than one instance of class, it's better to stick with XML configuration.