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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2902335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:05:02  来源:igfitidea点击:

Instantiating multiple beans of the same class with Spring annotations

springannotations

提问by Francois

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:

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

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

采纳答案by wax

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

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

Here is a simple example.

这是一个简单的例子。

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

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

First component:

第一个组件:

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;
    }
}

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

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

Second component:

第二部分:

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;
    }
}

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.

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

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.

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

The trickiest part is BeanFactoryPostProcessor implementation:

最棘手的部分是 BeanFactoryPostProcessor 实现:

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(",");
    }
}

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.

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

Here is a Spring config:

这是一个Spring配置:

<?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>

Last thing to notice here is although you cando it you shouldn'tunless 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.

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

回答by Espen

It's not possible. You get a duplicate exception.

这是不可能的。你得到一个重复的异常。

It's also far from optimal with configuration data like this in your implementation classes.

在您的实现类中使用这样的配置数据也远非最佳。

If you want to use annotations, you can configure your class with Java config:

如果你想使用注解,你可以使用Java config配置你的类:

@Configuration
public class PersonConfig {

    @Bean
    public Person personOne() {
        return new Person("Joe", "Smith");
    }

    @Bean
    public Person personTwo() {
        return new Person("Mary", "Williams");
    }
}

回答by huherto

I just had to solve a similar case. This may work if you can redefine the class.

我只需要解决一个类似的案例。如果您可以重新定义类,这可能会起作用。

// This is not a @Component
public class Person {

}

@Component
public PersonOne extends Person {
   public PersonOne() {
       super("Joe", "Smith");
   }
}

@Component
public PersonTwo extends Person {
   public PersonTwo() {
    super("Mary","Williams");
   }
}

Then just use PersonOne or PersonTwo whenever you need to autowire a specific instance, everywhere else just use Person.

然后,只要您需要自动装配特定实例,就使用 PersonOne 或 PersonTwo,其他任何地方都只需使用 Person。

回答by raisercostin

Continuing @espen answer, injecting beans with qualifiers and configuring them differently with external values.

继续@espen 回答,使用限定符注入 bean 并使用外部值对它们进行不同的配置。

public class Person{
  @Configuration
  public static class PersonConfig{
    @Bean
    //@Qualifier("personOne") - doesn't work - bean qualifier is method name
    public Person personOne() {
        return new Person("Joe", "Smith");
    }

    @Bean
    //@Qualifier("personTwo") - doesn't work - bean qualifier is method name
    public Person personTwo(@Value("${myapp.second.lastName}") String lastName) {
        return new Person("Mary", lastName);
    }
  }
  /* blah blah */
}

@Component
public class SomePersonReference{
  @Autowired
  @Qualifier("personTwo")
  Person marry;
}

回答by OrangeDog

Inspired by wax's answer, the implementation can be safer and not skip other post-processing if definitions are added, not constructed singletons:

受到蜡的回答的启发,如果添加了定义,则实现可以更安全,并且不会跳过其他后处理,而不是构造单例:

public interface MultiBeanFactory<T> {  // N.B. should not implement FactoryBean
  T getObject(String name) throws Exception;
  Class<?> getObjectType();
  Collection<String> getNames();
}

public class MultiBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
  public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
    Map<String, MultiBeanFactory> factories = beanFactory.getBeansOfType(MultiBeanFactory.class);

    for (Map.Entry<String, MultiBeanFactory> entry : factories.entrySet()) {
      MultiBeanFactory factoryBean = entry.getValue();
      for (String name : factoryBean.getNames()) {
        BeanDefinition definition = BeanDefinitionBuilder
            .genericBeanDefinition(factoryBean.getObjectType())
            .setScope(BeanDefinition.SCOPE_SINGLETON)
            .setFactoryMethod("getObject")
            .addConstructorArgValue(name)
            .getBeanDefinition();
        definition.setFactoryBeanName(entry.getKey());
        registry.registerBeanDefinition(entry.getKey() + "_" + name, definition);
      }
    }
  }
}

@Configuration
public class Config {
  @Bean
  public static MultiBeanFactoryPostProcessor() {
    return new MultiBeanFactoryPostProcessor();
  }

  @Bean
  public MultiBeanFactory<Person> personFactory() {
    return new MultiBeanFactory<Person>() {
      public Person getObject(String name) throws Exception {
        // ...
      }
      public Class<?> getObjectType() {
        return Person.class;
      }
      public Collection<String> getNames() {
        return Arrays.asList("Joe Smith", "Mary Williams");
      }
    };
  }
}

The bean names could still come from anywhere, such as wax's @Qualifierexample. There are various other properties on the bean definition, including the ability to inherit from the factory itself.

bean 名称仍然可以来自任何地方,例如wax 的@Qualifier示例。bean 定义还有各种其他属性,包括从工厂本身继承的能力。

回答by Enrico Giurin

Should you need to inject, in the new created object, beans or properties from the spring context, you can have a look at the following section of code in which I have extended the Espen answerby injecting a bean which is created from the spring context:

如果您需要从 spring 上下文中注入新创建的对象、bean 或属性,您可以查看以下代码部分,其中我 通过注入从 spring 上下文创建的 bean扩展了Espen 答案

@Configuration
public class PersonConfig {

@Autowired 
private OtherBean other;

@Bean
public Person personOne() {
    return new Person("Joe", "Smith", other);
    }
}

Have a look at this articlefor all the possibile scenarios.

查看本文了解所有可能的情况。