Wednesday, February 25, 2009

Full Katesplayground Sets

Initializing a property of type Date with Spring or Enum

The Spring framework lets you build objects in the configuration. Objects are created and can be initialized.

This works fine and just for simple types such as String or int when it comes to Enum is simple but you must know when it comes to date is much more difficult.

I propose to briefly explain how these two cases.

The case of Enum in Spring

The example works for version 2.5.6 of Spring with JDK 1.5. I think version 2.0 or 2.5 have made simplifications that I use in this example.

For a simple property of a bean. If this property is of type RoleStatAppel which is an enumeration which takes the values: RoleStatAppel.ECOUTANT or RoleStatAppel.ADMINISTRATEUR. To set the value of this property roleUtilisateur, simply add the definition of the Bean:

\u0026lt;property name="roleUtilisateur" value="ECOUTANT" />

Spring takes care of everything, he knows the type of the property and is able to do correspondence with the value in value.

The same rule applies to an object created with arguments in its constructor:

The initial code is:

new NsGUIFactory (NsGUIFactory.NsImplementationMode.GuiEcho2)

Where

  • NsGUIFactory is a singleton that is desired is a set
  • GuiEcho2 enumeration value that is taken into NsImplementationMode constructor parameter.

solution to create this object using Spring is simply:

\u0026lt;bean id="FactoryBean" class="fr.natsystem.natjet.application.NsGUIFactory">
\u0026lt;constructor-arg> \u0026lt;value> GuiEcho2 \u0026lt;/ value> \u0026lt; / constructor-arg>
\u0026lt;/ bean>

For information, the latest example, in my case it was an enumeration defined in the class NsGUIFactory. The case of

Date in Spring

For dates, the problem is somewhat more complicated:

  • Spring is not able to automatically convert a value 01/02/2009 in a property of type Date
  • there is a format. It is for this reason that Spring is not able to do the conversion alone.
  • it may be interesting to retrieve the current date without having to modify its configuration file.

To solve the problem of values of complex types for which the conversion of a simple String is not immediate or ambiguous, Spring offers what it calls a bean factory post-processor that can complement existing standards for these types.

is this solution we will look to treat the case of dates. The Spring Factory CustomEditorConfigurer will be set thanks to an interface in the Spring configuration file applicationContext.xml.

\u0026lt;bean id="customEditorConfigurer"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
\u0026lt;property name = "customEditors" >
\u0026lt;map>
<entry key="java.util.Date">
            <bean class="org.springframework.beans.propertyeditors.CustomDateEditor">
                <constructor-arg index="0">
                    \u0026lt;bean class="java.text.SimpleDateFormat">
; \u0026lt;constructor-arg> \u0026lt;value> dd / MM / yyyy \u0026lt;/ value> \u0026lt;/ constructor-arg>
\u0026lt;/ Bean>
\u0026lt;/ constructor-arg>
\u0026lt;constructor-arg index="1"> \u0026lt;value> true \u0026lt;/ value> \u0026lt;/ constructor- arg>
\u0026lt;/ Bean>
\u0026lt;/ entry>
\u0026lt; / map>
\u0026lt;/ property>
\u0026lt;/ bean>

The tag map can list, if necessary conversion for several different types. This is a list of Entry.

  • The entry has a key: key which indicates the Java type that will be affected by conversion: in our case is the type java.util.Date. this means that as soon as Spring meeting a property of type Date, it will automatically call our PropertyEditor. The
  • PropertyEditor for this type is defined in the bean tag indicating the class used. Spring provides a class to date. This object takes two input arguments, a SimpleDateFormat that defines the format for the conversion and a second argument that is true. I do not know what that avleur (this may be the property that allows lenient or not to force an error on an invalid date)

We choose the format you wish to use in \u0026lt;constructor-arg> \u0026lt;value> dd / MM / yyyy \u0026lt;/ value> \u0026lt;/ constructor-arg>. I recall that

mm in java refers to minutes while MM refers to month.

Once this statement added in the applicationContext.xml file, simply set the value like this:

\u0026lt;property name="dateArrivee" value="22/02/2009" />

Now if I want automatqiuement day value, it is possible to define dateJour a bean as follows:

\u0026lt;bean id="dateJour" class="java.util.Date" scope="prototype"/>

Then use this object as property value, which gives in our case:

\u0026lt;property name="dateArrivee">
\u0026lt;ref bean="dateJour"
/> \u0026lt;/ property>

0 comments:

Post a Comment