gpt4 book ai didi

java - Spring - 如何避免协作服务之间的实现锁定

转载 作者:行者123 更新时间:2023-11-30 11:55:32 24 4
gpt4 key购买 nike

我正在编写一个使用 spring 进行 DI 的小型框架。我有许多相互协作的服务。

ServiceA 引用了 ServiceB - 引用了 ServiceC 等。所有这些都预装在 spring 配置 xml 中。

当用户试图覆盖这些实现之一时,会出现问题,比如用他自己的 ServiceB,比如 ChildServiceB。现在,我真正想要的是用户 ChildSerivceB 应该连接到 ServiceA。 Spring 可以做吗?如果不是,这种情况的最佳设计模式是什么?

我正在寻找类似 Spring 后期绑定(bind)的东西。

我希望在这种情况下用户的 spring 配置需要尽可能少。

其他注意事项:

我为所有服务使用接口(interface)

为了方便使用默认服务的人,jar中提供了spring congiuration framework-beans.xml,他们只需要实例化即可。

想要覆盖实现的用户可以创建自己的 beans.xml,但这正是我感到困惑的地方,他们如何覆盖 framework-beans.xml 的“连接”?

最佳答案

The issue occurs when the user tries to override one of these implementations, say ServiceB with his own, say ChildServiceB. Now, what I really want here is that the users ChildSerivceB should be wired into ServiceA. Can it be done in spring? If not, what is the best design pattern for this scenario?

1 使用接口(interface)编程
2 让用户为ServiceB
实现自己的Service3在Spring xml中配置Bean

<bean name="serviceB" class="com.something.DefaultServiceBImpl"/>

现在,如果您希望用户拥有自己的实现,只需将其更改为

<bean name="serviceB" class="com.something.CustomServiceBImpl"/>

并在类中使用接口(interface)

关于java - Spring - 如何避免协作服务之间的实现锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4956483/

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