Spring | Autowiring Qualifier

Spring provides @Qualifier annotation to refer correct bean while doing autowiring.

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.
 
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
}
Now, above iservice object will hold object of IServiceImplTwo class.

Comments

Post a Comment

Popular posts from this blog

Spring | Using TIBCO EMS with Spring framework

TIBCO | For Loop - Accumulate output

TIBCO | JNDI Server & JMS Instance creation