Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two Spring Service Beans with same Class Name (different package) are throwing Error even with qualifier

I have the following...

package package1;

@Service
@Qualifier("kb")
public class UserService {
...
}

package package2;

@Service
@Qualifier("user")
public class UserService {
...
}

@Autowired
@Qualifier("user")
package2.UserService p2;
@Autowired
@Qualifier("kb")
package1.UserService p1;

But when I try to run this I get...

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [boot.Application]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'userService' for bean class [package1.UserService] conflicts with existing, non-compatible bean definition of same name and class [package2.UserService]

How do I have 2 Services with the same name?

like image 305
JGleason Avatar asked Jan 28 '23 04:01

JGleason


1 Answers

Remove @Qualifier from class, use @Qualifier while autowiring only

@Service("kb")
public class UserService {
...
}

package package2;

@Service("user")
public class UserService {
...
}

From @Qualifier javadoc

**
 * This annotation may be used on a field or parameter as a qualifier for
 * candidate beans when autowiring. It may also be used to annotate other
 * custom annotations that can then in turn be used as qualifiers.
 */
like image 89
Pratapi Hemant Patel Avatar answered Jan 31 '23 08:01

Pratapi Hemant Patel