从另一个调用Spring服务类 [英] Calling a Spring service class from another

查看:151
本文介绍了从另一个调用Spring服务类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有两个spring bean服务类。是否可以从另一个呼叫?如果是的话,怎么做?

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

推荐答案


我的两个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?

规范方法是在第一个服务中声明对第二个服务的依赖关系只需要调用它。

The canonical approach would be to declare a dependency on the second service in the first one and to just call it.

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配置为连接在一起(核心工作) of Spring)即将 Bar 实现注入 Foo 服务。

And configure Spring to wire things together (the core job of Spring) i.e. inject a Bar implementation into your Foo service.

这篇关于从另一个调用Spring服务类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆