Background: So, I have several beans of frontend interfaces. For development, it is convenient for mocking external systems and replacing the beans interface with some implementations that produce more or less static responses. So, what I did was create an interface, a real implementation, and a stub implementation as follows:
public interface ExternalService { // ... } @Service public class ExternalServiceImpl implements ExternalService { // ... } @Service @Primary @Profile({"stub"}) public class StubExternalService implements ExternalService { // ... }
... and this works fine: if there is no stub profile, the bean stub does not load at all. If present, it perfectly replaces the real implementation due to @Primary annotation.
Problem: Now, however, I first started in a situation where I actually have two real implementations of the same interface. One of them is defined as primary, but the other can also be used by loading it from the application context.
I would still like to create a service stub to replace both of them, but this time my old way of defining a stub as @Primary does not work, because there is already one main implementation. Basically what I need is a way to not load the primary bean when the stub profile is installed, but I am losing exactly how to do this. Web searches or other questions do not seem to help.
source share