有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我试图使用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,但我仍然尝试了这两种方法

我是春天的新手,如果有人能给我指出正确的方向,我将不胜感激

注:通用注释的代码可在此处找到: http://slackspace.de/articles/injecting-properties-in-java-ee-applications/


共 (0) 个答案