[Solved] expected single matching bean but found 2

Expected single matching bean but found 2 blog classification: exception

Exception information: org.springframework.beans.factory.unsatisfied dependencyexception:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.byd.mes.bussiness.service.siteoperation.activity.ExecuteActivityService] is defined: expected single matching bean but found 2: [executeActivityService, executeActivityForTesterService]

Solution:

Blog link address: http://haowei0315.javaeye.com/blog/444731

@Qualifier
@ Autowired is automatically assembled according to the type. In the above example, if there is more than one bean of type userdao in the spring context, the beancreationexception exception will be thrown; If a bean of type userdao does not exist in the spring context, a beancreationexception exception will also be thrown. We can use @ qualifier with @ Autowired to solve these problems
1. There may be multiple userdao instances

[java]

@Autowired

public void setUserDao( @Qualifier ( “userDao” )UserDaouserDao){

this .userDao=userDao;

}

@Autowired
public void setUserDao(@Qualifier("userDao") UserDao userDao) {
this.userDao = userDao;
}

In this way, Spring will find the bean with id userDao for assembly.
2. no UserDao instance may exist

[java]

@Autowired (required= false )

public void setUserDao(UserDaouserDao){

this .userDao=userDao;

}

@Autowired(required = false)
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

Spring supports not only the @ Autowired annotation defined by itself, but also several annotations defined by jsr-250 specification, which are @ resource, @ postconstruct and @ predestroy
@ resource is equivalent to @ Autowired, but @ Autowired is automatically injected by bytype, while @ resource is automatically injected by byname by default@ Resource has two important attributes: name and type. Spring resolves the name attribute of the @ resource annotation to the name of the bean, while the type attribute resolves to the type of the bean. Therefore, if the name attribute is used, the byname automatic injection policy is used, while the bytype automatic injection policy is used when the type attribute is used. If neither name nor type attribute is specified, byname will be used to inject the policy automatically through the reflection mechanism
@ resource assembly sequence
1. If both name and type are specified, a unique matching bean will be found from the spring context for assembly, and an exception will be thrown if it is not found
2. If name is specified, a bean matching name (ID) will be found from the context for assembly, and an exception will be thrown if it is not found
3, If you cannot find or find more than one bean, an exception will be thrown
4. If neither name nor type is specified, assembly will be automatically performed in byname mode; If there is no match, it will be backed back to an original type for matching. If there is no match, it will be assembled automatically

@Autowired

UserDao userDao

First, search by type and then by userdao name. If you can’t find it, report it

Similar Posts: