Spring没有调用 @Bean 方法

4
我将多个@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;
    }
}

1
当我从有问题的类中删除@Service注解时,Bean方法神奇地被调用了……发生了什么? - Thomas Oo
3
您需要展示一些代码。没有人能够确定地猜测问题的答案。 - Todd
这是一个要点:https://gist.github.com/thomas-oo/7d129f4a042fd87a8ed33f87a8dad396Application.java中的方法haProxyService从未被调用,而consulService确实被调用。当我从HAProxyService中删除@Service时,bean方法现在被调用了...但我的问题仍然没有解决。 - Thomas Oo
2
您正在将手动bean创建与@Bean和类注释(@Service)混合使用进行组件扫描。这会导致重复的实例,可能会导致上述异常(因为找到了多个候选项)。 - meistermeier
@meistermeier,谢谢您,很抱歉我对Spring还不太熟悉......但是,我明白您的意思。然而,如果我将它们标记为@Services而不进行任何手动bean创建,我该如何将字符串注入到我的类中呢? 我需要将这些字符串定义为beans吗?如果是这样,该怎么做呢? - Thomas Oo
1
目前Spring页面的文档已经无法访问,但是你可以使用@Value@PropertySource注入配置参数(网络搜索关键字提示 ;))。 - meistermeier
1个回答

5

移除 @Service 注释。

您正在混合手动创建Bean和使用@Bean和类注释 (@Service, @Controller, @Component等) 进行组件扫描,这会导致实例重复。

否则,如果您想保留 @Service 并且不进行手动bean创建,您应该删除带有 @Bean 注释的方法,并使用 @Value 注释注入字符串值。

例如:

// 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;

}

完成此操作后,我建议您阅读关于外部配置的相关文章,这样您就可以在application.properties文件中定义这些字符串,并从轻松更改它们而无需重新编译应用程序中受益。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接