带有 addXX 方法的 BeanUtils

发布于 2024-08-02 08:12:12 字数 318 浏览 2 评论 0原文

我使用 Apache BeanUtils setProperty 方法以通用方式从 Excel 文件导入数据。我向解析器提供“列名称”和“属性名称”对的列表,然后解析器使用 setProperty( ..., "属性名称", ...) 为与“列名称”匹配的 Excel 字段设置属性”。它工作得很好,但现在我遇到了问题,我有一个使用 addXXX() 方法设置的属性,但我无法更改该类。 如何在不更改此类或解析器的情况下设置此属性? 是否可以? 我可以用 DynaClass、PropertyUtils 或 MethodUtils 制作类似包装器的东西,以便在设置该属性时调用 addXX 方法吗?

谢谢!

I'm using the Apache BeanUtils setProperty method to import data from an Excel file in a generic manner. I provide the parser a list of "colum name" and "property name" pairs and then the parser sets the properties with setProperty( ..., "property name", ...) for the Excel fields that match with "column name". It's working nice but now I'm having a problem, I have a property that is setted with an addXXX() method and I cannot change that class.
How can I set this property without changing this class or the parser?
Is it possible?
Can I make something like a wrapper with DynaClass, PropertyUtils or MethodUtils so when setting that property it calls the addXX method?

Thanks!

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

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

发布评论

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

评论(1

独孤求败 2024-08-09 08:12:12

您可以编写一个装饰器 bean 类来包装您的类,并提供一个 setXXX() 方法,委托给包装 bean 的 addXXX() 。您甚至可以借助 cglib 或任何其他字节码检测库通过动态代理来完成此操作。

如果您想采用 DynaClass 方式,请查看 WrapDynaClass 作为起点。您必须扩展它才能为您的方法提供自定义 PropertyDescriptor。不过,我不会这样做,有两个原因 - 首先,addXXX() 语义可能与 setXXX() 有很大不同;其次,整个 DynaClass/DynaBean 内部 API 有点混乱,而编写自己的装饰器类则非常简单。

You can write a decorator bean class that will wrap around your class and provide a setXXX() method delegating to addXXX() of the wrapped bean. You can even do that via dynamic proxy with help of cglib or any other bytecode instrumentation library.

If you'd rather go the DynaClass way, take a look at WrapDynaClass as a starting point. You'll have to extend it to provide custom PropertyDescriptor for your method. I wouldn't do this, though, for two reasons - first, addXXX() semantics is likely quite different from setXXX(); and secondly, the whole DynaClass/DynaBean internal API is a bit messy whereas writing your own decorator class is very straightforward.

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