I have encountered a scenario in my application where I am calling a library class method which uses autowiring with @Qualifier
annotation. In my case, the behavior is exact same but the autowired bean should be different. This autowired bean simply calls a REST service.
I have tried to show the scenario with few simple classes:
public interface IMessage {
public String getMessage();
}
public class HelloMessage implements IMessage {
public String getMessage() {
return "Hello";
}
}
public class HiMessage implements IMessage {
public String getMessage() {
return "Hi";
}
}
public class PrintMessage {
@Qualifier("helloMessage")
@Autowired
private IMessage message;
public void service() {
System.out.println(message.getMessage());
}
}
Suppose all these classes belong to library. The only thing I am trying to achieve is HiMessage
bean to be autowired in PrintMessage
class. Overriding this class would be simple solution but since the service()
method behavior is exact same so I don't want to override it just for using different autowired bean.