我在@SpringBootApplication
類中放置了多個@Bean
方法來創建所需的bean。除了一個以外,他們都運行。一個Bean方法永遠不會運行,因此,相應的類(它被註釋爲Service)會投訴異常:org.springframework.beans.factory.NoSuchBeanDefinitionException
春天不叫@Bean方法
任何一個Bean方法在同一類中的其他方法不會運行的任何原因?
Application.java中的方法haproxyService永遠不會調用,而consulService不會被調用。
// 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 Application.java中的方法,haProxyService永遠不會調用,而consulService不會被調用。當我從HAProxyService中刪除@Service時,bean方法現在被調用..但我的問題仍未解決 –