我定义了一个基类接口BaseDao,下面有些update\save的方法;
然后我用一个BaseDaoImpl去实现这个接口;好啦,然后我现在有两个Dao接口,一个ADao extends BaseDao,一个BDao extends BaseDao; 然后再有这两个Dao的实现: ADaoImpl extends BaseDaoImpl implements ADao; BDaoImpl extends BaseDaoImpl implements BDao;
这两个实现都加了@repository。结果就是启动错:
No unique bean of type [com.a.b.BaseDao] is defined: expected single matching bean but found 2: [aDaoImpl, bDaoImpl]
出现这个异常的原因是因为我用了@Autowird这个注解,这个注解是根据类型的方式搜索匹配的,找到了两个相符的依赖类,对于上述配置就是找到了basedao 的两个 bean: adaoimpl , bdaoimpl。
对于这种同类型class有多个实例的解决方案的一种方案是继续延用autowired,不过通过@Qualifier指明是哪个名字的bean,如:
@Autowired
public void setADao(@Qualifier("aDaoImpl") ADao adao) {
this.adao= adao;
}
另外一种方案是使用@Resource这个注解,其功能与@autowired差不多,但是可以通过指定bean name或bean type注入相关bean,默认是按name注入,比autowired灵活很多,如:
@Resource
private ADao aDaoImpl;
分享到:
相关推荐
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController' defined in ServletContext resource [/WEB-INF/springMVC-servlet.xml]: Error ...
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Instantiation of bean failed; nested exception is org....
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed;...
Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in URL [file:/E:/cloudwave-core/src/main/webapp/WEB- INF/classes/...
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class ...
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customAreaService' defined in class path resource [applicationContext.xml]: Initialization of bean failed;...
spring boot exception handler.zip
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/...
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.interceptor.TransactionInterceptor#0': Error setting property values; nested ...
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in file [F:\IdeaProjects\ssmDemo1\out\artifacts\ssmDemo1_war_exp
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure...
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested ...
BeanCreationException BeanCreationNotAllowedException BeanCurrentlyInCreationException BeanDefinition BeanDefinitionBuilder BeanDefinitionDecorator BeanDefinitionDocumentReader ...
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args) throws BeanCreationException { ... try { // /** * 4. 填充属性 * ...