Springboot dependency: two dependencies found and Qualifier ignored

Viewed 361

This is my class:

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

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

    //...
}

My bean definition is:

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

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

I'm getting this message:

***************************
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
3 Answers

You will need to do two things:

  1. Update lombok.config and add this -

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

  2. Use @Qualifier and provide a suitable unique name. Ex:

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

See lombok config guide here - lombokconfig

in class ServeiTerritorialCatalegsClientRepositoryImpl it seems there is a constructor whose 3rd parameter is of type Marshaller. So while defining the parameter inside the constructor use qualifier ttribute for the paramter type Marshaller like below:

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

Lombok does not copy annotations from fields onto constructor parameters or property accessors. You'll need to provide the constructor yourself for now.

Related