首页 > 解决方案 > Springboot 依赖:找到两个依赖并忽略限定符

问题描述

这是我的课:

@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

标签: javaspringspring-bootlombok

解决方案


你需要做两件事:

  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


推荐阅读