note: this annotation is @qualifier.
The article directories
What does @Qualifier mean?1. @autowired infuses resources from the Spring container, but if it is an interface and there are two implementation classes inside the interface, if we need to use the methods of the interface, the system will not identify which implementation class we are using to implement it
conclusion
What does @Qualifier mean?
The modifier self-understanding is a supplement to @autowired, why do I think it is a supplement?Since we all know that the annotation follows @autowired, For the sake of understanding, I will consider it as a supplement. The keynote will be set here, and then we will see what kind of supplement it is.
Ii. Specific instructions
[email protected] is an injection of resources from the Spring container, but if it is an interface with two implementation classes in it, the system will not recognize which implementation class we are using if we need to use the methods of the interface
code as follows (example) :
@Autowired
// @Qualifier("PersonSeriverImpl2")
private PersonSeriver personSeriver;
public void eats() {
personSeriver.eat();
}
Look @ the Qualifier (” PersonSeriverImpl2 “)
the
I am comments yet my PersonSeriver has two implementation class, then I now run the program will offer the following wrong
under Caused by: org. Springframework. Beans. Factory. NoUniqueBeanDefinitionException: No qualifying bean of type ‘com. Itheima. Seriver. PersonSeriver’ available: expected single matching bean but found 2: personSeriverImpl, personSeriverImpl2
translation probably mean:
caused by the following reasons: Org. Springframework. Beans. Factory. NoUniqueBeanDefinitionException: no type is’ com. Itheima. Seriver. PersonSeriver qualified Bean: ‘expectations with a single match Bean, but found the 2: personSeriverImpl, personSeriverImpl2
that is to say, now into two resources, with which the resources to achieve it?The error is then reported, which is when our @Qualifier is needed to resolve the problem
- go to your implementation class and add the name @service (” PersonSeriverImpl2 “) and get the annotation off the @qualifier (” PersonSeriverImpl2 “), which is the interface method that your method needs to call
well, we’ll run our program again and it’s OK
conclusion
here is a one-sentence summary of the @qualifier annotation :
needs to inject service in Controller so how does my server have two implementation classes to distinguish between the two impls, we need to apply the @qualifier annotation, which provides a way to resolve this conflict
Read More:
- Springboot project startup exception – required a single bean, but 2 were found
- Field userrepository in com.example.demo2.service.imp.UserServiceImp required a bean of type ‘com.ex
- @Autowired injection * * required a single bean, but 2 were found**
- Start tomcat server error Context initialization failed
- org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name
- [springboot] problem failed to determine a suitable driver class
- Error: java.lang.IllegalArgumentExcept :Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required
- Error creating bean with name ‘requestMappingHandlerAdapter‘ defined in class path resource
- Failed to instantiate [org.springframework.aop.aspectj.AspectJExpressionPointcut]: No default constr
- Failed to instantiate org.mybatis.spring.SqlSessionTemplate Constructor threw exception
- org.springframework.core.NestedIOException: Failed to parse mapping resource: ‘file
- exception is java.lang.LinkageError: loader constraint violation: loader
- Introducing external configuration files into spring XML db.properties
- Failed to determine a suitable driver class error in mybatis
- ERROR 1044 (42000): Access denied for user ”@’localhost’ to database ‘mysql’
- Using global timing task cronutil in hutool tool class
- Spring configuration transaction, JUnit unit test error “failed to load ApplicationContext”
- Spring boot running appears java.lang.IllegalStateException : Failed to load ApplicationContext
- Error java.lang.IllegalArgumentException: Validation failed for query for method public abstract
- java.lang.VerifyError : inconsistent stackmap frames at branch target