简体   繁体   English

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

[英]Spring - how to avoid implementation lock in between collaborating services

I am writing a small framework which uses spring for DI. 我正在编写一个使用spring进行DI的小型框架。 I have a number of services which collaborate with each other. 我有许多相互协作的服务。

ServiceA has reference to ServiceB - has reference to ServiceC etc. All these are prewired in spring configuration xml. ServiceA引用了ServiceB-引用了ServiceC等。所有这些都预装在spring配置xml中。

The issue occurs when the user tries to override one of these implementations, say ServiceB with his own, say ChildServiceB. 当用户尝试覆盖其中的一种实现(例如说ServiceB和他自己的说说ChildServiceB)时,就会发生此问题。 Now, what I really want here is that the users ChildSerivceB should be wired into ServiceA. 现在,我真正想要的是应该将用户ChildSerivceB连接到ServiceA。 Can it be done in spring? 春天可以做吗? If not, what is the best design pattern for this scenario? 如果没有,那么此方案的最佳设计模式是什么?

Something like a late-binding for spring is what I am looking for. 我正在寻找像春天一样晚装的东西。

I would like the user's spring configuration needs to be as minimal as possible in such scenarios. 我希望在这种情况下,用户的弹簧配置必须尽可能小。

Other Notes: 其他说明:

I use interfaces for all my services 我将接口用于所有服务

To make it easy for people who use the default services, the spring congiuration framework-beans.xml is provided in the jar, they just need to instantiate it. 为了使使用默认服务的人员容易使用,jar中提供了spring congiuration framework-beans.xml,他们只需要实例化即可。

Users who want to override implementations can create their own beans.xml, but that is where I am stumped, how do they override the "wiring" of framework-beans.xml? 想要覆盖实现的用户可以创建自己的bean.xml,但这就是我的绊脚石,他们如何覆盖framework-beans.xml的“接线”?

The issue occurs when the user tries to override one of these implementations, say ServiceB with his own, say ChildServiceB. 当用户尝试覆盖其中的一种实现(例如说ServiceB和他自己的说说ChildServiceB)时,就会发生此问题。 Now, what I really want here is that the users ChildSerivceB should be wired into ServiceA. 现在,我真正想要的是应该将用户ChildSerivceB连接到ServiceA。 Can it be done in spring? 春天可以做吗? If not, what is the best design pattern for this scenario? 如果没有,那么此方案的最佳设计模式是什么?

1 Use Interface programming 1使用接口编程
2 Let user implement its own Service for ServiceB 2让用户为ServiceB实现自己的服务
3 Configure Bean in Spring xml 3在Spring xml中配置Bean

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

now if you want user to have its own implementation just change it to 现在,如果您希望用户拥有自己的实现,只需将其更改为

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

and in Class use interface 并在Class使用界面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM