简体   繁体   English

是否可以创建多个同名的 spring bean(来自不同的接口)?

[英]is it possible to create multiple spring beans (from different interfaces) with the same name?

I'm trying to create the following structure:我正在尝试创建以下结构:

public interface A {string calculateA(){....}}

@Service("policy1")
public class APolicy1 implements A {
  @Override
  public String calculateA(){...}
}

@Service("policy2")
public class APolicy2 implements A {
  @Override
  public String calculateA(){...}
}

and another interface和另一个界面

 public interface B{string calculateB(){....}}

@Service("policy1")
public class BPolicy1 implements B{
  @Override
  public String calculateB(){...}
}

@Service("policy2")
public class BPolicy2 implements B{
  @Override
  public String calculateB(){...}
}

and the classes that uses these beans:以及使用这些 bean 的类:

@service
@Primary
public class Arouter implements A{
   @Autowired
   Map<String, A> AServices;

   @Autowired
   PolicyResolver policyResolver;

   @Override
   public String calculateA(){
     String policy = policyResolver.getPolicy();
     AServices.get(policy).CalculateA();
}

@service
@Primary
public class Brouter Implements B{
   @Autowired
   Map<String, B> AServices;

   @Autowired
   PolicyResolver policyResolver;

   @Override
   public String calculateB(){
     String policy = policyResolver.getPolicy();
     AServices.get(policy).CalculateB();
}

note: the idea here is to delegate to the correct business logic by some kind of policy for example, if i'm running in policy a context and need to trigger Service B, then the router will call BPolicy2 method.注意:这里的想法是通过某种策略委托正确的业务逻辑,例如,如果我在策略上下文中运行并且需要触发服务 B,则路由器将调用 BPolicy2 方法。

but I'm getting:但我得到:

org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'policy1' for bean class [...apackage.APolicy1] conflicts with existing, non-compatible bean definition of same name and class [...bpackage.BPolicy1]

I would expect that since these are different bean types I would be able to give them the same name我希望因为这些是不同的 bean 类型,所以我可以给它们起相同的名字

edit: I have a solution: add a prefix to the beans the name and the routers will add the prefix to the policyResolver return value, but I'm it's less elegant编辑:我有一个解决方案:在bean名称中添加前缀,路由器会将前缀添加到policyResolver返回值中,但我认为它不太优雅

@qualifier annotation can be used to differentiate between different beans. @qualifier 注解可以用来区分不同的bean。

Here is a link with small example of @qualifier annotation usage in Spring :这是一个链接,其中包含 Spring 中 @qualifier 注释用法的小示例:

https://memorynotfound.com/handling-multiple-autowire-dependencies-with-spring-qualifier/ https://memorynotfound.com/handling-multiple-autowire-dependencies-with-spring-qualifier/

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

相关问题 Spring Framework:是否可以在没有@Configuration的情况下创建具有相同@Component的两个bean? - Spring Framework: is possible to create two beans of the same @Component without @Configuration? 为不同的配置文件定义具有相同方法名称的 Spring Bean - Defining Spring Beans with same method name for different profiles 在春季,如何定义两个具有相同名称和不同类的Bean - In Spring, How to Define Two Beans, with Same Name and Different Class 是否可以在一个循环中创建 spring bean? - Is it possible to create spring beans in a cycle? 如何根据Spring中的配置创建多个相同类型的bean? - How to create multiple beans of same type according to configuration in Spring? 如何在运行时为 Spring Boot 中的不同配置文件创建多个 bean? - How to create multiple beans at runtime for different profiles in Spring Boot? 创建具有不同属性的相同类的多个bean的最简单方法 - cleanest way to create multiple beans of the same class with different properties 具有相同名称的多个EJB bean - Multiple EJB beans with same name Spring:如何从单个模板定义动态创建多个bean - Spring: how to dynamically create multiple beans from single template definition 包含具有不同组接口的bean的多个bean验证 - Multiple beans validation inside containing bean with different group interfaces
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM