JiBX: How do I keep using interfaces in my code?

和自甴很熟 提交于 2019-12-05 21:45:31

In the mapping, you should be able use the "create-type" attribute to specify the concrete class that JiBX should instantiate for bean properties that have an interface type. I use this a lot for collection properties. For example, you can tell JiBX to instantiate a java.util.HashSet for a property of type java.util.Set. But I believe it works just as well for non-collection properties. Your mapping would look something like:

<mapping class="com.mypackage.AImpl" name="A">
  <structure get-method="getB" set-method="setB" create-type="com.mypackage.BImpl">
    ...
  </structure>
  ...
</mapping>

JiBX will call the no-arg constructor to create the B object. Alternatively, you could use a factory or a custom serializer/deserializer if you need fancy instantiation logic. See this reference page for details.

Another good resource is the binding.dtd - apparently it's not in the distribution but can be downloaded from here: http://jibx.cvs.sourceforge.net/viewvc/checkout/jibx/core/docs/binding.dtd. Put this file somewhere (c:\binding.dtd for example). Then, in the top binding entry, use this:

<binding xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file://jibx/binding.dtd">

and register file://jibx/binding.dtd to point to your saved binding.dtd for documentation and verification goodies.

It's amazing what inertia does - I know that xml files should have schemas / dtds, I've used them before and always said "without a schema understanding this would've been impossible". Yet when I've entered this project, it never occurred to me to search for the schema / dtd for this xml - I just accepted it as given that it had none.
Lesson learned.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!