【问题标题】:Inject collection of beans to constructor using annotations使用注解将 bean 集合注入构造函数
【发布时间】:2014-05-07 08:15:51
【问题描述】:

我正在开发基于 spring 的项目,我们使用构造函数注入并使用 @Configuration 配置上下文。

这是显示我的问题的简化示例。

我有 bean MyMainBean 指的是 Foo bean 的集合:

public class MyMainBean {
    private Collection<Foo> foos;
    public MyMainBean(Collection<Foo> foos) {
            this.foos = foos;
    }
}

这是豆子Foo:

public class Foo {
    private final String name;
    public Foo(String name) {
            this.name = name;
    }
    public void foo(String arg) {
            System.out.println("foo (" + name + "): " + arg);
    }
}

下面是配置类的样子:

@Configuration
public class AppConfig {
    @Bean
    public MyMainBean myMain(Collection<Foo> foos) {
        return new MyMainBean(foos);
    }

    @Bean
    public Collection<Foo> foos() {
        System.out.println("foos");
        return Arrays.asList(new Foo("colletion1"), new Foo("colletion2"));
    }
}

当我运行它时,我会收到异常消息:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.hello.impl.Foo] found for dependency [collection of com.hello.impl.Foo]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

消息很清楚,所以虽然不是我需要的,但我将以下方法添加到AppConfig:

@Bean
public Foo foo1() {
    System.out.println("foo1");
    return new Foo("single1");
}

和类似的foo2()

现在上下文运行并且 bean 已连接。但是尽管foo1()、foo2() 和foos() 被称为MyAppBean 在其构造函数集合中接收,该集合包含由foo1() 和foo2() 创建的2 个元素。

我想让foos() 工作,因为在我的真实代码中,类似的方法使用配置动态检索Foo 的列表。我相信这里缺少一些神奇的注释,因为我可以使用context.xml 创建 bean 列表,但我必须在这里使用以编程方式创建的上下文。

作为一种解决方法,我可以创建FooFactory bean,它将公开方法getFoos() 并将这个工厂连接到MyMain,但这看起来很难看。有没有更好的解决方案?

备注

  1. 尝试添加 @Qualifier 没有帮助
  2. 尝试使用 @Autowire 和 @Resource 而不是构造函数注入也没有帮助。

【问题讨论】:

    标签: java spring configuration dependency-injection


    【解决方案1】:

    由于两个@Bean 都在同一个AppConfig 中声明,您可以像这样解决您的问题:

     @Bean
     public MyMainBean myMain() {
         return new MyMainBean(foos());
     }
    

    如果有不同的@Configuration 类,@Resource 会派上用场:

    @Resource(name="foos")
    private Collection<Foo> foos;
    

    @Autowire 在这种情况下也无济于事,即使是 @Qualifier。

    【讨论】:

    • 谢谢,阿尔乔姆。如此明显和优雅......不幸的是,到目前为止,@Resource(name="foos") 的推荐对我不起作用,但第一个解决方案已经足够好了。
    • 为了记录,注入集合是 Spring 框架中的一个特例。当您请求Collection&lt;Foo&gt; 时,Spring 会尝试定位所有Foo 类型的bean 并将它们收集到所需的集合类型中。这就是您的 foo 集合 bean 被忽略的原因。
    【解决方案2】:

    有点晚了,但我们到了:

    @Configuration
    public class AppConfig {
    
        // Answer 1 : Inject the "foos" collection bean  using @Value instead of @Qualifier
        @Bean
        public MyMainBean myMain1(@Value("#{foos}") Collection<Foo> foos) {
            return new MyMainBean(foos);
        }
    
        // Answer 2 : call directly foos() since @Configuration bean is proxified,
        // "foos" collection bean will only be called and instanciated once (at first call)
        @Bean
        public MyMainBean myMain2() {
            return new MyMainBean(foos());
        }
    
        @Bean
        public Collection<Foo> foos() {
            System.out.println("foos");
            return Arrays.asList(new Foo("colletion1"), new Foo("colletion2"));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      相关资源
      最近更新 更多