Spring | Autowiring Qualifier
Spring provides @Qualifier annotation to refer correct bean while doing autowiring.
Suppose we have interface IService which has two concrete implementation.
Suppose we have interface IService which has two concrete implementation.
When we try to Autowire IService object in any other class it will throw exception as there are two beans.
Spring Qualifier is used in such situation to place object of correct implementation class to autowired object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IService { | |
doSomething(); | |
} | |
@Component("iServiceImplOne") | |
public class IServiceImplOne implements IService { | |
doSomething() { | |
//some code | |
} | |
} | |
@Component("iServiceImplTwo") | |
public class IServiceImplTwo implements IService { | |
doSomething() { | |
//some code | |
} | |
} | |
@Component("someClass") | |
public class SomeClass { | |
@Autowire | |
private IService iservice; | |
//some other code | |
} | |
@Component("someClass") | |
public class SomeClass { | |
@Autowire | |
@Qualifier("iServiceImplTwo") | |
private IService iservice; | |
//some other code | |
} |
Nice
ReplyDelete