<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>spiros.blog() &#187; dependency injection</title>
	<atom:link href="http://www.tzavellas.com/techblog/tag/dependency-injection/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tzavellas.com/techblog</link>
	<description>Spiros Tzavellas’s blog, mostly on software development and Java.</description>
	<lastBuildDate>Fri, 24 Sep 2010 07:42:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Using java.util.Date values in Spring bean definitions</title>
		<link>http://www.tzavellas.com/techblog/2006/06/11/using-javautildate-values-in-spring-bean-definitions/</link>
		<comments>http://www.tzavellas.com/techblog/2006/06/11/using-javautildate-values-in-spring-bean-definitions/#comments</comments>
		<pubDate>Sun, 11 Jun 2006 20:31:26 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[spring framework]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/?p=52</guid>
		<description><![CDATA[In a Spring bean definition you can set value properties to a bean by using the value attribute in the property element. For example below I set the value of the firstName property of a bean with id spiros of type com.tzavelas.beans.Person to Spiros. &#60;bean id="spiros" class="com.tzavellas.beans.Person"&#62; &#60;property name="firstName" value="Spiros"/&#62; &#60;/bean&#62; In Spring XML, by [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://springframework.org/">Spring</a> bean definition you can set value properties to a bean by using the value attribute in the property element. For example below I set the value of the <code>firstName</code> property of a bean with id <code>spiros</code> of type <code>com.tzavelas.beans.Person</code> to <code>Spiros</code>.</p>
<pre>
&lt;bean id="spiros" class="com.tzavellas.beans.Person"&gt;
  &lt;property name="firstName" value="Spiros"/&gt;
&lt;/bean&gt;
</pre>
<p>In <a href="http://springframework.org/">Spring</a> XML, by default, you can only set string and number values in bean properties. Now, what happens when the Javabean, you want to configure, has a value property that is of type <code>java.util.Date</code>?</p>
<p><a href="http://springframework.org/">Spring</a> provides an extensible way to set any arbitrary object value to a bean property. This is done with a Javabean standard mechanism called PropertyEditors. PropertyEditors are objects whose purpose is to transform an object&#8217;s value to a string and vice versa. These objects implement the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/beans/PropertyEditor.html"><code>java.beans.PropertyEditor</code></a> interface.</p>
<p>Spring has out of the box implemented some very useful PropertyEditors for common classes, like <code>java.util.Date</code>, <code>java.io.File</code> and others, that can be found in <a href="http://www.springframework.org/docs/api/org/springframework/beans/propertyeditors/package-summary.html"><code>org.springframework.beans.propertyeditors</code></a> package. To use the above editors in your spring configuration you have to first register them with a <a href="http://www.springframework.org/docs/api/org/springframework/beans/factory/config/CustomEditorConfigurer.html"><code>CustomEditorConfigurer</code></a>. <a href="http://www.springframework.org/docs/api/org/springframework/beans/factory/config/CustomEditorConfigurer.html"><code>CustomEditorConfigurer</code></a> is a <a href="http://www.springframework.org/docs/api/org/springframework/beans/factory/config/BeanFactoryPostProcessor.html"><code>BeanFactoryPostProcessor</code></a> and runs after the <a href="http://www.springframework.org/docs/api/org/springframework/beans/factory/BeanFactory.html"><code>BeanFactory</code></a> is initialized.</p>
<p>In the below code I have an example where I register a <a href="http://www.springframework.org/docs/api/org/springframework/beans/propertyeditors/CustomDateEditor.html"><code>CustomDateEditor</code></a> that binds <code>java.util.Date</code> values to bean properties. Then I have a bean definition where I set a bean property to a date value.</p>
<p>The Java code for the example Javabean:</p>
<pre>
package com.tzavellas.beans;

import java.util.Date;

public class Person {

  private Date birthDate;
  private String firstName;

  public Date getBirthDate() {
    return birthDate;
  }
  public void setBirthDate(Date birthDate) {
    this.birthDate = birthDate;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
}
</pre>
<p>The XML configuration:</p>
<pre>
&lt;bean id="customEditorConfigurer"
      class="org.springframework.beans.factory.config.CustomEditorConfigurer"&gt;
  &lt;property name="customEditors"&gt;
    &lt;map&gt;
      &lt;entry key="java.util.Date"&gt;
        &lt;bean class="org.springframework.beans.propertyeditors.CustomDateEditor"&gt;
          &lt;constructor-arg index="0"&gt;
            &lt;bean class="java.text.SimpleDateFormat"&gt;
              &lt;constructor-arg value="dd/MM/yyyy"/&gt;
            &lt;/bean&gt;
          &lt;/constructor-arg&gt;
          &lt;constructor-arg index="1" value="false"/&gt;
        &lt;/bean&gt;
      &lt;/entry&gt;
    &lt;/map&gt;
  &lt;/property&gt;
&lt;/bean&gt;

&lt;bean id="spiros" class="com.tzavellas.beans.Person"&gt;
  &lt;property name="firstName" value="Spiros"/&gt;
  &lt;property name="birthDate" value="15/04/1980"/&gt;
&lt;/bean&gt;
</pre>
<p>As you can see I am passing a <a href="http://java.sun.com/j2se/1.5/docs/api/java/text/SimpleDateFormat.html"><code>java.text.SimpleDateFormat</code></a> to the constructor of the <a href="http://www.springframework.org/docs/api/org/springframework/beans/propertyeditors/CustomDateEditor.html"><code>CustomDateEditor</code></a> and later in the definition of the bean with id <code>spiros</code> I am setting a date property using the format I specified in the <a href="http://www.springframework.org/docs/api/org/springframework/beans/propertyeditors/CustomDateEditor.html"><code>CustomDateEditor</code></a>.</p>
<p><strong>UPDATE:</strong> For the above code to work you have to use an <a href="http://www.springframework.org/docs/api/org/springframework/context/ApplicationContext.html"><code>ApplicationContext</code></a>. A <a href="http://www.springframework.org/docs/api/org/springframework/beans/factory/BeanFactory.html"><code>BeanFactory</code></a> (like <a href="http://www.springframework.org/docs/api/org/springframework/beans/factory/xml/XmlBeanFactory.html"><code>XmlBeanFactory</code></a>) does not work here because BeanFactories do not auto-detect <a href="http://www.springframework.org/docs/api/org/springframework/beans/factory/config/BeanFactoryPostProcessor.html"><code>BeanFactoryPostProcessors</code></a> like the <a href="http://www.springframework.org/docs/api/org/springframework/beans/factory/config/CustomEditorConfigurer.html"><code>CustomEditorConfigurer</code></a> used in the above code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2006/06/11/using-javautildate-values-in-spring-bean-definitions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Spring Framework&#8217;s FactoryBean</title>
		<link>http://www.tzavellas.com/techblog/2005/07/03/explaining-spring-frameworks-factorybean/</link>
		<comments>http://www.tzavellas.com/techblog/2005/07/03/explaining-spring-frameworks-factorybean/#comments</comments>
		<pubDate>Sun, 03 Jul 2005 04:04:53 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[spring framework]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/?p=16</guid>
		<description><![CDATA[Alef Arendsen in this post explains the various ways you can create objects using the Spring &#8216;s ApplicationContext. He also explains in detail the FactoryBean interface and how it can be used to implement factories that can be embedded in the ApplicationContext XML configuration.]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.arendsen.net/">Alef Arendsen</a> in <a href="http://blog.arendsen.net/?p=35">this</a> post explains the various ways you can create objects using the <a href="http://www.springframework.org">Spring</a> &#8216;s <a href="http://static.springframework.org/docs/api/org/springframework/context/ApplicationContext.html">ApplicationContext</a>. He also explains in detail the <a href="http://static.springframework.org/docs/api/org/springframework/beans/factory/FactoryBean.html">FactoryBean</a> interface and how it can be used to implement factories that can be embedded in the <a href="http://static.springframework.org/docs/api/org/springframework/context/ApplicationContext.html">ApplicationContext</a> XML configuration.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2005/07/03/explaining-spring-frameworks-factorybean/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

