由于BeanCreationException,使用CDI在Spring中注入属性失败

时间:2016-06-16 18:56:36

标签: java spring cdi

我正在尝试使用CdI将属性文件注入我的spring组件并继续抛出下面的异常。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'validationService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Properties ValidationService.ProjectProperties; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.util.Properties] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @PropertiesFromFile(value=config.properties)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)

这是我的代码:

@Component
public class ValidationService {
    @Autowired
    @PropertiesFromFile("config.properties")
    private Properties configProperties;

    @Autowired
    @PropertiesFromFile("custom.properties")
    private Properties customProperties;
}

我尝试的另一段代码是:

@Component
public class ValidationService {
    @Inject
    @PropertiesFromFile("config.properties")
    private Properties configProperties;

    @Inject
    @PropertiesFromFile("custom.properties")
    private Properties customProperties;
}

虽然Spring现在支持AutoWired下的Inject,但我仍然尝试过。

我是Spring的新手,如果有人能指出我正确的方向,我真的很感激

P.S。可以在此处找到Generic注释的代码: http://slackspace.de/articles/injecting-properties-in-java-ee-applications/

0 个答案:

没有答案