У меня проблема с функцией spring autowire, которая обнаружила два компонента с одним и тем же типом, но на самом деле у меня есть только один компонент и интерфейс, который реализует этот компонент.spring autowire 2 beans found exception
В applicationContext.xml я эти строки:
<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" />
и:
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;
}
}
с этой конфигурации я получаю:
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]
Это происходит, потому что я просматриваю пакеты, содержащие интерфейсы, которые реализуют классы DAO?
действительно, после того, как я удалил xml bean, все было в порядке. Благодарю. –