How to dynamically get the bean of same interface based on criteria
I have an interface and class implementation like the following:
I would like when the program accepts the authentication request passing
1. systemId
2. userId
3. password
based on the systemId, the program will retrieve the corresponding bean by Spring, e.g. if systemId = quot;SystemAquot;, SysAAuthenticateDAOImpl will be returned back to program.
then, it's more generic that even there's a new system, say SystemC, i need to provide the implementation and some configuration on Spring config file only.
public interface ISystemAuthenticateBO { public boolean login(String loginId, String password);
}
then, i have several implementation of some systems, say:
public class SysAAuthenticateDAOImpl implements ISystemAuthenticateDAO { public int login(String loginId, String password) { // real authenticate logic of System A }
}
public class SysBAuthenticateDAOImpl implements ISystemAuthenticateDAO { public int login(String loginId, String password) { // real authenticate logic of System B }
}main program:
Should inject with corresponding authentication class based on system id mapping.
public void setAuthenticateBO(ISystemAuthenticateBO authenticateBO) { this.authenticateBO = authenticateBO; }
Is there any chance you can supply more information on what you are trying to do here?
Originally Posted by mullinmain program:
Should inject with corresponding authentication class based on system id mapping.
It's probably easier / more natural to inject a proxy that has the right interface - you can do that at configuration time. Then the proxy can return the correct implementation A,B,C,etc. when asked. You might need to look at docs for TargetSource and have a play around with an implementation that knows how to identify the user / systemId fro myour example. |