尝试在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 [...]
就我对构建弹簧应用程序上下文的理解而言,我应该正确地做到这一点,这种性质的类似问题遗憾地也没有给我任何解决错误的答案。
对不起下面的代码墙,但我觉得这些都是相关的。
无法自动连接的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> {
}
我尝试将其自动装入的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;
}
}
创建应用程序上下文的测试类:
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);
}
}
我的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>
答案 0 :(得分:7)
将您的交易配置修改为
<tx:annotation-driven proxy-target-class="true"/>
这样Spring就可以使用CGLIB代理来扩展你的bean类,即。 AlbumDao
。或者,编程到接口。