i have a problem with spring autowire feature which found two beans with the same type, but in fact i only have one bean and the interface that this bean is implementing.
In applicationContext.xml i have these lines:
<context:component-scan base-package="xxx.vs.services"/>
<context:component-scan base-package="xxx.vs.dao"/>
<context:annotation-config/>
<bean id="intermedDao" class="xxx.vs.dao.yyy.IntermedDaoImpl" />
and :
package xxx.vs.dao.abs.yyy;
public interface IntermedDao extends GenericDao<Intermed> {
// methods here
}
package xxx.vs.dao.yyy;
import org.springframework.stereotype.Repository;
@Repository
public class IntermedDaoImpl extends GenericInboundDaoImpl<Intermed> implements IntermedDao {
// methods here
}
package xxx.vs.services.yyy;
@Service
@Transactional
public class IntermedServiceImpl implements IntermedService {
@Autowired
IntermedDao dao;
public IntermedDao getDao() {
return dao;
}
public void setDao(IntermedDao dao) {
this.dao = dao;
}
}
with this config i get:
java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'intermedServiceImpl':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: xxx.vs.dao.abs.yyy.IntermedDao xxx.vs.services.yyy.IntermedServiceImpl.dao; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException:No unique bean of type [xxx.vs.dao.abs.yyy.AgentDao] is defined: expected single matching bean but found 2: [intermedDaoImpl, intermedDao]
does this happens because i scan the packages that contains the interfaces that my DAO`s classes are implementing ?
This is happening because you’ve both explicitly declared a bean:
as well as declaring a
@Repositoryto be picked up by component scanning:You need to do one or the other, not both. I suggest deleting the
<bean>.Note that the error message:
Mentions the conflicting beans. The first one is your
@Repository, where the name of the bean is auto-generated from the classname; the second one is your<bean>.