<?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; spring framework</title>
	<atom:link href="http://www.tzavellas.com/techblog/tag/spring-framework/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>Implementing Seam style @Logger injection with Spring</title>
		<link>http://www.tzavellas.com/techblog/2007/03/31/implementing-seam-style-logger-injection-with-spring/</link>
		<comments>http://www.tzavellas.com/techblog/2007/03/31/implementing-seam-style-logger-injection-with-spring/#comments</comments>
		<pubDate>Sat, 31 Mar 2007 13:23:44 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[seam]]></category>
		<category><![CDATA[spring framework]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/2007/03/31/implementing-seam-style-logger-injection-with-spring/</guid>
		<description><![CDATA[Seam provides a number of features to help programmers with the tedious but necessary logging. One of them is the @Logger annotation that is used to inject a Seam Log instance into a Seam component. For example instead of writing: private Log log = LogFactory.getLog(MyClass.class); you can write: @Logger private Log log; and Seam will [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.jboss.com/products/seam">Seam</a> provides a number of <a href="http://docs.jboss.com/seam/1.2.1.GA/reference/en/html/concepts.html#d0e3162">features</a> to help programmers with the tedious but necessary logging. One of them is the <code>@Logger</code> annotation that is used to inject a <a href="http://www.jboss.com/products/seam">Seam</a> Log instance into a <a href="http://www.jboss.com/products/seam">Seam</a> component. For example instead of writing:</p>
<pre>private Log log = LogFactory.getLog(MyClass.class);</pre>
<p>you can write:</p>
<pre>@Logger private Log log;</pre>
<p>and <a href="http://www.jboss.com/products/seam">Seam</a> will inject an appropriate logger. Below we will try to to implement this feature using the <a href="http://springframework.org/">Spring Framework</a> for objects managed by the Spring DI container (actually a  <a href="http://www.springframework.org/docs/api/org/springframework/beans/factory/BeanFactory.html"><code>BeanFactory</code></a>).</p>
<p>The Spring DI container provides a number of extension interfaces that beans can implement to get callbacks from the container in various stages of the container operation. One callback interface is the <a href="http://www.springframework.org/docs/api/org/springframework/beans/factory/config/BeanPostProcessor.html"><code>BeanPostProcessor</code></a>. <a href="http://www.springframework.org/docs/api/org/springframework/beans/factory/config/BeanPostProcessor.html"><code>BeanPostProcessor</code></a>s are called before and after the initialization of each bean and allow the custom modification of bean instances (for example wrapping an instance with a <a href="http://java.sun.com/j2se/1.3/docs/guide/reflection/proxy.html">dynamic proxy</a>).</p>
<p>The only thing we have to do to implement the <code>@Logger</code> injection (besides defining a <code>@Logger</code> annotation) is to write a <a href="http://www.springframework.org/docs/api/org/springframework/beans/factory/config/BeanPostProcessor.html"><code>BeanPostProcessor</code></a> that, before each bean gets initialized (right after it gets constructed), will iterate over the fields of the bean to detect any <code>@Logger</code> annotations and construct and inject a new logger instance.</p>
<p>Let&#8217;s define the annotation:</p>
<pre>
package com.tzavellas.spring.logger;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Retention(RUNTIME)
@Target(FIELD)
@Documented
public @interface Logger { }
</pre>
<p>and now the <code>BeanPostProcessor</code>:</p>
<pre>
package com.tzavellas.spring.logger;

import java.lang.reflect.Field;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.util.ReflectionUtils;

import static org.springframework.util.ReflectionUtils.FieldCallback;

public class LoggerPostProcessor implements BeanPostProcessor {

  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    return bean;
  }

  public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
    ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() {
      public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
        if (field.getAnnotation(Logger.class) != null) {
          Log log = LogFactory.getLog(bean.getClass());
          field.set(bean, log);
        }
      }
    });
    return bean;
  }
}
</pre>
<p>Below I have a small <a href="http://www.junit.org">JUnit</a> test to verify that it works:</p>
<pre>
package com.tzavellas.spring.logger;

import static org.junit.Assert.*;

import org.apache.commons.logging.Log;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;

public class LoggerPostProcessorTest {

  DefaultListableBeanFactory factory = new DefaultListableBeanFactory();

  @Before
  public void setupFactory() {
    RootBeanDefinition bean = new RootBeanDefinition(Bean.class, true);
    factory.addBeanPostProcessor(new LoggerPostProcessor());
    factory.registerBeanDefinition("bean", bean);
  }

  @Test
  public void injectLogger() {
    Bean b = (Bean) factory.getBean("bean");
    assertNotNull(b.log);
    b.doSomething();
  }
}

class Bean {

  @Logger Log log;

  public void doSomething() {
    log.debug("message");
  }
}
</pre>
<p>This is a very simple implementation since the goal of this article is to demonstrate the extensibility of the Spring DI container and not to implement a complete solution. One limitation is that this implementation only injects loggers (actually commons-logging Logs) to public fields (Seam can inject Seam Logger to private fields).</p>
<p>To use the above in a Spring XML file you simply define a bean with class <code>com.tzavellas.spring.logging.LoggerPostProcessor</code> (the id/name is not needed). The <code>BeanFactory</code>/<code>ApplicationContext</code> will automatically detect all beans that implement the <code>BeanPostProcessor</code> interface at startup time and will initialize them and then call them every time a bean gets initialized.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2007/03/31/implementing-seam-style-logger-injection-with-spring/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>New features in Spring IDE 2.0M3</title>
		<link>http://www.tzavellas.com/techblog/2007/03/15/new-features-in-spring-ide-20m3/</link>
		<comments>http://www.tzavellas.com/techblog/2007/03/15/new-features-in-spring-ide-20m3/#comments</comments>
		<pubDate>Thu, 15 Mar 2007 14:12:21 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[spring framework]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/2007/03/15/new-features-in-spring-ide-20m3/</guid>
		<description><![CDATA[Spring IDE 2.0M3 was released and the springframework.org site writes about the long-awaited Spring Webflow support. Spring IDE now includes graphical and XML editors for Spring WebFlow. I believe that this major new feature hides 3 very important new features that IMHO programmers will find more useful, since people working with Spring spend more time [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.springide.org/project/wiki/SpringIde2Milestone3">Spring IDE 2.0M3</a> was released and the <a href="http://www.springframework.org">springframework.org</a> site writes about the long-awaited <a href="http://www.springframework.org/node/429">Spring Webflow support</a>. <a href="http://www.springide.org">Spring IDE</a> now includes graphical and XML editors for <a href="http://www.springframework.org/webflow">Spring WebFlow</a>.</p>
<p>I believe that this major new feature hides 3 very important new features that IMHO programmers will find more useful, since people working with <a href="http://www.springframework.org">Spring</a> spend more time editing Spring XML files than WebFlow XML files.</p>
<h3>New Spring 2.0M3 features for Spring XML</h3>
<ul>
<li>Spring IDE now integrates with class reference search (Shift+Ctrl+G). This means that the results of class reference search now also include Spring beans.</li>
<li>The XML bean editor now supports renaming of bean ids (Refactor -> Rename bean element, or Alt+Shift+R)</li>
<li>Spring IDE now participates in class refactorings (class rename, class move and property rename).</li>
</ul>
<p>See the <a href="http://www.springide.org/project/wiki/SpringIde2Milestone3">changelog</a> of the 2.0M3 release for more information about changes and new features.</p>
<p>I am using the Spring IDE 2.0 milestones from the development update site and I am very satisfied with all the new features. The <a href="http://springide.org/project/wiki/SpringideTeam">Spring IDE team</a> is doing a great job (thanks guys) and I believe that the 2.0 release is going to be a huge step forward.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2007/03/15/new-features-in-spring-ide-20m3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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>Testing Spring AOP code</title>
		<link>http://www.tzavellas.com/techblog/2006/02/23/testing-spring-aop-code/</link>
		<comments>http://www.tzavellas.com/techblog/2006/02/23/testing-spring-aop-code/#comments</comments>
		<pubDate>Thu, 23 Feb 2006 05:45:26 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[spring framework]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/?p=50</guid>
		<description><![CDATA[In Using the Spring AOP Framework with EJB Components, Eugene Kuleshov presents a really nice way to test Spring AOP configuration code and advices. When you use Spring AOP use can easily test your target objects with unit tests, but how do you test the weaved code? In the above article Eugene Kuleshov uses unit [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://dev2dev.bea.com/pub/a/2005/12/spring-aop-with-ejb.html">Using the Spring AOP Framework with EJB Components</a>, <a href="http://jroller.com/page/eu">Eugene Kuleshov</a> presents a really nice way to test Spring AOP configuration code and advices. When you use Spring AOP use can easily test your target objects with unit tests, but how do you test the weaved code?</p>
<p>In the above article <a href="http://jroller.com/page/eu">Eugene Kuleshov</a> uses unit tests that reference the Spring <a href="http://static.springframework.org/spring/docs/1.2.x/api/org/springframework/context/ApplicationContext.html">ApplicationContext</a> that has the AOP configuration code. Then at the setup method of the TestCase class he registers a custom <a href="http://static.springframework.org/spring/docs/1.2.x/api/org/springframework/beans/factory/config/BeanPostProcessor.html">BeanPostProcessor</a> that provides mock implementations for the target objects. At the test methods he trains the mock objects and tests the all the AOP code except the target objects.</p>
<p>I really liked the trick with the custom <a href="http://static.springframework.org/spring/docs/1.2.x/api/org/springframework/beans/factory/config/BeanPostProcessor.html">BeanPostProcessor</a> to provide the mock objects. I&#8217;ll probably start using this trick in my tests really soon.</p>
<p>BTW <a href="http://dev2dev.bea.com/pub/a/2005/12/spring-aop-with-ejb.html">Using the Spring AOP Framework with EJB Components</a> also provides lots of information on how to effectively use <a href="http://springframework.org/">Spring</a> in a EJB 2.x environment. If you are maintaining EJB 2.x applications and you want to make your applications more testable over time you should probably read this article.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2006/02/23/testing-spring-aop-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SpringIDE</title>
		<link>http://www.tzavellas.com/techblog/2005/12/02/springide/</link>
		<comments>http://www.tzavellas.com/techblog/2005/12/02/springide/#comments</comments>
		<pubDate>Fri, 02 Dec 2005 01:30:50 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[spring framework]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/?p=35</guid>
		<description><![CDATA[If you are developing with the Spring Framework and you use the Eclipse IDE, then you should definitely check out the SpringIDE Eclipse plug-in. This plug-in is a Spring Framework subproject that aims to assist developers with the development of Spring applications. The main features of SpringIDE are: Spring bean definition editor (with auto-complete support) [...]]]></description>
			<content:encoded><![CDATA[<p>If you are developing with the <a href="http://www.springframework.org/">Spring Framework</a> and you use the <a href="http://www.eclipse.org/">Eclipse</a> IDE, then you should definitely check out the <a href="http://www.springide.org/">SpringIDE</a> <a href="http://www.eclipse.org/">Eclipse</a> plug-in. This plug-in is a Spring Framework <a href="http://www.springframework.org/subprojects/">subproject</a> that aims to assist developers with the development of <a href="http://www.springframework.org/">Spring</a> applications.</p>
<p>The main features of <a href="http://www.springide.org">SpringIDE</a> are:</p>
<ul>
<li>Spring bean definition editor (with auto-complete support)</li>
<li>Graphical view of Spring beans</li>
<li>Spring beans Eclipse view</li>
<li>Graphical editor for Spring Web-Flow (under development)</li>
</ul>
<p>See <a href="http://www.tzavellas.com/techblog/techblog-files/springide2.png">here</a> for a screenshot of the bean definition editor while I am configuring a <a href="http://www.springframework.org/docs/api/org/springframework/web/servlet/DispatcherServlet.html">DispatcherServlet</a> for a new project.</p>
<p>For more information visit the Spring IDE <a href="http://www.springide.org/">homepage</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2005/12/02/springide/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

