Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
Creating only one bean instance from interface

I have a interface named IpAssginer and two classes that implement IpAssginer named CloudIpAssginer & DHCPClientService.

I'm trying to set a default IpAssginer to use based on some information on the system startup.

The following code does it successfully but two instances of the selected bean are being created:

@Configuration
public class BeanConfiguration {
    
    @Autowired
    private ShellUtilsService shellUtilsService;
    @Autowired
    private IpAssginer cloudIpAssginer;
    @Autowired
    private IpAssginer dhcpClientService;
    
    @Bean(name = "ipAssginer")
    public IpAssginer ipAssginer() {
    HwType hwType = shellUtilsService.getHwType();
    IpAssginer ipAssginer = hwType.isInCloud() ? cloudIpAssginer : dhcpClientService;
    Log.Management.info("{} IpAssginer created with type: {}", this.getClass().getSimpleName(),
        ipAssginer.getIPAssginerType());
    return ipAssginer;
    }

}

any ideas how I can do the same but create only one instance from the selected bean?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can use @Bean annotation to create spring bean as well, remove the stereotype annotations from CloudIpAssginer and DhcpClientService class, and then use @Bean annotation in @Configuration class

@Configuration
public class BeanConfiguration {

    @Autowired
    private ShellUtilsService shellUtilsService;


    @Bean(name = "ipAssginer")
    public IpAssginer ipAssginer() {

        HwType hwType = shellUtilsService.getHwType();
        IpAssginer ipAssginer = hwType.isInCloud() ? new CloudIpAssginer() : new DhcpClientService();
        Log.Management.info("{} IpAssginer created with type: {}", this.getClass().getSimpleName(),
        ipAssginer.getIPAssginerType());
        return ipAssginer;

    }

}
over 4 years ago · Santiago Trujillo Relatório

0

A solution that worked for me:

I excluded the IpAssigner from the context and it worked with the following code:

@Configuration
public class BeanConfiguration {

    @Autowired
    private ShellUtilsService shellUtilsService;
    
    @Bean(name = "ipAssginer")
    @DependsOn("cloudApiService")
    public IpAssigner ipAssginer() {
    HwType hwType = shellUtilsService.getHwType();
    IpAssigner ipAssginer = hwType != null && hwType.isInCloud() ? new CloudIpAssginer()
        : new DHCPClientService();
    return ipAssginer;
    }

    
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda