I get the following exception when trying to start my application context in a 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:
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:
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
<?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. AlbumDao
. Alternatively, program to interfaces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With