简体   繁体   English

Spring配置文件中的动态依赖项注入

[英]Dynamic dependency injection in spring configuration file

I need to know how to inject a dependency dynamically in spring configuration file. 我需要知道如何在spring配置文件中动态注入依赖项。 For an example I have a business logic class called 'Class A'. 例如,我有一个称为“ Class A”的业务逻辑类。 Inside that class, it handle a method called 'doSomething()'. 在该类内部,它处理称为“ 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. 因此,我已使用此方法声明了一个名为“ Manager”的接口,并为同一接口创建了两个实现类。 Lets called them 'Impl1' and 'Impl2' which implement interface 'Manager'. 让我们将它们称为“ Impl1”和“ Impl2”,它们实现了接口“ 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. A类具有对称为“ manager”的“ Manager”界面的引用,该界面将在部署时通过Spring DI进行设置。 That injection can be either Impl1 object or Impl2 object. 该注入可以是Impl1对象或Impl2对象。 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). 因此,我必须为最终用户提供两个选项,使他或她可以决定用哪种方式来处理此“ doSomething()”方法,并根据他或她的选择,我将注入相关的实现类(Impl1或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? 我如何根据用户在spring xml文件中的选择动态注入相关的实现。解决这种情况的最佳方法是什么? Ideas are welcome. 欢迎提出想法。

Thanks all in advance! 提前谢谢大家!

You need to use Spring profiles . 您需要使用Spring配置文件

Use two profiles, wrap each implementation to one of them. 使用两个配置文件,将每个实现包装到其中一个。 Then at start time active one of profiles (using system properties for example). 然后在启动时激活配置文件之一(例如,使用系统属性)。

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
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM