如何使用反射以外的方式填充 JavaBean

发布于 2024-10-29 11:36:51 字数 433 浏览 1 评论 0原文

你知道我是否可以填充 javabean 但我不想使用反射。

例如,我有这个 xml 模板来填充它

示例 XML 文件

<property name = "card" value = "cdd"/>

public class Customer {
    private String card;

     public void setCard(String card) {
          this.card = card;
     }

     public String getCard() {
     }

}

我想在 Java bean 上调用 setCard 但我不想使用反射 因为我以前用过它,而且速度很慢,

有什么替代方案吗?例如,Hibernate 是如何做到的?

谢谢 卡洛

do you know if there is anyway that I can populate a javabean but i don't want to use reflection.

For example I have this xml template to pouplate it

Sample XML File

<property name = "card" value = "cdd"/>

public class Customer {
    private String card;

     public void setCard(String card) {
          this.card = card;
     }

     public String getCard() {
     }

}

I want to call setCard on the Java bean but I don't want to use reflection
since I've used it before and it's quite slow,

Are there any alternatives? How does Hibernate do it for example?

Thanks
Carlo

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

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

发布评论

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

评论(3

森林很绿却致人迷途 2024-11-05 11:36:51

从 XML 填充 JavaBean 的唯一更快的方法(即比使用反射更快)是编写或生成一些绑定代码,这些代码使用从 XML(在本例中是从 XML 属性)提取的值来调用 setter。

  • 手动编写绑定代码是最简单的方法...只要您不需要编写太多内容。

  • 代码可以作为源代码生成并编译。

  • 可以使用诸如BCEL或ASM之类的字节码生成技术来生成代码。

  • 可能有一些现有的 XML 到 JavaBean 绑定生成器,尽管现有的绑定很可能使用反射而不是代码生成。


然而,尚不清楚是否值得费心去避免反思。虽然反射的成本相对较高,但 XML 的成本可能要高得多。我建议您在决定使用更复杂的实现方法之前进行一些分析。

The only faster way (i.e. faster than using reflection) to populate a JavaBean from XML is to either write or generate some binding code that calls the setters with values extracted from the XML (in this case, from the XML attributes).

  • Hand writing the binding code is the simplest approach ... provided you don't have much to write.

  • Code could be generated as source code and compiled.

  • Code could be generated using a bytecode generation technology such as BCEL or ASM.

  • There may some existing XML-to-JavaBean binding generator, though existing bindings may well use reflection rather than code generation.


However, it is not clear this is worth going to the bother of avoiding reflection. While reflection is relatively expensive, XML is probably significantly more expensive. I'd recommend doing some profiling before you decide to use a more complicated implementation approach.

鹿港巷口少年归 2024-11-05 11:36:51

我非常确定 Hibernate 在底层使用了反射 API。 Groovy 还对自动生成和使用 bean getter/setter 提供了一些很好的支持,它们最终也在底层使用了反射。

现在有一个选项,您可以对解析器进行硬编码以读取 xml 并根据给定的 name 属性调用适当的 setter,但是您会遇到解析器变得脆弱的问题(当您的模型发生变化时,如果这有意义的话)。

I'm pretty sure Hibernate uses reflection APIs deep under the hood. Groovy also has some nice support for automatically generating and using bean getters/setters which also ultimately use reflection under the hood as well.

Now there is an option where you could hard code your parser to read the xml and call the appropriate setter given the name attribute, but you run into the problem of your parser becoming brittle (when your model changes if that makes sense).

帅的被狗咬 2024-11-05 11:36:51

如果 Bean 是您的,您可以实现如下所示的接口:

/** Tries to set the property named key with the value given and returns true for success or false otherwise. */
boolean set(String key, Object value);

然后简单地转换为该接口并尝试使用该方法来设置属性。它确实需要在 bean 中进行一些工作 - 但避免反射。

If the Bean is your's you may implement an interface like this:

/** Tries to set the property named key with the value given and returns true for success or false otherwise. */
boolean set(String key, Object value);

Then simply cast to that interface and try to use that method to set the properties. It sure needs some work in the bean - but avoids reflection.

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