Consider the following code:
@Service
class UserSpecificService {
@Autowired ServiceA serviceA;
@Autowired ServiceB serviceB;
@Autowired User user;
public void doUserSpecificThings() {
// consume serviceA, serviceB and user here
}
}
@Service class ServiceA {}
@Service class ServiceB {}
@Service
class Facade {
public void doThingsForUser(Long userId) {
User user = userService.getById(userId);
UserSpecificService userSpecificService = ...; // !!!
userSpecificService.doUserSpecificThings();
}
}
The question is about line marked with // !!!. Is there any way I can construct UserSpecificService without implementing my own factory which is aware of ServiceA and ServiceB, and only requires an instance of User to instantiate a UserSpecificService?
If it matters – I’m going to use the Facade with Spring MVC, though I’d like it not to rely on this.
Thanks!
Here’s a suggestion:
userSpecificService(I guess these are ServiceA and serviceB) have a common interfacedoUserSpecificThing()andisEligibleFor(user)@Autowired List<YourInterface> listdoThingsForUser()method iterate the list of all instances of the given interface, and for each of them check if it is eligible for the given user. If it is – invoke the method.