3
I’ve been looking for solutions for Strategy Pattern with spring boot, but nothing I’ve found so far seems performatic or even functional.
I have an interface like:
public interface UserService {
User getById(Integer id);
}
And I have two separate injections:
@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
}
}
On the controller I call the interface:
@Controller
public class UserController {
@Autowired
private UserService userService;
//some methods here
}
The way it’s just the UserServiceImpl
will be instantiated and called, for it is the Primary.
The application can have two states, one in which it queries the local bank and the other where it requests a microservice. I need that when the state changes (some request in an endpoint will let me know and it’s already implemented) I change the Userservice implementation 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 state refresh Scope and it changes, without checking every request I have the right Userservice implementation in the controller.
See if this helps you: https://lofidewanto.blogspot.com.br/2013/06/creating-spring-bean-dynamically-in.html
– StatelessDev