I've been looking for strategy pattern solutions with spring boot, but nothing I've found so far seems performative or even functional.
I have an interface like:
public interface UserService {
User getById(Integer id);
}
And I have two different implementations:
@Primary
@Service("userService")
public class UserServiceImpl implements UserService{
@Override
public User getById(Integer id){
//todo here
}
}
@Service("userRemoteService")
public class UserRemoteServiceImpl implements UserService{
@Override
public User getById(Integer id){
//todo here
}
}
In the controller I call the interface:
@Controller
public class UserController {
@Autowired
private UserService userService;
//some methods here
}
As it is only the UserServiceImpl
will be instantiated and called because it is the primary.
The application can have two states, one in which it queries the local bank and another where it requests a microservice. I need to change the implementation of the UserService that will be consumed by the controller.
I found some solutions, most are versions of if else. I need to find some solution that when I do a refresh scope of the state and it changes, without checking every request I have the correct implementation of the UserService in the controller.