[英]Spring @Autowired bean not found, No qualifying bean of type […] found
I get the following exception when trying to start my application context in a TestNG-Test: 尝试在TestNG-Test中启动应用程序上下文时出现以下异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'albumService': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private net.yoon.archive.dao.AlbumDao net.yoon.archive.service.AlbumService.albumDa
o; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [net.yoon.archive.dao.AlbumDao] found for dependency: exp
ected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at [... Framework code ...]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private net.yoon.archive.dao.AlbumDao net.yoon.archive.service.AlbumService.albumDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [net.yoon.archive.dao.AlbumDao] 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 [...]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [net.yoon.archive.dao.AlbumDao] 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 [...]
As far as my understanding of building a spring application context goes I should be doing it correctly, similar questions of this nature have sadly also not given me any answers that solved the error. 至于我对构建弹簧应用程序上下文的理解,我应该正确地做到这一点,这种性质的类似问题遗憾地也没有给我任何解决错误的答案。
Sorry for the wall of code below, but I felt it could all be relevant. 对不起下面的代码墙,但我觉得它可能都是相关的。
The bean that can't be autowired: 无法自动连接的bean:
package net.yoon.archive.dao;
import net.yoon.archive.domain.music.Album;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
@Transactional
public class AlbumDao extends AbstractHibernateDao<Album> {
}
The bean I'm trying to autowire it in: 我试图将它自动装入的bean:
package net.yoon.archive.service;
import net.yoon.archive.dao.AbstractHibernateDao;
import net.yoon.archive.dao.AlbumDao;
import net.yoon.archive.domain.music.Album;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AlbumService extends AbstractDaoService<Album> {
@Autowired
private AlbumDao albumDao;
@Override
protected AbstractHibernateDao<Album> getDao() {
return albumDao;
}
}
The test class that's creating the application context: 正在创建应用程序上下文的测试类:
package net.yoon.archive.service;
import net.yoon.archive.TestGroups;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;
@Test(groups = { TestGroups.SERVICE_TEST })
@ContextConfiguration({ "classpath:applicationContext.xml" })
public class NameServiceTest extends AbstractTestNGSpringContextTests {
@Autowired
private NameService nameService;
public void testSetup() {
Assert.assertNotNull(nameService);
}
}
My applicationContext.xml 我的applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<tx:annotation-driven />
<context:component-scan base-package="net.yoon.archive" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory">
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
[...]
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
[...]
</bean>
</beans>
Modify your transactional configuration to 将您的事务配置修改为
<tx:annotation-driven proxy-target-class="true"/>
so that Spring uses CGLIB proxies which can extend your bean classes, ie. 这样Spring就可以使用CGLIB代理来扩展你的bean类,即。
AlbumDao
. AlbumDao
。 Alternatively, program to interfaces. 或者,编程到接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.