0

I am have a confusion on how to go about with above scenario in Spring (dependency injection context)

class Login {
   String username;
   String password;
   UserAuthService userAuth;

   /* 
      assume getter and setter 
      methods for above private
      properties here
   */

}

UserAuthService is a interface which has a boolean method validate(String username , String password);

It is implemented by BasicAuthService, LDAPAuthService, MockBasicAuthService, MockLDAPAuthService; Also Assume that BasicAuthServie has a property serverAddress , as string which has the IP address of authentication server, same with LDAPAuthService.

Context of my Question is Dependency Injection and Spring ,

I understood that based on beanconfig file Spring('s IOC) will inject one of the concrete implementation of UserAuthService.

1 . In beanconfig file we can only configure one ref say ( ) Scenario: Suppose in UI user has a dropdown to select BasicAuth / LDAPAuth . User have selected LDAPAuth , how to deal with this case in spring ? since we have hardwired ref="beanIdBasicAuthService" in beanconfig file). Is there a way to dynamicaly change the ref ?

2 . Little more complex ( assume IT admin or so) , UI is now providing option to select the address of the authentication server (which is a property in the Basic/LDAP Auth Service Class) Again in beanconfig file we would have hardwired the properties. How to go about dynamicaly changing it in spring ? (other than explicitly getting the injected bean and calling setterMethod)

These scenarios are a bit confusing to me. Can someone please explain this ?

2 Answers2

0

Answer to both questions: inject a factory class instead. E.g. in your example:

class Login {
   String username;
   String password;
   UserAuthServiceFactory userAuthFactory;

   /* 
      assume getter and setter 
      methods for above private
      properties here
   */
}

class UserAuthServiceFactory {
     UserAuthService createUserAuth(String type, String remoteHost, ...) {...}
}
Alexander
  • 2,761
  • 1
  • 28
  • 33
0

Spring offers some white magic, but you are really requiring black magic here :-).

More seriously, you are right on one part: Spring can only configure a bean at bean creation time and only if it creates the bean. BTW, there is also the question of the scope: a Singleton bean will only be created once and same object will be used throughout the application, whereas with bean scope, a new object will be constructed when it is requested from the application context.

But, once you have a bean, you are free to use it and change values that were initialized by Spring.

That is simply the way to go for second question: inject the AuthService(s) in the UI, and simply change the server address through the UI (with a bit of synchronization...): the new address will be used from that moment.

For the first question, I would first say that is is uncommon to inject an authentication service in a class like your Login class. First it forces you to create any Login object through a request to the application context which unnecessarily ties your application to Spring classes. IMHO a better design would be to have a master authentication class injected with a container of concrete authentication objects, and delegating actual authentication to them. It is what Spring Security does with an AuthenticationManager that delegates actual authentication to AuthenticationProviders.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252