1

I need to know how to inject a dependency dynamically in spring configuration file. For an example I have a business logic class called 'Class A'. Inside that class, it handle a method called 'doSomething()'. According to the application this method can perform in two different ways(two type of implementations for same method). So I have declared a interface called 'Manager' with this method and create two implementation classes for same interface. Lets called them 'Impl1' and 'Impl2' which implement interface 'Manager'.

Interface Manager{
 void doSomething();
}

Class Impl1 implements Manager{ 

 public void doSomething(){
  //doIt like this way
 }

 Class Impl2 implements Manager{ 

 public void doSomething(){
  //doIt like that way
 }
}

Class A has a reference to 'Manager' interface called 'manager' which will be set at the deploy time via Spring DI. That injection can be either Impl1 object or Impl2 object. It will decide by the end user. So I have to offer two options to end user where he or she can decide which way he or she wants handle this 'doSomething()' method and according to his or her choice i'm going to inject relevant implementation class(Impl1 or Impl2).

Class A{ 

 private Manager manager; //this can be either Impl1 or Impl2

 public void setManager(Manager manager){
  this.manager = manager;
 }

 public void performLogic(){
  manager.doSomething();
 }
}

End user have only one chance to make that choice and after that application will use that implementation forever unless user wants to install a fresh copy of application again. How do i inject relevant implementation dynamically according to the user's choice in spring xml file.What is the best way to handle this situation? Ideas are welcome.

Thanks all in advance!

2
  • Just want to say - solution using @Profiles more preferable Commented Mar 7, 2013 at 11:38
  • hi, yes, but i feel @profile is more preferable to large scale.I just need to switch between two classes only for whole app. so i found that Nandkumar Tekale's answer is quite suitable to my case. btw thanks for your support! Commented Mar 7, 2013 at 11:45

2 Answers 2

5

You need to use Spring profiles.

Use two profiles, wrap each implementation to one of them. Then at start time active one of profiles (using system properties for example).

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for ur answer! I found another way to do this. check accepted answer.
yes, but in case of Qualifier solution you application will create and handle 2 beans. In case of profiles - only one required.
You might have gone with the other approach, but that really did not answer the question as it is not "dynamic". That one gives you two static instances, one for each manager types. [The string used for @Qualifier can not even be an environment variable or anything flexible.] Spring profiles may not be your implementation choice, but it is the answer to your question.
1

What do you think something like below :

Class A{ 

 @Autowired 
 @Qualifier("Impl1")
 private Manager manager1; // Impl1

 @Autowired 
 @Qualifier("Impl1")
 private Manager manager2; // Impl2

 // getter setter for manager1 and manager2

 public void performLogic(){
  getUserChoiceManager().doSomething();
 }

 private Manager getUserChoiceManager() {
   // return manager according to user choice
 }
}

1 Comment

thanks Nandkumar Tekale for your answer! I decided to go with this solution. thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.