gpt4 book ai didi

java - 从另一个调用 Spring 服务类

转载 作者:搜寻专家 更新时间:2023-10-31 20:07:02 24 4
gpt4 key购买 nike

我的项目中有两个 spring bean 服务类。是否可以从另一个调用一个?如果是,如何实现?

最佳答案

I have two spring bean Service classes in my project. Is it possible to call on from another? if yes, how it can be done?

规范的方法是在第一个服务中声明对第二个服务的依赖并调用它。

public class FooImpl implements Foo {
private Bar bar; // implementation will be injected by Spring

public FooImpl() { }
public FooImpl(Bar bar) { this.bar = bar; }

public void setBar(Bar bar) { this.bar = bar; }
public Bar getBar() { return this.bar; }

public void doFoo() {
getBar().doBar();
}
}

并配置 Spring 将所有东西连接在一起(Spring 的核心工作),即将 Bar 实现注入(inject)到您的 Foo 服务中。

关于java - 从另一个调用 Spring 服务类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3648635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com