如何在同一个应用程序中使用一个类的不同版本?
我目前正在开发一个 Java 应用程序,该应用程序应该能够同时使用一个类的不同版本(因为多租户支持)。我想知道,有什么好的方法来管理这个吗?我的基本方法是拥有一个接口,比如说 Car,并实现不同的版本,如 CarV1、CarV2 等。每个版本都有自己的类。
我认为我的方法有点奇怪。但我没有找到任何与这个主题相关的文献,但我实际上不知道我应该搜索什么。
I'm currently working on a Java application which should have the capability to use different versions of a class at the same time (because of multi tenancy support). I was wondering, is there any good approach to manage this? My basic approach is to have an interface, lets say Car, and implement the different versions as CarV1, CarV2, and so on. Every version gets its own class.
My approach is kind of wiered, I think. But I didn't found any literature regarding to this topic, but I actually don't know what I should search for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
界面思路很谨慎。将其与一个工厂结合起来,该工厂可以根据某些外部输入(例如租户 ID)生成所需的实现实例。如果您不需要在应用程序的同一运行实例中支持多个租户,您还可以使用 JDK 中的 ServiceLocator 之类的东西,它允许使用基于文件的配置方法。
如果您在应用程序服务器中运行,请考虑启动多个实例,每个实例都针对不同的客户端进行配置。然后服务器将负责实例的分离,就很好了。
否则,如果您确实认为在非 Java EE 应用程序中同时(在运行时)需要多个实现,那么这是一个棘手的问题。也许您想考虑一下 OSGi 容器,它提供了具有多个版本的类的功能。然而,如果您还不熟悉的话,这样的方法会显着增加复杂性。
The interface idea is prudent. Combine it with a factory that can produce the required implementation instance depending on some external input, e. g. the tenant-id. If you don't need to support multiple tenants in the same running instance of the application, you could also use something like the
ServiceLocator
from the JDK which allows to use a file-based configuration approach.If you are running in an application server, consider just firing up multiple instances, each configured for a different client. The server will then take care of the separation of instances, just fine.
Otherwise, if you really think you need multiple implementations at the same time (at runtime) in a non-Java EE application, this is a tricky problem. Maybe you want to consider a look at OSGi containers, which provide features for having multiple versions of a class. However, an approach like this add significant complexity, if you are not already familiar with it.
理论上,您可以使用多个类加载器(例如 JBoss)来处理此问题。
但是:我强烈建议不要自己实现这一点。这是一个相当复杂的问题,而且很容易出错。如果您谈论的是 Web 应用程序,则可以为每个租户创建一个 Web 应用程序实例。如果您正在开发独立应用程序,则应检查每个租户运行一个实例是否可行。
In theory you can handle this using multiple class loaders like JBoss for example does.
BUT: I would strongly advise against implementing this yourself. This is a rather complicated matter and easily gotten wrong. If you are talking about a web application, you can instead create one web app instance per tenant. If you are working on a stand-alone app, you should check, if running one instance per tenant might be feasible.