Community   SpringSource   Projects    Downloads    Documentation    Forums    Training   Exchange   Blogs

Go Back   Spring Community Forums > Core Spring Projects > Core Container

Reply
 
Thread Tools Display Modes
  #1  
Old Mar 27th, 2008, 10:20 PM
spcmdr spcmdr is offline
Member
 
Join Date: Oct 2007
Posts: 81
Default set a date value in a bean property

Hi

How to set a date value on bean ?

<bean id="beanid" class="BeanClass">
<property name="date" value="2008-03-01"/>
</bean>
Reply With Quote
  #2  
Old Mar 28th, 2008, 08:52 AM
wpoitras wpoitras is offline
Senior Member
 
Join Date: Feb 2005
Location: Boston, MA
Posts: 1,140
Default

By default there isn't a date PropertyEditor registered. PropertyEditors are the objects which convert strings into other object types. There are many PropertyEditors registered by default. Things like numbers, Class, etc. So you need to create a CustomDateEditor and then register it with your ApplicationContext using CustomEditorConfigurer. Take a look here for more information.
__________________
Bill
Reply With Quote
  #3  
Old Jul 10th, 2008, 02:37 PM
pbssundar pbssundar is offline
Junior Member
 
Join Date: Oct 2007
Location: Cleveland, OH
Posts: 3
Thumbs up Quick Configuration for registering CustomDateEditor

Just add the below configuration right after your <beans> definition.
This would take care of registering the CustomDateEditor with the SHORTdate format (mm/dd/yy).
PHP Code:
<!-- PATTERN: Register the CustomDateEditor with SHORT date format (3) -->    
    <
bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <
property name="customEditors">
            <
map>
                <
entry key="java.util.Date">                
                    <
bean id="date_editor" class="org.springframework.beans.propertyeditors.CustomDateEditor">
                        <
constructor-arg>
                            <
bean class="java.text.DateFormat" factory-method="getDateInstance">
                                <
constructor-arg value="3"/>
                            </
bean>
                        </
constructor-arg>
                        <
constructor-arg value="true"/>
                    </
bean>
                </
entry>
            </
map>
        </
property>
    </
bean>    
<!--
End of Custom Date Editor Registering! -->

With this in place, you can now configure String-values for Date (java.util.Date) properties in your Bean definition file:
PHP Code:
<bean id="id" class="com.som.Identity">
    <
property name="issueDate" value="1/1/2008" />
</
bean>
The Bean (for your refernence) would look like this:
PHP Code:
Identity.java

public
class Identity {
    
public Identity() { }

    
// Inject
    
java.util.Date issueDate;
    
public void setIssueDate(java.util.Date issueDate) {
        
this.issueDate = issueDate;
    }
    
public java.util.Date getIssueDate() {
        return
issueDate;
    }
}
Let me know if there is any better way to do this.
The concern that I have with this approach is using hardcoded '3' for SHORT in the Bean configuration. This doesn't seem great idea. (What if Sun decides to change this value to something else?).
__________________
Som
----------
Java Admirer!
Reply With Quote
  #4  
Old Oct 24th, 2008, 10:51 AM
spcmdr spcmdr is offline
Member
 
Join Date: Oct 2007
Posts: 81
Default What abount using factory method

PHP Code:
<bean id="id" class="com.som.Identity">
    <
property name="issueDate">
        <
bean class="java.util.GregorianCalendar" factory-method="getTime">
            <
constructor-arg value="2008"/>
            <
constructor-arg value="1"/>
            <
constructor-arg value="1"/>
        </
bean>
    </
property>
</
bean>
Reply With Quote
  #5  
Old Oct 25th, 2008, 03:01 PM
spcmdr spcmdr is offline
Member
 
Join Date: Oct 2007
Posts: 81
Default It does not work

maby factory method should be static
Reply With Quote
  #6  
Old Dec 3rd, 2008, 09:50 AM
ch4mp ch4mp is offline
Member
 
Join Date: Sep 2007
Location: Paris
Posts: 32
Default Inject bean getter return value ?

Hi,

I'm facing the exact same problem.
GregorianCalendar.getTime() is not static, it's a regular getter.

How to reference not GregorianCalendar bean but it's getTime() method return value ?

this doesn't work
PHP Code:
<bean id="initDay" class="java.util.GregorianCalendar">
    <
constructor-arg value="2008" />
    <
constructor-arg value="0" />
    <
constructor-arg value="1" />
</
bean>
<
bean id="id" class="com.som.Identity">
    <
property name="issueDate" ref="initDay.time"/>
</
bean>
Reply With Quote
  #7  
Old Dec 3rd, 2008, 10:28 AM
denis.zhdanov denis.zhdanov is offline
Senior Member
 
Join Date: May 2007
Location: Saint Petersburg, Russian Federation
Posts: 1,189
Default

Reference documentation - 3.2.3.2.3. Instantiation using an instance factory method
Reply With Quote
  #8  
Old Dec 3rd, 2008, 11:23 AM
ch4mp ch4mp is offline
Member
 
Join Date: Sep 2007
Location: Paris
Posts: 32
Thumbs up Thank you

It works !
I had missed this in the doc. I was stupidly associating "factory" word with "static" method (wich is really stupid), so I jumped over this paragraph

final conf:
PHP Code:
<bean id="initDay" class="java.util.GregorianCalendar">
    <
constructor-arg value="2008" />
    <
constructor-arg value="0" />
    <
constructor-arg value="1" />
</
bean>
<
bean id="id" class="com.som.Identity">
    <
property name="issueDate"><bean factory-bean="initDay" factory-method="getTime"/></property>
</
bean>
Thank you Denis
Reply With Quote
  #9  
Old Dec 3rd, 2008, 11:37 AM
denis.zhdanov denis.zhdanov is offline
Senior Member
 
Join Date: May 2007
Location: Saint Petersburg, Russian Federation
Posts: 1,189
Default

Don't blame yourself, everyone makes a mistakes

This forum is intended to be a place where people can help to each other. Believe me, it was not hard for me to point to the necessary documentation location
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 09:36 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.