Results 1 to 10 of 10

Thread: How Can I wire a date in the application context?

  1. #1
    Join Date
    Jul 2006
    Posts
    24

    Default How Can I wire a date in the application context?

    No this is not the same post from over a year ago:

    http://forum.springframework.org/showthread.php?t=13440

    I'm having problems wiring up a date in my appContext. I've followed the same intructions from the post above, the step by step from the book "Professional Java Development with the Spring Framework," and even the online documentation.

    But I can't get rid of the cursed:

    org.springframework.beans.TypeMismatchException:
    Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'createdOn'

    I'm at my wits end and maybe just need a sanity check. Can someone please help?

    Here is my appContext:
    Code:
    <bean name="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    		<property name="customEditors">
    			<map>
    				<entry key="java.util.Date">
    					<bean class="pe.DatePropertyEditor">
    						<constructor-arg index="0">
    							<bean class="java.text.SimpleDateFormat">
    								<constructor-arg><value>MM/dd/yyyy</value></constructor-arg>
    							</bean>
    						</constructor-arg>
    						<constructor-arg index="1">
    							<value>true</value>
    						</constructor-arg>
    					</bean>
    				</entry>
    			</map>
    		</property>
    	</bean>
    And my bean definition (located in another file):

    Code:
    	<bean id="ticket1" class="domain.Ticket">
    		//...
    
    		<property name="createdOn">
    			<value>01/11/12</value>
    		</property>
    		
    		//...
    	</bean>
    I've replaced the spring CustomDateEditor with my own version that is a duplicate with the exception of added logging to see when my date editor is called:

    Code:
    	public DatePropertyEditor(DateFormat dateFormat, boolean allowEmpty) {
    		System.out.println("Creating date property editor with format: " + dateFormat.toString());
    		this.dateFormat = dateFormat;
    		this.allowEmpty = allowEmpty;
    	}
    	
    	public void setAsText(String text) throws IllegalArgumentException {
    		System.out.println("Attempting to convert: " + text);
    		if(this.allowEmpty && !StringUtils.isNotBlank(text)) {
    			setValue(null);
    		} else {
    			try {
    				setValue(this.dateFormat.parse(text));
    				
    			} catch(ParseException ex) {
    				throw new IllegalArgumentException("Could not parse date: " + ex.getMessage());
    			}
    		}		
    	}
    When I run it, I never see the console output. ...Only the cursed, "Failed to convert property..." That seems very strange.

    Any ideas? I'm crying here.

    Thanks!

    -Mike

  2. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Do you use a BeanFactory or an ApplicationContext? In the former case your configurer bean (which actually is a post processor) will not be initialized automatically.
    Besides that: Is there a reason not to use the existing CustomDateEditor?

    Regards,
    Andreas

  3. #3
    Join Date
    Jul 2006
    Posts
    24

    Default

    Thanks for your response, Andreas.

    I'm using an ApplicationContext in a servlet environment. I am using Spring Web MVC. But the beans with dates are not being injected into anything to do with the web layer.

    I'm only using a custom class now to perform some debug logging. I had originally used the spring provided class. Its almost an exact copy except that I added System.out.println() calls to each method of the dateEditor to see when that method was called. I'm not yet familiar enough with spring to do it another way.

    I don't know if this matters (I thought that it doesn't), but I have the appContext.xml split into multiple files and the order of the beans doesn't necessarily mean that my customEditorConfigurer is loaded before the beans containing the date. Like you said, its a post processor so I would assume the order doesn't matter.

    I just can't figure out why my logging statements are never called. It makes me think I'm not setting something up correctly, but my customEditorConfigurer bean XML is exactly the same as two other example sources.

  4. #4
    Join Date
    Oct 2004
    Location
    Fareham, England
    Posts
    313

    Default

    Hi digerata

    BFPP are per-container only. In a Spring WMVC setup one has at least two containers. Is the CustomEditorConfigurer (CEC) defined in the correct container (i.e. the web or the business one as appropriate)?

    Cheers
    Rick

  5. #5
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Quote Originally Posted by digerata
    I don't know if this matters (I thought that it doesn't), but I have the appContext.xml split into multiple files
    You mean the context loaded by the ContextLoaderListener/ContextLoaderServlet consists of several files, and one of them defines your configurer? I think that this should work as all files will be merged into one ApplicationContext.
    However, for testing you could place the configurer in the same file as the offending bean.

    In any case I would suggest to turn on DEBUG-level logging and see what Spring does on startup. Perhaps this provides some useful information.

    regards,
    Andreas

  6. #6
    Join Date
    Jul 2006
    Posts
    24

    Default

    Rick,

    I assume you are referring to the [DispatcherServlerName]-servlet.xml and the springApplicationContext.xml when you speak of two containers? If that's the case, then the CEC is defined in the springAppContext.xml file and not the dispatcher-servlet.xml file.

  7. #7
    Join Date
    Jul 2006
    Posts
    24

    Default

    Andreas,

    Just to be on the safe side, I put everything back into the appContext.xml to make sure and I still encountered the same error.

    I'm working on turning up the logging to DEBUG to see what spring is saying, but am having trouble getting spring to pick up the setting. Will post more when I figure that out.

    Thanks for the replies!

  8. #8
    Join Date
    Jul 2006
    Posts
    24

    Default Spring Log

    I was finally able to turn up logging to debug. I've attached the output pruned for filesize.

    What I find strange about the output is that Spring is recognizing one of the two property editors I setup. I have a custom property editor to handle a categoryID to category object conversion. This is being picked up fine and I can see log statements about it. But the second property editor for my dates is never mentioned in the log.

    ?!?!

    I'm including the full snippet of my customEditorConfigurer:

    Code:
    <bean name="categoryEditor" class="pe.CategoryPropertyEditor">
    	<property name="ticketService">
    		<ref bean="ticketService"/>
    	</property>
    </bean>
    
    <bean name="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    	<property name="customEditors">
    		<map>
    			<entry key="domain.Category">
    				<ref bean="categoryEditor"/>
    			</entry>
    			<entry key="java.util.Date">
    				<bean class="pe.DatePropertyEditor">
    					<constructor-arg index="0">
    						<bean class="java.text.SimpleDateFormat">
    							<constructor-arg><value>MM/dd/yyyy</value></constructor-arg>
    						</bean>
    					</constructor-arg>
    					<constructor-arg index="1">
    						<value>true</value>
    					</constructor-arg>
    				</bean>
    			</entry>
    		</map>
    	</property>
    </bean>
    I started to think it was because my nested bean definitions did not have a name attribute. But none of the examples show that and I even re-ran the code with a name attribute added. That didn't change anything.

    This seems like it would be such a simple thing but I can't figure it out. (Just to make sure its not something stupid, I'm using the date pattern: MM/dd/yyyy and the date: 01/11/2006) Any ideas?

    Thanks!!!!

    -Mike
    Attached Files Attached Files
    Last edited by digerata; Aug 2nd, 2006 at 12:42 PM.

  9. #9
    Join Date
    Aug 2006
    Posts
    3

    Default Another possibility

    I realize this is not the answer you are looking for, but this does work.


    *****************************

    <bean id="DateRangeApplicable" class="com.blahblah.app.DateRangeApplicable" singleton="true">
    <property name="effectiveDate">
    <value>6/1/2006 12:00:00</value>
    </property>
    </bean>

    *****************************


    public abstract class DateRangeApplicable {

    private String effectiveDateString;
    private Date effectiveDate;

    public String getEffectiveDateString() {
    return effectiveDateString;
    }

    public void setEffectiveDate(String effectiveDateString) throws
    ParseException{
    SimpleDateFormat dFormatter = new
    SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
    effectiveDate = dFormatter.parse(effectiveDateString);
    }

    public Date getEffectiveDate(){
    return effectiveDate;
    }

  10. #10
    Join Date
    Jul 2006
    Posts
    24

    Default

    Thanks for the follow up j.

    That definitely works but forces us to change the way we handle dates everywhere. In the end, we just dropped the use of all date fields in our configuration.

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •