将弹簧上下文移动到META-INF后,自动装配失败

时间:2013-12-09 22:48:52

标签: java spring autowired meta-inf

将我的spring上下文配置移动到src/main/resources/META-INF/spring后,我无法连接我的bean。在我的spring上下文直接放在src文件夹之前,我的测试以及自动装配运行良好:

enter image description here

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:cmn-dao-context.xml" })
@Transactional
public class ComplaintDaoTest extends TestCase {

    @Autowired
    private ComplaintDao mComplaintDao;

错误:

09.12.2013 23:30:02 org.springframework.test.context.TestContextManager prepareTestInstance
SCHWERWIEGEND: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@873723] to prepare test instance [null(de.bc.qz.dao.ComplaintDaoTest)]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'de.bc.qz.dao.ComplaintDaoTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private de.bc.qz.dao.ComplaintDao de.bc.qz.dao.ComplaintDaoTest.mComplaintDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [de.bc.qz.dao.ComplaintDao] 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)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)

database.properties:

<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>database.properties</value>
    </property>
</bean>

有人能帮助我吗?

1 个答案:

答案 0 :(得分:3)

尝试使用classpath*:META-INF/spring/cmn-dao-context.xml

修改

<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>META-INF/spring/database.properties</value>
    </property>
</bean>