Spring组件在Configuration类之前初始化

时间:2015-10-28 10:24:12

标签: spring

我有以下Spring组件:

@Component(LogicQualifiedBeanNames.PRODUCT_EVENTS_FLOW_PROCESSOR)
public class ProductEventsFlowProcessor extends AbstractFlowProcessor {

private List<ProductEventDetector> productEventDetectors;
private BesRequestFactory besRequestFactory;
private BesAccessor besAccessor;

@Autowired
public ProductEventsFlowProcessor(ItemsClassifier<WriteProductItem> itemsClassifier,
        @Qualifier(LogicQualifiedBeanNames.PRODUCT_EVENTS_DETECTORS) List<ProductEventDetector> productEventDetectors,
        BesRequestFactory requestFactory, BesAccessor besAccessor) {
    super(itemsClassifier);
    this.productEventDetectors = productEventDetectors;
    this.besRequestFactory = requestFactory;
    this.besAccessor = besAccessor;
}

以下配置类:

@Configuration
public class FlowConfiguration {

    private ProductEventDetector deleteProductEventDetector = new DeleteProductEventDetector();

    @Bean(name = LogicQualifiedBeanNames.PRODUCT_EVENTS_DETECTORS)
    public List<ProductEventDetector> getProductEventDetectors() {
         List<ProductEventDetector> productEventDetectors = Lists.newArrayList(deleteProductEventDetector);
         return productEventDetectors;
    }
}

以下上下文配置文件:

<beans:beans>
    <import resource="classpath:core-module-context.xml" />

<context:component-scan base-package="com.stackoverflow.logic" />
<aop:aspectj-autoproxy />   

</beans:beans>

当我测试我的应用程序时,收到以下消息:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.stackoverflow.logic.events.ProductEventDetector] found for dependency [collection of com.stackoverflow.logic.events.ProductEventDetector]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=productEventDetectors)}

我已经尝试使用DependsOn注释,但这没有帮助。

1 个答案:

答案 0 :(得分:1)

我发现了问题。 问题是创建了MediaStore类型的bean。出于某种原因,Spring并不喜欢这样。 该问题有两种解决方案:

  1. 使用类似List
  2. 的具体类
  3. 使用ArrayList注释代替@Resource
  4. 解决了这个问题,但我仍然不知道为什么Spring拒绝创建类型为@Autowired的bean。对此有何解释?