【问题标题】:Springboot dependency: two dependencies found and Qualifier ignoredSpringboot 依赖:找到两个依赖并忽略限定符
【发布时间】:2021-09-23 11:49:27
【问题描述】:

这是我的课:

@Repository
@RequiredArgsConstructor
@Slf4j
public class ServeiTerritorialCatalegsClientRepositoryImpl implements ServeiTerritorialCatalegsClientRepository {

    @Qualifier("catalegsMarshaller") private final Marshaller marshaller;

    //...
}

我的bean定义是:

@Bean
public Marshaller oidMarshaller() throws JAXBException {
   //...
}

@Bean
public Marshaller catalegsMarshaller() throws JAXBException {
  //...
}

我收到了这条消息:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 3 of constructor in cat.catsalut.hes.mpi.hazelcast.loader.repository.ServeiTerritorialCatalegsClientRepositoryImpl required a single bean, but 2 were found:
    - oidMarshaller: defined by method 'oidMarshaller' in class path resource [cat/catsalut/hes/mpi/hazelcast/loader/configuration/ServeiTerritorialConfiguration.class]
    - catalegsMarshaller: defined by method 'catalegsMarshaller' in class path resource [cat/catsalut/hes/mpi/hazelcast/loader/configuration/ServeiTerritorialConfiguration.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

【问题讨论】:

标签: java spring spring-boot lombok


【解决方案1】:

在 ServeiTerritorialCatalegsClientRepositoryImpl 类中,似乎有一个构造函数,其第三个参数的类型为 Marshaller。 因此,在构造函数中定义参数时,请为参数类型 Marshaller 使用限定符属性,如下所示:

public ServeiTerritorialCatalegsClientRepositoryImpl(parameter1, paramter2, @Qualifier("catalegsMarshaller") Marshaller marshaller1)

【讨论】:

    【解决方案2】:

    Lombok 不会将注释从字段复制到构造函数参数或属性访问器。您现在需要自己提供构造函数。

    【讨论】:

      【解决方案3】:

      你需要做两件事:

      1. 更新 lombok.config 并添加这个 -

        lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Qualifier

      2. 使用@Qualifier 并提供一个合适的唯一名称。例如:

      @Repository
      @RequiredArgsConstructor
      @Slf4j
      public class MyImplClass{
        @Qualifier("Myqualifier1") MyBean bean;
       
        Person getPerson()
      }
      

      在此处查看 lombok 配置指南 - lombokconfig

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-06
        • 1970-01-01
        • 1970-01-01
        • 2022-09-23
        相关资源
        最近更新 更多