Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

The simplest example uses a custom namespace starting with java:// and ending with the Java package name to denote the package/classes to use in the XML. This avoids needing any special mapping configuration and can work with most POJOs today...

...

The Java POJO which this configures is shown below. Notice in the above that the element localName is the same as the class in the package from the namespace. So this mechanism works great when there is a one to one mapping of XML element names to class names in the package defined by the java:// namespace.

...

...

Custom XML mappings

This example shows how to customise the mapping of XML to POJOs using a discovery-properties file. Using the same POJO as above, here's an example of the XML

...

...

Notice that we are using our own custom namespace here and using a custom element name too. To inform the XBean XML parser of your new custom mapping you need to create a properties file on the classpath at META-INF/services/org/apache/xbean/spring/$namespace. The namespace is encoded to take out : and // etc.

So in the above example we need to include this properties file in META-INF/services/org/apache/xbean/spring/http/xbean.apache.org/schemas/pizza. The contents of the file are here

...

Notice that we are renaming both XML element names to classes and XML attribute names to different property names. The renaming of properties can be useful to avoid clashes with standard Spring attributes like "id", "class", "ref" etc.

...

This example shows how to customise the mapping of XML to POJOs which use constructor injection. For this example, we are going to configure the following bean which can only be configured using constructor injection:

...

...

The Xml uses the same clean format as above:

...

...

In this example we use a custom namespace just like we did in the previous example. To enable constructor injection we need to provide the constructor argument names to the XBean Spring parser in the mapping properties file as follows:

...

...

The most important element of this file is the last entry, which tell the parser that the SaladService(java.lang.String,java.lang.String,boolean) constructor parameters are named dressing, size and crouton respectively.

...

Its quite common to want to use nested child elements to map to complex property values. The following example shows this in action...

...

...

Notice that the nested element <dinnerMenu> maps to the collection of pizza beans which maps to a Spring <property name="foo"><list>... construct. This can be achived using the pizza.dinnerMenu.list = realPropertyName entry in the properties file. If there is no entry for the nested property element then introspection is used on the class to determine if its a <property><bean>... or <property><list><bean>... style property.

...