微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何解决 NonUniqueBeanException:在微米中找到多个可能的 bean 候选者

如何解决如何解决 NonUniqueBeanException:在微米中找到多个可能的 bean 候选者

我收到以下 Micronaut 代码NonUniqueBeanException: Multiple possible bean candidates found:

@Context
@Slf4j
@AllArgsConstructor
public class RemoteService {
   private final Provider<Session> remoteSessionFactory;
}

我有 2 个 Provider 的实现

@Slf4j
@Prototype
@AllArgsConstructor
public class RemoteSessionFactoryA implements Provider<Session> {
    //some code here
}

@Slf4j
@Prototype
@AllArgsConstructor
public class RemoteSessionFactoryB implements Provider<Session> {
    //some code here
}

我什至尝试过这样但仍然得到同样的错误

private final @Named("remoteSessionFactoryA) Provider<Session> remoteSessionFactory;

请建议如何解决此问题。

问候

解决方法

advisory_agreement__advisory,value_1_29,3rd_party_key 注释应该是构造函数参数的一部分。由于让 Lombok 生成构造函数,因此无法通过 Lombok 设置 Named 注释。

我建议自己编写构造函数,例如:

@Named

Micronaut 无法注入 bean,因为名称与命名约定不匹配。 Micronaut 文档指出:

Micronaut 能够在前面的示例中注入 V8Engine,因为: @Named 限定符值 (v8) + 类型被注入的简单名称 (Engine) == (case-insensitive) == Engine (V8Engine) 类型的 bean 的简单名称 您也可以在 bean 的类级别声明 @Named 来显式定义 bean 的名称。

因此,如果您将名称放在源 bean 上,Micronaut 将选择您定义的名称。

@Context
@Slf4j
public class RemoteService {
   private final Provider<Session> remoteSessionFactory;

   public RemoteService(@Named("remoteSessionFactoryA") Provider<Session> remoteSessionFactory) {
       this.remoteSessionFactory = remoteSessionFactory;
   }
}

或者创建限定符注释

@Slf4j
@Prototype
@AllArgsConstructor
@Named("remoteSessionFactoryA") 
public class RemoteSessionFactoryA implements Provider<Session> {
    //some code here
}

@Slf4j
@Prototype
@AllArgsConstructor
@Named("remoteSessionFactoryB") 
public class RemoteSessionFactoryB implements Provider<Session> {
    //some code here
}

然后像这样注入

@Qualifier
@Retention(RUNTIME)
public @interface FactoryA {
}

@Qualifier
@Retention(RUNTIME)
public @interface FactoryB {
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。