samedi 7 mars 2015

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:



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://ift.tt/GArMu6"
xmlns:context="http://ift.tt/GArMu7"
xmlns:xsi="http://ift.tt/ra1lAU" xmlns:tx="http://ift.tt/OGfeU2"
xmlns:p="http://ift.tt/1jdM0fE"
xsi:schemaLocation="
http://ift.tt/OGfeU2
http://ift.tt/18tm2Tg
http://ift.tt/GArMu6
http://ift.tt/1jdM0fG
http://ift.tt/GArMu7
http://ift.tt/1jdLYo7">

<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>

Aucun commentaire:

Enregistrer un commentaire