简体   繁体   English

如何同时使用同一类的不同版本

[英]How to use concurrently different version of the same classes

I have to provide an interface to a set of web services for which I have generated the code stubs and object binding. 我必须提供一组Web服务的接口,为此我生成了代码存根和对象绑定。 Many of the transactions share a common object model and thus a large part of the generated code overlaps. 许多事务共享一个公共的对象模型,因此生成的代码大部分重叠。 Normally this would not be a concern as I would just re-use the same code since the wsdl would link the same xsd files. 通常,这不会引起任何问题,因为wsdl将链接相同的xsd文件,因此我将仅重复使用相同的代码。

Unfortunately here the provider of these services has separated the xsd so that each services binds to a separate set (basically a copy) of the same files. 不幸的是,这些服务的提供者在这里将xsd分开了,因此每个服务都绑定到相同文件的单独集合(基本上是副本)。 In their specification they state that the implementation of the client side should isolate each web-services for easier maintenance. 他们在其规范中指出,客户端的实现应隔离每个Web服务,以便于维护。 Basically they want to be able to modify the xsd for a single web service while leaving all others untouched. 基本上,他们希望能够修改单个Web服务的xsd,同时保持所有其他功能不变。

The question is as follows : 问题如下:

How could I integrate these different set of generated classes in the same program so that each services exposes their functionality without interfering with their brethren ? 我如何才能将这些不同的生成类集集成到同一程序中,以使每个服务在不干扰其兄弟的情况下公开其功能?

One solution I thought was to create a facade for each of them that would expose the desired functionality and object model so that the actual implementation remains hidden. 我认为一种解决方案是为每个对象创建一个外观,以暴露所需的功能和对象模型,从而使实际实现保持隐藏。 Then though clever use of custom class loaders each facade would load a specific jar containing the generated code for this particular service. 然后,尽管巧妙地使用了自定义类加载器,但每个外观都将加载一个特定的jar,其中包含该特定服务的生成代码。

Any thought ? 任何想法 ? ideas ? 想法? what was your experience facing a similar problem ? 您面对类似问题的经历是什么?

thanks 谢谢

Have you considered using OSGi based bundles (eg Eclipse Equinox runtime)? 您是否考虑过使用基于OSGi的捆绑软件(例如Eclipse Equinox运行时)? Using an OSGi based implementation each set of generated classes would be in a separate bundle (.jar file) that gets its own class loader and can selectively export parts of its API to the rest of the application. 使用基于OSGi的实现,每组生成的类将位于一个单独的捆绑包(.jar文件)中,该捆绑包具有自己的类加载器,并且可以将其API的一部分选择性地导出到应用程序的其余部分。 So it provides a ready implementation of the solution you proposed. 因此,它为您提出的解决方案提供了现成的实现。

In general, the easiest way to handle this is to use a jaxb binding file (or the -p param to CXF's wsdl2java tool) to map the generated code into a package specific for the particular service. 通常,处理此问题的最简单方法是使用jaxb绑定文件(或CXF的wsdl2java工具的-p参数)将生成的代码映射到特定于特定服务的包中。 Thus, each service would have it's own objects to work with. 因此,每个服务都将具有其自己的对象。

The complexity would come if you have to integrate the various services. 如果您必须集成各种服务,则将变得很复杂。 Like take the return from one to call into another. 就像从一个人那里获得回报,再呼叫另一个人一样。 Since the objects would be completely different, you would need to write code to copy data from one to the other. 由于对象将完全不同,因此您需要编写代码以将数据从一个复制到另一个。 Possibly not fun. 可能不好玩。 :-( :-(

For Java two classes are different from eachother when they are loaded by separate classloaders. 对于Java,当两个类由单独的类加载器加载时,它们彼此不同。 You can take advantage of this by loading the 2 versions of the classes yourself from 2 URLClassLoaders which get the URL of the respective jars as classpath. 您可以通过从2个URLClassLoaders加载类的2个版本来利用这一点,这两个类将各个jar的URL作为类路径。 You might not need to build 2 facades if both sets of objects implement the same interfaces. 如果两组对象都实现相同的接口,则可能不需要构建2个立面。

update 更新

Yes, the jars should not be on the classpath of the main application. 是的,罐子不应位于主应用程序的类路径上。 If you set a classloader of the main application as parent to your URLClassLoaders, the classes loaded still will have access to the rest of the classpath your application uses. 如果将主应用程序的类加载器设置为URLClassLoaders的父级,则加载的类仍然可以访问应用程序使用的其余类路径。

For static s if they are constants they will probably be inlined by the compiler. 对于static如果它们是常量,则它们可能会被编译器内联。 For static variables you will get 2, 1 for each jar loaded. 对于静态变量,每个加载的jar将得到2、1。 If those variables are used outside of the context of the jars, you will probably have to access them via reflection off their class objects. 如果这些变量在jar的上下文之外使用,则可能必须通过反射其类对象来访问它们。 Wether you need to select one over the other or combine them depends on the application, it would be my guess that if you can load and use 2 versions in 1 application that any static variables would only be used in the context of the respective jars. 您是否需要选择一个或结合使用取决于应用程序,这是我的猜测,如果您可以在1个应用程序中加载和使用2个版本,则任何静态变量都只能在相应jar的上下文中使用。

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

相关问题 如何对不同的类使用相同的变量名 - How to use same variable name for different classes 如何在两个不同的类中使用相同的计数器 - How to use the same counter in two different classes 如何使用不同的参数同时启动同一个 spring-batch 作业? - How to start the same spring-batch job concurrently with different parameters? 如何同时工作具有相同优先级的两个不同线程? - How to work concurrently two different threads which have the same priority? 如何在Java中同时访问同一对象的两种不同方法? - How to access two different methods of the same object concurrently in Java? 如何在两个不同的类中使用相同的方法(带有Scenebuilder的JavaFX)? - How to use same method in two different classes (JavaFX with Scenebuilder)? Java模板,如何使用两个具有相同名称和不同类型的类 - Java Templates, how to use two classes with the same name and different types 如何在仅使用批注实现相同接口的不同类中使用@autowired - How to use @autowired in different classes that implement the same interface with only annotations 如何在同一测试中使用2个不同的参数捕获器类 - How to use 2 different argument captor classes in the same test 如何在两个不同的类中使用相同的add()方法 - How to use the same add() method in two different classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM