java - Spring根据请求值使用不同的bean

标签 java spring spring-mvc spring-boot inversion-of-control

我想根据请求对象中的信息,在根级别 bean 中使用特定(限定) bean,并在每个父 bean 中使用 bean。

假设我有针对特定类型请求的特定实现,并且我在运行时获得决策值(请求主体对象中的信息)。我需要结构化我的代码,以完全满足我的以下要求。

考虑以下示例 Web 示例,如果请求中的标识值要求 B 的实现,我需要打印 B 的方法,如果专门请求 C 的实现,则需要打印 C 的方法。 InputPayload 具有作为 systemType 的标识属性来决定系统特定的实现。

----------Controller class---------

@RestController
public class ResourceController {
  @Autowired
  private RootInterface i;   //Class A will be injected here

  @PostMapping(path = "/update", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public Result callInterfaceMethod(@RequestBody InputPayload inputPayload) {
    return i.callRootInterfaceMethod(inputPayload);
  }
}

----------Service classes---------

@Service
public class A implements RootInterface {
  @Autowired
  private D d;

  @override
  public Result callRootInterfaceMethod(InputPayload inputPayload) {
    return d.methodToImplement(inputPayload);
  }
}

public interface D {
  Result methodToImplement(InputPayload inputPayload);
}

@Service
public class B implements D {
  @override
  private Result methodToImplement(InputPayload inputPayload) {
    system.out.println("Class B method is called");
    ....
  }
}

@Service
public class C implements D {
  @override
  private Result methodToImplement(InputPayload inputPayload) {
    system.out.println("Class C method is called");
    ....
  }
}

如果我没有的话。服务类,它们被注入(inject)到另一个父服务类中(假设最小深度为 5)。如何使用调用各自的服务类而不是使用带有多个变量声明的@Qualifier注释或创建不同的引用?如何调用相应的实现方法而不是任何条件 block ?

最佳答案

您可以:

  • 在 D 接口(interface)上引入方法,该方法将检查实现是否可以根据输入的属性处理输入

    public interface D {
      Result methodToImplement(InputPayload inputPayload);
      boolean canProcessInput(InputPayload inputPayload);
    }
    
    //autowire all beans of type D
    @Autowired
    private List<D> dList;
    
    @Override
    public Result callRootInterfaceMethod(InputPayload inputPayload) {
      for(D d: dList) {
        if(d.canProcessInput(inputPayload)) {
          return d.methodToImplement(inputPayload);
        }
      }
      // if there is no bean that is capable of processing the input payload
      return null;
    }
    
  • Autowiring 所有服务并为每种类型的服务添加切换条件,如下所示

    // autowire all beans with their names
    @Autowired
    private Map<String, D> dMap;
    
    @Override
    public Result callRootInterfaceMethod(InputPayload inputPayload) {
      switch(inputPayload.getType()) {
        case A: { 
          return dMap.get("A").methodToImplement(inputPayload); 
        }
        case B: { 
          return dMap.get("B").methodToImplement(inputPayload); 
        }
        // if there is no bean that is capable of processing the input payload
        default: return null;
      }
    }
    

在我看来,第一个选项更清晰且可扩展

关于java - Spring根据请求值使用不同的bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46543172/

相关文章:

java - 实现类 Java

java - 使用普林斯顿的源代码在 cmd 中找不到或加载主类错误

java - 如何在 Java 中运行 .sql 脚本(从文件)并使用 Spring 返回结果集?

java - 在需要 .txt 文件的 html 中嵌入 .jar 文件 (java)

Java (Eclim + Vim) "system.out.print"不工作

java - Spring Boot 和上下文路径

java - 在不依赖框架的情况下理解 java 中的依赖注入(inject)。它在纯 Java 代码中是什么样子的?

java - 使用 Ajax 将数据添加到表行

java - 使用 Spring Security 注解 isFullyAuthenticated v/s hasRole

spring - 添加没有头像的学生