如何向 JAXB 生成的类添加自定义行为。自定义不覆盖现有方法

发布于 2024-10-08 16:39:07 字数 625 浏览 1 评论 0原文

我从我无法控制的外部来源获得了一个 xsd。现在我们想要向 JAXB 生成的类添加自定义方法,以便对象能够更多地了解其自身,而不是成为具有 getter 和 setter 的简单 POJO。

这个想法就像一旦我们实现了这种类型的方法,我们就不需要深入研究水合对象,而是通过调用自定义方法向对象询问我们需要的特定信息,并且可以获得我们想要的东西?

当然,还有一种传统的编程方法,我不断深入根对象的子对象的子对象,在每个子对象上调用 getter 方法并实现业务逻辑。但不知怎的,这里的团队不希望这样。

到目前为止我所做的一种方法是暴力方法。就像我使用 JAXB 从 XSD 生成类一样。然后我有一个外部实用程序(现在这是一个独立的java程序),当运行时,它将读取特定目录中的一些文本文件(以我们自己的方式构建,以找出应该进入哪个方法并进入哪个.java文件)它保存自定义方法,并将这些方法移动到适当的 JAXB 生成的类。每次我获得新版本的 XSD 时,我都需要生成类、调用此实用程序,并可能解决一些编译问题等,以防 JAXB 生成的类中存在一些变量更改。该实用程序运行良好。但我想知道这一点。

有没有更好的方法来做到这一点?我查了一下互联网。给出了一些解决方案,但这些只是覆盖 JAXB 生成的对象中现有的 get 方法,而不是添加全新的方法。

请建议。

I got an xsd from an external source over which i have no control. Now we want to add custom methods to the JAXB generated classes so that the object would know more about itself rather than being a simple POJO with getters and setters.

The idea is like once we implement this type of approach we don't need to drill down the hydrated object but ask the object for specific information that we need through calling the custom methods and could get what we want?

Of course there is the traditional programming approach where in i keep on drilling down the children of children children of the root object, calling getter methods on each child and implementing business logic. But somehow the team here doesn't want it that way.

One approach I have done so far is brute force one. Like i generate classes from XSD using JAXB. then i have an external utility (right now this is a stand alone java program) which when run, will read some text files (structured in our own way to find out which method should go and into which .java file) in a specific directory which holds the custom methods, and move those methods to appropriate JAXB generated classes. Every time i get a new version of XSD i need to generate classes, invoke this utility and perhaps resolve some compilation issues etc in case there are some variable changes in JAXB generated classes. The utility is working perfectly fine. But i want to know this.

Is there any better way to do this? I checked the internet. There are some solutions given but those are just overriding the existing get methods in the JAXB generated objects and not adding completely new methods.

Please suggest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖公主 2024-10-15 16:39:08

好吧,“最干净的方法”是使用面向方面的编程(AOP),它不是在源代码级别而是在运行时执行您所描述的操作。它将方法注入到类中。 AOP 的一种可能实现是 AspectJ

如果 XML 模式不太复杂,则可以使用第二种替代方案正在围绕它创建一个包装器。

您已经描述了第三种选择。但我认为你可以依靠标准工具而不是自己编码 - 我建议为此使用标准差异/补丁工具。只要您的代码风格是固定的,从现有的修改后的 java 文件创建差异并将其应用到新生成的 JAXB 代码上就不会太复杂。

Well, the "cleanest approach" would be using Aspect Oriented programming (AOP) which does what you describe not on source code level but at runtime. It injects methods into classes. One possible implementation of AOP is AspectJ

A second alternative that would be possible if the XML shema is not too complex is creating a wrapper around it.

The third alternative you have already described. But I thing you can rely on standard tools instead of coding something yourself - I would suggest to use the standard diff/patch tools for that. As long as your code style is fixed it is not too complicated to create a diff from an existing modified java file and apply it on the freshly generated JAXB code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文