我将多个
有什么原因导致其中一个Bean方法不会运行而该类中的其他方法可以?
在Application.java中的方法haProxyService从未被调用,而consulService确实被调用。
@Bean
方法放在一个@SpringBootApplication
类中来创建所需的bean。除了一个方法外,所有方法都能正常运行。该Bean方法从未被运行过,因此,相应的类(标记为服务)会报错:org.springframework.beans.factory.NoSuchBeanDefinitionException
。有什么原因导致其中一个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;
}
}
@Services
而不进行任何手动bean创建,我该如何将字符串注入到我的类中呢? 我需要将这些字符串定义为beans吗?如果是这样,该怎么做呢? - Thomas Oo@Value
和@PropertySource
注入配置参数(网络搜索关键字提示 ;))。 - meistermeier