Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring not calling @Bean method

I'm putting multiple @Bean methods in a @SpringBootApplication class to create required beans. All of them run except for one. That one Bean method is never run and thus, the corresponding class (which is annotated as a Service) complains with an exception: org.springframework.beans.factory.NoSuchBeanDefinitionException

Any reason why one Bean method will not run while the others in that same class do?

The method in Application.java, haProxyService is never called while consulService does get called.

// Application.java:
@SpringBootApplication
public class Application {
    //Config for services
    //Consul
    String consulPath = "/usr/local/bin/com.thomas.Oo.consul.consul";
    String consulConfPath = "/root/Documents/consulProto/web.json";

    //HAProxy
    String haproxyPath = "/usr/local/bin/haproxy";
    String haproxyConfFilePath = "/root/Documents/consulProto/haproxy.conf";

    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ConsulService consulService(){
        return new ConsulService(consulPath, consulConfPath);
    }

    @Bean
    public HAProxyService haProxyService(){
        return new HAProxyService(haproxyPath, haproxyConfFilePath);
    }
}


// ConsulService.java
@Service
public class ConsulService extends BaseService {
    String executablePath;
    String confFilePath;

    public ConsulService(String consulPath, String confFilePath) {
        this.executablePath = consulPath;
        this.confFilePath = confFilePath;
    }
}


// HAProxyService.java
@Service
public class HAProxyService extends BaseService {
    String executablePath;
    String confFilePath;

    public HAProxyService(String executablePath, String confFilePath) {
        this.executablePath = executablePath;
        this.confFilePath = confFilePath;
    }
}
like image 304
Thomas Oo Avatar asked Oct 29 '25 18:10

Thomas Oo


1 Answers

Remove the @Service annotations.

You are mixing manual bean creation with @Bean and class annotations (@Service, @Controller, @Component, etc.) for the component scan. This leads to duplicated instances.

Otherwise, if you want to leave @Services and not to do manual bean creation, you should remove the methods with @Bean annotation and inject string values using @Value annotation.

Example:

// Application.java:
@SpringBootApplication
public class Application {
    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }
}

// HAProxyService.java
@Service
public class HAProxyService extends BaseService {

    @Value("/usr/local/bin/haproxy")
    private String executablePath;
    @Value("/root/Documents/consulProto/haproxy.conf")
    private String confFilePath;

}

// ConsulService.java
@Service
public class ConsulService extends BaseService {

    @Value("/usr/local/bin/com.thomas.Oo.consul.consul")
    private String executablePath;
    @Value("/root/Documents/consulProto/web.json")
    private String confFilePath;

}

After doing this I would recommend you to read about externalizing your configuration, so you can define these strings in application.properties file and benefit from changing them easily without recompiling your application.

like image 90
naXa Avatar answered Nov 01 '25 08:11

naXa