@SpringBootApplication
クラスに複数の@Bean
メソッドを配置して、必要なBeanを作成しています。それらのすべては1つを除いて実行されます。その1つのBeanメソッドは決して実行されないため、対応するクラス(サービスとして注釈付けされている)は例外を伴います。org.springframework.beans.factory.NoSuchBeanDefinitionException
Springは@Beanメソッドを呼び出さない
同じクラスの他のBeanメソッドは実行されません。
consulServiceが呼び出される間、Application.java、haProxyServiceのメソッドは呼び出されません。
// 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;
}
}
問題のクラスから@Serviceを削除すると、Beanメソッドが魔法のように呼び出されます.......何が起こっているのですか –
あなたはいくつかのコードを表示する必要があります。誰もこれを確かに推測することはできません。 – Todd
要点は次のとおりです。https://gist.github.com/thomas-oo/7d129f4a042fd87a8ed33f87a8dad396 consulServiceが呼び出されている間、Application.java、haProxyServiceのメソッドは呼び出されません。 HAProxyServiceから@Serviceを削除すると、Beanメソッドが呼び出されます。しかし、問題は解決しません。 –