将我的 spring 上下文配置移动到 src/main/resources/META-INF/spring 后,我无法连接我的 bean。在我的 spring 上下文直接放置在 src 文件夹下并且我的测试和 Autowiring 运行良好之前:
@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)
数据库.属性:
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>database.properties</value>
</property>
</bean>
有人可以帮助我吗?
最佳答案
尝试使用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>
关于java - 将 spring 上下文移动到 META-INF 后 Autowiring 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20482503/