Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!