【问题标题】:Spring - Autowire multiple Service class from the same Abstract ClassSpring - 从同一个抽象类自动装配多个服务类
【发布时间】:2017-07-17 09:53:19
【问题描述】:

我正在努力寻找解决问题的最佳方法。

我需要根据一个变量值在其中几个列表中选择一个服务接口(及其具体实现)。

具体实现扩展了一个抽象类。

所以我有

public abstract class AbstractService {

public void method1(){
//do stuff
}

public void method2(){
//do stuff
}

}

那么我们有

 public interface Service1{
void method1();

    void method2();

    void method3();

    void method4();
    }

    public interface Service2 implements Service{
void method1();

    void method2();

    void method3();

    void method4();
    }

    public interface ServiceN implements Service{
void method1();

    void method2();

    void method3();

    void method4();
    }

最后是实现

@Service
public class Service1Implementation extends AbstractService implements Service1 {


}

@Service
public class Service2Implementation extends AbstractService implements Service2 {


}



 @Service
    public class ServiceNImplementation extends AbstractService implements ServiceN {


    }

现在,例如,我需要在控制器中根据变量值来决定我需要哪个服务。

我的想法是自动装配控制器中的所有服务接口,然后执行类似的操作

@Controller
public class Controller{

@Autowired Service1 service1;

@Autowired Service2 service2;

//...

@Autowired ServiceN serviceN

@GetMapping("/")
public String myController(){

int variable;

switch(variable){

case 1:
service1.method()1;
break;
case 2:
service2.method1();
//....
break;
case n:
serviceN.method1();
break();

return "template";

}

}

它可以工作...但是我有几个服务类扩展了抽象类,它看起来不像是一个做得很好的工作流,有没有办法以更轻松的方式拥有它?

【问题讨论】:

  • Service1Service2Service3之间没有通用接口?
  • 不在此配置中,但我可以创建它,您的意思是所有其他人都实现的单个接口?那么我该如何决定我需要调用哪个服务呢?
  • 如果你能做到,我可以写一个依赖它的答案。但我不明白getService()的逻辑。它在正确的实例上调用method1(),而方法名称为getService()。不是很清楚。
  • 对不起,我会说得更清楚

标签: java spring


【解决方案1】:

为此目的使用@Qualifier。参考this

【讨论】:

    【解决方案2】:

    您可以将switch 语句替换为Map 查找,您将在其中提供作为键的int 值,您在switch 中使用,您将从服务实例的映射中获得值关联到。

    这个逻辑:

    public String myController(){
    
      int variable = ;
    
      switch(variable){
    
        case 1:
         service1.method();
         break;
        case 2:
         service2.method1();
         //....
         break;
       case n:
         serviceN.method1();
         break();    
      }
      ...
    }
    

    可以替换为:

    public String myController(){
       int variable = ...;
       Service service = servicesByCode.get(variable);
       service.method1(); 
      ...
    }
    

    正如解释的那样,您应该在类中添加一个Map 字段和一个在bean 自动装配后初始化映射的方法:

    private Map<Integer, Service> servicesByCode;
    
    @PostConstruct
    private void postConstruct() {
        servicesByCode = new HashMap<>();
        servicesByCode.put(1, service1);
        servicesByCode.put(2, service2);
        servicesByCode.put(3, service3);
        ...
    }
    

    【讨论】:

    • 谢谢,这背后的逻辑已经很清楚了,我的问题是要了解地图中的Service是哪一种,org.hibernate.service.Service?而service1、service2、service3是什么?实现还是接口?
    • @besmart 欢迎您。这些服务是您在 spring 控制器中声明为字段的服务。你必须把它们放在地图上。很高兴你得到它:)
    猜你喜欢
    • 2017-06-12
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多