<?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>Basil Vandegriend: Professional Software Development &#187; Java</title>
	<atom:link href="http://www.basilv.com/psd/blog/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.basilv.com/psd</link>
	<description></description>
	<lastBuildDate>Thu, 17 May 2012 14:31:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Visualizing Java Package Dependencies</title>
		<link>http://www.basilv.com/psd/blog/2012/visualizing-java-package-dependencies</link>
		<comments>http://www.basilv.com/psd/blog/2012/visualizing-java-package-dependencies#comments</comments>
		<pubDate>Wed, 07 Mar 2012 13:16:42 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[architecture]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/?p=781</guid>
		<description><![CDATA[I have recently been examining the overall package structure of a Java enterprise application. I discovered an easy way to visualize the dependencies between packages using two open source tools JDepend and Graphviz and a little glue code. JDepend analyzes compiled Java bytecode and determines dependencies and metrics between Java packages. These analysis results can [...]]]></description>
			<content:encoded><![CDATA[<p>I have recently been examining the overall package structure of a Java enterprise application. I discovered an easy way to visualize the dependencies between packages using two open source tools <a href="http://clarkware.com/software/JDepend.html">JDepend</a> and <a href="http://www.graphviz.org/">Graphviz</a> and a little glue code. JDepend analyzes compiled Java bytecode and determines dependencies and metrics between Java packages. These analysis results can be used to generate a graph with packages as nodes and dependencies as edges. Graphviz can then be used to layout and draw the graph as an image. Sample code is provided at the end of this article to show how to do this.</p>
<p>Visualizing package dependencies can be useful for a number of reasons:</p>
<ul>
<li>Reverse engineering the architecture of an application when documentation is limited, out-of-date, or non-existent.</li>
<li>Automatically generating diagrams, saving the time required to draw them manually.</li>
<li>To understand impacts when doing large-scale refactorings or changing implementations of components.</li>
<li>To determine how to extract functionality for reuse.</li>
<li>To support good architecture / design as espoused by Robert C. Martin in his book <a href="http://www.amazon.ca/gp/product/0135974445/ref=as_li_tf_tl?ie=UTF8&#038;tag=basilvandegri-20&#038;linkCode=as2&#038;camp=15121&#038;creative=330641&#038;creativeASIN=0135974445">Agile Software Development, Principles, Patterns, and Practices</a><img src="http://www.assoc-amazon.ca/e/ir?t=basilvandegri-20&#038;l=as2&#038;o=15&#038;a=0135974445" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />, such as by eliminating cyclical dependencies or unwanted dependencies.</li>
</ul>
<p>Here is an example package dependency diagram generated by Graphviz:<br />
<a href="http://www.basilv.com/psd/wp-content/uploads/2012/03/packages.png"><img src="http://www.basilv.com/psd/wp-content/uploads/2012/03/packages.png" alt="Example Java Package Dependencies Diagram" title="Example Java Package Dependencies Diagram" width="507" height="251" class="alignnone size-full wp-image-782" /></a></p>
<p>The source code provided below that was used to generate this example diagram is available from my <a href="https://github.com/basilv/Java-Examples">Java Examples</a> project on GitHub or as a <a href="http://www.basilv.com/psd/software#JavaExamples">packaged bundle on my Software page</a>. The code can easily be reused: change the directory of compiled code scanned by JDepend ("dist/classes"), and change the root package used to limit the scope of the diagram ("com.basilv.examples.packagediagram").</p>
<pre class="prettyprint">
package com.basilv.examples.packagediagram;
import java.io.*;
import java.util.*;
import jdepend.framework.*;

public class PackageDiagramCreatorApp {

  public static void main(String[] args) {
    createPackageDependencyDiagram();
    System.exit(0);
  }

  public static void createPackageDependencyDiagram() {
    Collection<JavaPackage> packages = analyzePackages();
    StringBuilder builder = generateGraph(packages);
    generateImage("packages", builder.toString());
  }

  @SuppressWarnings("unchecked")
  private static Collection<JavaPackage> analyzePackages() {
    JDepend jdepend = new JDepend();
    try {
      jdepend.addDirectory("dist/classes");
    } catch (IOException e) {
      throw new RuntimeException("Error adding directory for JDepend to analyze.", e);
    }
    Collection<JavaPackage> packages = jdepend.analyze();
    return packages;
  }

  private static StringBuilder generateGraph(
    Collection<JavaPackage> packages) {
    StringBuilder builder = new StringBuilder();
    builder.append("digraph packages {").append("\n");
    builder.append("node [shape=box];").append("\n");
    builder.append("rankdir=BT;").append("\n");
    Set<String> drawnDependencies = new HashSet<String>();
    for (JavaPackage javaPackage : packages) {
      String packageNodeName = getGraphVizNodeForPackage(javaPackage);
      if (packageNodeName == null) {
        continue;
      }
      builder.append(packageNodeName).append("\n");

      @SuppressWarnings("unchecked")
      Collection<JavaPackage> dependencies = javaPackage.getEfferents();

      for (JavaPackage dependency : dependencies) {
        String dependencyNodeName = getGraphVizNodeForPackage(dependency);
        if (dependencyNodeName == null
          || packageNodeName.equals(dependencyNodeName)) {
          continue;
        }
        String dependencyKey = packageNodeName + "->"
          + dependencyNodeName;
        if (drawnDependencies.contains(dependencyKey)) {
          continue;
        }
        builder.append(packageNodeName).append(" -> ").append(
          dependencyNodeName).append(" [weight=4]").append("\n");
        drawnDependencies.add(dependencyKey);
      }
    }
    builder.append("}\n");
    return builder;
  }

  private static String getGraphVizNodeForPackage(
    JavaPackage javaPackage) {

    String rootPackage = "com.basilv.examples.packagediagram";
    String packageName = javaPackage.getName();
    if (!packageName.startsWith(rootPackage)) {
      return null;
    }

    return packageName.replace(".", "_");
  }

  private static void generateImage(String fileName,
    String graphVizDotFormattedGraph) {
    try {
      File graphFile = createFileWithContents(fileName
        + ".txt", graphVizDotFormattedGraph);

      // This requires the Graphviz software to be installed -
      // see http://graphviz.org/
      String imageFileLocation = fileName + ".png";
      Runtime.getRuntime().exec(
        "dot -v -Tpng " + graphFile.getName() + " -o "
          + imageFileLocation);

      System.out.println("Image file available at "
        + new File(imageFileLocation).getAbsolutePath());
    } catch (IOException e) {
      throw new RuntimeException("Error generating image " + fileName, e);
    }
  }

  private static File createFileWithContents(
    String fileName, String graphVizDotFormattedGraph)
    throws IOException {
    File graphFile = new File(fileName);
    FileWriter writer = new FileWriter(graphFile, false);
    try {
      writer.append(graphVizDotFormattedGraph);
    } finally {
      writer.close();
    }
    return graphFile;
  }

}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2012/visualizing-java-package-dependencies/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Avoiding Caching To Improve Hibernate Performance</title>
		<link>http://www.basilv.com/psd/blog/2010/avoiding-caching-to-improve-hibernate-performance</link>
		<comments>http://www.basilv.com/psd/blog/2010/avoiding-caching-to-improve-hibernate-performance#comments</comments>
		<pubDate>Mon, 08 Feb 2010 14:18:54 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[tools]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[software development]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/?p=486</guid>
		<description><![CDATA[I was recently doing some performance tuning and made the surprising discovery that doing less caching in Hibernate actually improved performance in a particular scenario. When I discovered the problem this seemed very counter-intuitive. In fact, my original design maximized the use of caching in order to improve performance, but the opposite happened in practice. [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently doing some performance tuning and made the surprising discovery that doing less caching in <a href="http://www.hibernate.org/">Hibernate</a> actually improved performance in a particular scenario. When I discovered the problem this seemed very counter-intuitive. In fact, my original design maximized the use of caching in order to improve performance, but the opposite happened in practice. In hindsight, naturally, the reason for this was fairly obvious. So I thought I would share the details of this situation so help you avoid making the same mistake.</p>
<p>I was tuning a batch processing application that received XML input data sets, each consisting of thousands of separate input records. The processing logic converted each input record into multiple Hibernate entities – as many as several hundred. This logic required a number of queries to implement - some to load related, preexisting entities, and others to verify consistency with existing data. This queried data would often be needed for multiple input records in the same data set. Based on this, I decided to use a single Hibernate session to process the entire data set, committing after each input record but keeping the session open to be able to make use of cached entities for subsequent processing.</p>
<p>When initial performance tests were carried out, they showed a disturbing trend: the processing time required per input record in the data set increased linearly. This meant that the total time required to process a data set increased exponentially with the size of the set! This is illustrated by the diagrams below.<br />
<img src="http://www.basilv.com/psd/wp-content/uploads/2010/02/RecordProcessingPerformance.png" alt="Record Processing Performance" title="Record Processing Performance" width="559" height="670" class="size-full wp-image-487" /></p>
<p>An analysis of where the time was being spent showed that the majority of the processing logic required only constant time per record. Where was the extra time going? The culprit seemed to be the call to commit the transaction to the database. I knew that even a few hundred database insert/update statements would execute quickly in nearly constant time (databases are built to scale, after all). The actual database commit was equally speedy. Normally by default I assume that network calls will be the source of performance delays. But in this case this assumption appeared to be incorrect.</p>
<p>So what exactly was happening when I committed the Hibernate transaction, before the calls to the database? Hibernate's first step is to perform a flush to write all entities with changes (called dirty entities) to the database via insert/update/delete calls. How exactly does Hibernate determine which entities are dirty? For loaded entities Hibernate uses byte-code instrumentation to add logic to track when entities become dirty. But my scenario involved new entities, for which Hibernate could not work its magic. So on each flush Hibernate scanned the fields of each entity to see if there were changes. A linearly-increasing number of entities naturally led to a linearly-increasing time per flush. To make matters worse, Hibernate's flush algorithm apparently has a <a href="https://jira.jboss.org/jira/browse/EJBTHREE-649">performance problem</a> when dealing with cascaded collections, which I was using in my scenario.</p>
<p>The solution to my performance problem was to evict all the entities from the session after committing, thus making all entities detached, and then reattaching to the session the few entities I reused in subsequent processing.</p>
<p>This article is one of a series on <a href="http://www.basilv.com/psd/blog/2008/hibernate-tips-and-tricks">Hibernate Tips &#038; Tricks</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2010/avoiding-caching-to-improve-hibernate-performance/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Java Code Examples Available</title>
		<link>http://www.basilv.com/psd/blog/2009/java-code-examples-available</link>
		<comments>http://www.basilv.com/psd/blog/2009/java-code-examples-available#comments</comments>
		<pubDate>Mon, 11 May 2009 16:07:21 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/?p=382</guid>
		<description><![CDATA[I have added a new project called Java Examples to my Software page. This project contains the source code used as examples in the following articles: Working with Java 5 Annotations Parsing and Generating XML with Java Advanced Uses of Java 5 Enums A Tale of Bad Exception Handling in Finally Blocks in Java Automatically [...]]]></description>
			<content:encoded><![CDATA[<p>I have added a new project called <em>Java Examples</em> to my <a href="http://www.basilv.com/psd/software">Software</a> page. This project contains the source code used as examples in the following articles:</p>
<ul>
<li><a href="http://www.basilv.com/psd/blog/2008/working-with-java-5-annotations">Working with Java 5 Annotations</a></li>
<li><a href="http://www.basilv.com/psd/blog/2006/parsing-and-generating-xml-with-java">Parsing and Generating XML with Java</a></li>
<li><a href="http://www.basilv.com/psd/blog/2006/advanced-uses-of-java-5-enums">Advanced Uses of Java 5 Enums</a></li>
<li><a href="http://www.basilv.com/psd/blog/2007/a-tale-of-bad-exception-handling-in-finally-blocks-in-java">A Tale of Bad Exception Handling in Finally Blocks in Java</a></li>
<li><a href="http://www.basilv.com/psd/blog/2008/automatically-populating-audit-columns-in-hibernate">Automatically Populating Audit Columns in Hibernate</a></li>
<li><a href="http://www.basilv.com/psd/blog/2008/simple-xml-parsing-using-jaxb">Simple XML Parsing using JAXB</a></li>
<li><a href="http://www.basilv.com/psd/blog/2006/tips-for-using-log4j">Tips for Using Log4j</a></li>
<li><a href="http://www.basilv.com/psd/blog/2008/exposing-mutable-objects-as-public-properties">Exposing Mutable Objects as Public Properties</a></li>
<li><a href="http://www.basilv.com/psd/blog/2006/java-rmi-tutorial">Understanding Java RMI: A Simple Tutorial</a></li>
<li><a href="http://www.basilv.com/psd/blog/2009/java-based-configuration-of-spring">Java-Based Configuration of Spring Dependency Injection</a></li>
</ul>
<p>Code is formatted using a non-standard coding convention to display better on the website. Code is generally written with minimal comments or error-handling: it is intended for educational purposes and not necessarily for production use.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2009/java-code-examples-available/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java-Based Configuration of Spring Dependency Injection</title>
		<link>http://www.basilv.com/psd/blog/2009/java-based-configuration-of-spring-dependency-injection</link>
		<comments>http://www.basilv.com/psd/blog/2009/java-based-configuration-of-spring-dependency-injection#comments</comments>
		<pubDate>Fri, 01 May 2009 12:55:15 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/?p=353</guid>
		<description><![CDATA[At the heart of the Spring Framework is its dependency injection capabilities provided by its inversion-of-control container. Traditionally the configuration of dependencies has been done in one or more separate XML files. In these files you need to specify code-specific constructs such as the concrete classes to use as implementations of interfaces. It has always [...]]]></description>
			<content:encoded><![CDATA[<p>At the heart of the <a href="http://www.springsource.org/">Spring Framework</a> is its <a href="http://martinfowler.com/articles/injection.html">dependency injection</a> capabilities provided by its inversion-of-control container. Traditionally the configuration of dependencies has been done in one or more separate XML files. In these files you need to specify code-specific constructs such as the concrete classes to use as implementations of interfaces. It has always seemed unnatural to me to do this configuration within XML instead of using Java code. One problem, for example, is that doing renames of your classes or packages will break your XML configuration. Tooling support for these Spring XML files has admittedly helped, but doesn't overcome for me the fundamental inconsistency in using XML to configure Java code. What's wrong with using Java? Traditionally, one significant problem with doing Spring configuration within Java code is that it turned out to be much more verbose than the XML files – the Spring Java APIs were not explicitly designed and optimized to do Java-based configuration. </p>
<p>This state of affairs with Spring configuration changed with the introduction of the <a href="http://www.springsource.org/javaconfig">Spring Java Configuration project</a>, commonly called JavaConfig. JavaConfig provides roughly the same capabilities as Spring's XML-based configuration but uses Java code that is quite compact and readable. I have tried out JavaConfig and I really like it – my unease over XML configuration has been finally laid to rest. Using JavaConfig is fairly simple: you create one or more configuration classes using the conventions expected by JavaConfig, then create an instance of <code>JavaConfigApplicationContext</code> or <code>JavaConfigWebApplicationContext</code> and use it like a regular Spring <code>AppplicationContext</code>.</p>
<p>The most significant decision I think you need to make when using JavaConfig is what approach to use to define dependencies. The approach you choose determines how you go about writing your configuration class. While this decision is not specific to JavaConfig - Spring's standard XML configuration allows for the same set of approaches to be used – the application of each approach using JavaConfig is different. These three approaches to defining dependencies are:</p>
<ol>
<li>Explicit declaration of beans and dependencies.</li>
<li>Automatic dependencies via autowiring.</li>
<li>Automatic beans via component scanning.</li>
</ol>
<p>In the remainder of this article I am going to provide examples using JavaConfig for these three approaches and discuss the pros and cons of each. While the examples focus exclusively on one of the approaches at a time you can easily combine all three approaches within a single configuration class. For the sake of clarity, however, I recommend on real projects to choose one approach as the primary method of configuration. The source code listed in this article is provided in the <em>Java Examples</em> project which can be downloaded from the <a href="http://www.basilv.com/psd/software">Software</a> page.</p>
<p>All three approaches use the same configuration example. There are two beans to configure, each with an interface and an underlying implementation class. The first bean is <code>ResumeRepository</code> which is a Repository class (aka Data Access Object) that provides CRUD-type operations for the domain object <code>Resume</code>. For this example this repository interface only contains a single method <code>Set&lt;Resume&gt; findAll()</code>. The second bean is <code>CalculatorService</code>, which is a Service class that provides functionality relating to resumes:  for this example a single method <code>void validate(Resume)</code>. The Java code for these two interfaces and the domain object is provided below:</p>
<pre class="prettyprint">
package com.basilv.examples.spring;
import java.util.Set;

public interface CalculatorService
{
  boolean validate(Resume resume);
}

public interface ResumeRepository
{
  Set&lt;Resume&gt; findAll();
}

public class Resume
{
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }

  private String name;
}
</pre>
<p>The implementation class of <code>CalculatorService</code> is <code>CalculatorServiceImpl</code> which requires (has a dependency on) <code>ResumeRepository</code>. The implementation class of <code>ResumeRepository</code> is <code>HardcodedResumeRepository</code>, which is used for this example rather than a more typical repository implementation to avoid further dependencies on e.g. a DataSource. These implementation classes are the same in terms of logic across the three approaches, but there are configuration differences so the code for these classes will be shown for each approach below.</p>
<h3>Explicit Declaration of Beans and Dependencies</h3>
<p>The explicit approach requires that all beans and their dependencies be explicitly coded within the configuration class as shown below:</p>
<pre class="prettyprint">
package com.basilv.examples.spring.explicit;

import org.springframework.config.java.annotation.*;
import com.basilv.examples.spring.*;

@Configuration
public class ExplicitConfig
{
  @Bean
  public ResumeRepository resumeRepository() {
    return new HardcodedResumeRepository();
  }

  @Bean
  public CalculatorService calculatorService() {
    return new CalculatorServiceImpl(
      resumeRepository()); // Inject dependency
  }
}
</pre>
<p>The <code>@Configuration</code> annotation is needed on the class, and each bean definition method must be annotated with <code>@Bean</code>. By default bean names are based on the method names.</p>
<p>The implementation classes <code>HardcodedResumeRepository</code> and <code>CalculatorServiceImpl</code> do not require any special configuration code. Constructor injection was used in <code>CalculatorServiceImpl</code> as it is safer and results in more compact code within the configuration class. The code for these implementation classes is shown below:</p>
<pre class="prettyprint">
package com.basilv.examples.spring.explicit;

import java.util.*;
import com.basilv.examples.spring.*;

public class HardcodedResumeRepository implements ResumeRepository
{
  public Set&lt;Resume&gt; findAll() {
    HashSet&lt;Resume&gt; set = new HashSet&lt;Resume&gt;();
    Resume resume = new Resume();
    resume.setName("Coded, Hard");
    set.add(resume);

    return set;
  }
}

public class CalculatorServiceImpl implements CalculatorService
{
  private ResumeRepository resumeRepository;

  public CalculatorServiceImpl(ResumeRepository resumeRepository) {
    this.resumeRepository = resumeRepository;
  }

  public boolean validate(Resume resume) {
      for (Resume repoResume : resumeRepository.findAll()) {
        if (repoResume.getName().equals(resume.getName())) {
          return true;
        }
      }

      return false;
  }
}
</pre>
<p>As explained above, using the <code>ExplicitConfig</code> configuration class is quite simple. The unit test shown below demonstrates how it is done.</p>
<pre class="prettyprint">
package com.basilv.examples.spring.explicit;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.Set;

import org.junit.*;
import org.springframework.config.java.context.JavaConfigApplicationContext;

import com.basilv.examples.spring.*;

public class ExplicitConfigTest
{
  private JavaConfigApplicationContext context;

  @Before
  public void setUp() {
    context = new JavaConfigApplicationContext(ExplicitConfig.class);
  }

  @Test
  public void repositoryConfig() {

    ResumeRepository resumeRepository = context.getBean(ResumeRepository.class);
    Set&lt;Resume&gt; resumes = resumeRepository.findAll();
    assertNotNull(resumes);
    assertTrue(!resumes.isEmpty());
  }

  @Test
  public void serviceConfig() {

    CalculatorService service = context.getBean(CalculatorService.class);
    assertNotNull(service);

    assertFalse(service.validate(new Resume()));
  }

}
</pre>
<p>One nice feature of the <code>JavaConfigApplicationContext</code> class is that beans can be retrieved  via the <code>getBean</code> method in a type-safe manner with no casting required. The only piece of this code that is specific to this approach is the configuration class used – the other approaches to defining dependencies do not need to change anything else in this test case.</p>
<p>At first glance it may appear that the configuration class with its explicit definitions could be used directly without Spring. That would be correct for this simplistic example. In real-life usage, however, the Spring Framework provides many additional features via its container including bean life-cycle events, bean scope (singleton versus prototype), and aspects such as transactional behavior.</p>
<h3>Automatic Dependencies via Autowiring</h3>
<p>While the explicit declaration approach described above may look quite reasonable for this simplistic example, in real-life usage the number of dependencies per class can really add up, particularly for Service classes that may require five or more Repository or other Service classes. Instead of manually defining all of these dependencies, Spring allows such dependencies to be inferred using a feature called Autowiring.</p>
<p>Here is how Autowiring works. Dependencies within an implementation class must be flagged with the <code>@Autowired</code> annotation. At bean construction time the Spring container will automatically populate these dependencies by finding a matching bean based on the Java type. (Multiple beans of the same type can be handled through the use of qualifiers or through the <code>@Resource</code> annotation. See the Spring documentation for further details.) </p>
<p>While the Spring XML-based configuration can autowire both constructor and setter injection, Java-based configuration cannot autowire dependencies in constructors when beans are explicitly defined. So for the example the <code>CalculatorServiceImpl</code> class had to be changed to use setter injection. Below is the code for the configuration class and <code>CalculatorServiceImpl</code> - the <code>HardcodedResumeRepository</code> class is unchanged from the explicit declaration approach.</p>
<pre class="prettyprint">
package com.basilv.examples.spring.autowire;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.config.java.annotation.*;
import org.springframework.config.java.plugin.context.AnnotationDrivenConfig;
import com.basilv.examples.spring.*;

@Configuration
@AnnotationDrivenConfig // Needed for @Autowire to work
public class AutowireConfig
{

  @Bean
  public ResumeRepository resumeRepository() {
    return new HardcodedResumeRepository();
  }

  @Bean
  public CalculatorService calculatorService() {
    return new CalculatorServiceImpl(); // Do not need to specify dependency.
  }
}

public class CalculatorServiceImpl implements CalculatorService
{
  @Autowired
  private ResumeRepository resumeRepository;

  public boolean validate(Resume resume) {
      for (Resume repoResume : resumeRepository.findAll()) {
        if (repoResume.getName().equals(resume.getName())) {
          return true;
        }
      }

      return false;
  }

  public void setResumeRepository(ResumeRepository resumeRepository) {
    this.resumeRepository = resumeRepository;
  }
}
</pre>
<h3>Automatic Beans via Component Scanning</h3>
<p>While the previous approach did reduce the amount of configuration required, each bean still needed to be explicitly defined within the configuration class. This approach eliminates this requirement by enabling Spring to scan for component classes that are automatically configured as beans with dependencies autowired as per the previous approach. Bean implementation classes must be annotated with the annotation <code>@Component</code> or a specialization of this annotation such as <code>@Service</code> or <code>@Repository</code>. The configuration class is then configured to scan one or more packages for these classes. The code below shows the configuration class and the two implementation classes which were revised to add these annotations. </p>
<pre class="prettyprint">
package com.basilv.examples.spring.componentscan;

import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.config.java.annotation.Configuration;
import org.springframework.config.java.plugin.context.*;
import org.springframework.stereotype.*;
import com.basilv.examples.spring.*;

@Configuration
@AnnotationDrivenConfig // Needed for @Autowire to work
@ComponentScan("com.basilv.examples.spring.componentscan")
public class ComponentScanConfig
{
  // No explicit configuration needed
}

@Repository
public class HardcodedResumeRepository implements ResumeRepository
{
  public Set&lt;Resume&gt; findAll() {
    HashSet&lt;Resume&gt; set = new HashSet&lt;Resume&gt;();
    Resume resume = new Resume();
    resume.setName("Coded, Hard");
    set.add(resume);

    return set;
  }
}

@Service
public class CalculatorServiceImpl implements CalculatorService
{
  private ResumeRepository resumeRepository;

  @Autowired
  public CalculatorServiceImpl(ResumeRepository resumeRepository) {
    super();
    this.resumeRepository = resumeRepository;
  }

  public boolean validate(Resume resume) {
      for (Resume repoResume : resumeRepository.findAll()) {
        if (repoResume.getName().equals(resume.getName())) {
          return true;
        }
      }

      return false;
  }
}
</pre>
<p>The most notable thing about this example is that the configuration class is empty – all bean definitions and dependencies have been inferred via annotations. The <code>CalculatorServiceImpl</code> class is also able to use autowired constructor injection, unlike the previous approach, because the class itself is being instantiated by the Spring container rather than explicitly in our configuration class.</p>
<h3>Pros and Cons</h3>
<p>There are advantages and disadvantages to each configuration approach. The explicit declaration approach is the easiest to understand, especially for developers unfamiliar with Spring. But this approach is the most work to set up. The approach of autowiring dependencies while explicitly defining beans strikes I believe a nice balance between these two factors. Implementation classes are still explicitly referenced, which I think is important for developers unfamiliar with Spring as otherwise IDEs will report no uses of such classes within the production code base. But all dependency information can be left out. A significant disadvantage with this approach, however, is that constructor injection cannot be autowired. In general I tend to prefer constructor injection, which makes this approach pointless (as constructor dependencies still need to be explicitly defined). The third approach of using component scanning is the most aggressive in minimizing configuration needed – virtually no setup work is required. But I am concerned that this will be much harder to understand for those developers unfamiliar with Spring as a lot of magic is being performed behind the scenes. </p>
<p>Is there a clear winner? I would say no, which is one reason Spring provides for these various configuration options. While I value the ease-of-understanding provided by the first approach, I strongly dislike writing "boilerplate" code, which much of the dependency configuration is, and this pushes me towards the third approach. As per the comment below from Chris Beams, Lead for the Spring JavaConfig project, his recommendation is to use component scanning and not use the hybrid explicit beans + autowiring approach. He also points out that 'external' dependencies, such as data sources, still require explicit configuration.</p>
<p>Despite the questions over the approach to defining dependencies, I am happy with the use of Spring JavaConfig overall. One issue I encountered using Spring JavaConfig is that it is still in beta – it has not had a final 1.0 release yet. I used JavaConfig 1.0.0.M4, which worked fine with Spring 2.5.6 but does not work with Spring 3.0.M2 due to classpath issues. I discovered on the Spring forums that some portion of the JavaConfig functionality with be included in the Spring 3.0 release due out this summer, but it is unclear what is planned for the remaining functionality. See Chris Beam's comment below for more details.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2009/java-based-configuration-of-spring-dependency-injection/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Why You Should Be Using FindBugs</title>
		<link>http://www.basilv.com/psd/blog/2009/why-you-should-be-using-findbugs</link>
		<comments>http://www.basilv.com/psd/blog/2009/why-you-should-be-using-findbugs#comments</comments>
		<pubDate>Mon, 02 Mar 2009 14:35:48 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[tools]]></category>
		<category><![CDATA[defects]]></category>
		<category><![CDATA[FindBugs]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[quality]]></category>
		<category><![CDATA[software development]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/?p=284</guid>
		<description><![CDATA[Build automation has been the theme of my recent learning activities, so when I came across multiple positive references to a tool called FindBugs I decided to give it a try. My conclusion: FindBugs is worth using on all Java projects. Read below for the details. FindBugs is a Java static analysis tool that scans [...]]]></description>
			<content:encoded><![CDATA[<p>Build automation has been the theme of my recent <a href="http://www.basilv.com/psd/blog/2006/personal-learning-by-doing">learning activities</a>, so when I came across multiple positive references to a tool called <a href="http://findbugs.sourceforge.net/">FindBugs</a> I decided to give it a try. My conclusion: FindBugs is worth using on all Java projects. Read below for the details.</p>
<p>FindBugs is a Java static analysis tool that scans compiled java code for potential defects and bad programming practices. Think of it as the Java compiler on steroids: it operates in roughly the same fashion but reports on a much larger set of errors and warnings. Static analysis makes a great complement to <a href="http://www.basilv.com/psd/blog/2009/java-unit-testing-tutorial">automated unit tests</a>. Unit tests require effort to write, targets a specific piece of code, but can verify application-specific functionality. Static analysis requires no effort to write (beyond initial setup), targets the entire code base, but can only verify general code constructs. At least, that's the theory. How does it work in practice?</p>
<p>FindBugs supports a number of different ways of being used: command line, Swing GUI, integration into automated builds (i.e. via an Ant task and Hudson plugin), and Eclipse plugin. I decided to go with the plugin, and installation was as easy as adding the update site <a href="http://findbugs.cs.umd.edu/eclipse">http://findbugs.cs.umd.edu/eclipse</a> and installing it. Well, actually there were two gotchas. First, you need to be running Eclipse version 3.3 or greater for the plugin to work – RAD version 7 will not work. Second, you need to fully restart Eclipse after installing the plugin. I made the mistake of choosing the option to activate the plugin without doing a restart, which left portions of the plugin not working. It also seemed like you must bring up the Bug Explorer and Bug Details views, then restart Eclipse, in order to get those views working properly.</p>
<p>I used the <a href="http://findbugs.sourceforge.net/manual/index.html">FindBugs manual</a> to get started. I selected my <a href="http://www.basilv.com/psd/blog/2009/time-reporter-version-20-available">Time Reporter</a> project to be the first guinea pig and ran FindBugs on it. I keep my <a href="http://www.basilv.com/psd/blog/2007/why-you-should-polish-your-code">code well-polished</a> and well-tested with most Eclipse warnings turned on so I was not expecting FindBugs to turn up anything major. As I scanned the relatively small list of issues (under 30), I was surprised to see an actual defect! It actually took me a few moments of staring at it to find the problem. See the screen shot below.</p>
<p><a href="http://www.basilv.com/psd/wp-content/uploads/2009/02/findbugscaughterror.png"><img src="http://www.basilv.com/psd/wp-content/uploads/2009/02/findbugscaughterror.png" alt="" title="FindBugs finding a defect in Eclipse" class="alignnone size-medium wp-image-285" /></a></p>
<p>It turned out this same defect occurred elsewhere in the code. FindBugs also identified cases of bad error handling that I would classify as defects: I was improperly ignoring return values from method calls like <code>File.delete()</code> or <code>File.mkdir()</code>. Most of these serious issues were in test code rather than application code, which made me feel a little better (but not much).</p>
<p>I fixed all the issues reported by FindBugs that I agreed with, turned off one FindBugs warning I did not agree with at all, and was then left with a small number of false positives – incorrect warnings about code that was actually correct. My preferred approach to compiler warnings is to have none in the code base. This is based on the fact that if your code base has existing warnings that should be ignored then it becomes very difficult to tell when you write some code that produces a warning that should instead be fixed. People become blind to all warnings if there are always some present. Warnings should be produced as part of the developer's regular process (i.e. writing code) rather than requiring an extra step. It was trivial to configure FindBugs to run automatically, but how to get rid of the unwanted warnings? </p>
<p>Eclipse warnings can be eliminated by using the Java 5 annotation <code>@SuppressWarning</code>, but FindBugs does not appear to support this annotation (or if it does I could not successfully determine what text must be supplied to the annotation to ignore the warning). I found some hints on the web that FindBugs does have the ability to ignore specific warnings, but it appears this feature has only been implemented in the Swing GUI and not in the Eclipse plugin. After further investigation I found a solution. I exported the current set of project warnings to an XML file, and then configured FindBugs to use that XML file as a baseline of warnings to ignore. This causes FindBugs to only report issues not in the baseline, leaving me with an empty list of FindBugs warnings – for now at least.</p>
<p>One annoying limitation of the FindBugs Eclipse plugin is that all configuration can only be done on a per-project basis. Unlike most other Eclipse functionality with global configuration that can be overridden on a per-project basis, FindBugs must be enabled and configured individually for each project. There were a few other wrinkles with the FindBugs Eclipse plugin. My general impression is that the plugin is still in a beta state, lagging behind the functionality offered by the FindBug Swing GUI. </p>
<p>Next I tried including FindBug as part of a continuous integration build running in <a href="https://hudson.dev.java.net/">Hudson</a>. It was fairly easy to configure the Ant build to execute FindBugs on the project: the only snag was needing to allocate more memory for the JVM. Installing and configuring the FindBugs plugin for Hudson was likewise straight-forward, and resulted in a nice set of pages for viewing the trends and details regarding the FindBugs warnings. The big issue came when I wanted to filter out (exclude) all the warnings I did not want to fix. After much investigation and trial and error, I discovered that if I used the FindBug Swing GUI to create an exclude list, I could then configure the ant build to use that list as an exclude filter. Using the filter created by the Eclipse plugin did not seem to work. The FindBugs documentation concerning this was rather poor, but I did see a statement admitting that filter support was a bit messed up across the various FindBugs tools, and it sounds like this area will be targeted for improvement in the next year.</p>
<p>Despite these tooling problems, the ease with which FindBugs finds defects makes it definitely worthwhile to use on all Java projects. When I turned FindBugs loose against the code base of a reasonably-sized production application, it found 5 definite defects, 14 cases that were either potential defects or extremely bad coding style, and many other warnings (I didn't bother to count, but probably over 100) that were worth investigating and often worth fixing.</p>
<p>My conclusion from this is that using FindBugs is definitely worthwhile. I plan to roll it out to all my Java projects and integrate it into the automated builds so that the FindBugs results are also available from the continuous integration server. If you plan to adopt FindBugs then I recommend checking out some <a href="http://code.google.com/p/findbugs-tutorials/">FindBugs tutorials</a>. If you want to promote the use of FindBugs to your coworkers or management then I'll point out that FindBugs is a corporate standard at both Google and eBay and eBay reports that "using 2 developers to audit/review FindBugs warnings was 10 times more effective at finding P1 bugs than using two testers" (<a href="http://findbugs-tutorials.googlecode.com/files/UFIA-intro.pdf">http://findbugs-tutorials.googlecode.com/files/UFIA-intro.pdf</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2009/why-you-should-be-using-findbugs/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Unit Testing Tutorial</title>
		<link>http://www.basilv.com/psd/blog/2009/java-unit-testing-tutorial</link>
		<comments>http://www.basilv.com/psd/blog/2009/java-unit-testing-tutorial#comments</comments>
		<pubDate>Fri, 16 Jan 2009 13:32:24 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[unit testing]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[software development]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/?p=227</guid>
		<description><![CDATA[This tutorial explains how to set up and write automated unit tests in Java using JUnit 4, a widely used java unit testing framework, Eclipse 3, an open source integrated development environment (IDE), and Java SE 5 or later. This tutorial is intended to be an introduction to unit testing aimed at developers who have [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial explains how to set up and write automated unit tests in Java using <a href="http://junit.org/">JUnit 4</a>, a widely used java unit testing framework, <a href="http://www.eclipse.org/">Eclipse 3</a>,  an open source integrated development environment (IDE)</a>, and <a href="http://java.sun.com/">Java SE 5</a> or later. This tutorial is intended to be an introduction to unit testing aimed at developers who have limited or no experience with it. A working knowledge of Java (including Java language features new to version 5) and Eclipse is assumed. The vision for this tutorial is to enable you to adopt unit testing as part of your Java development activities by explaining how to setup, write, and use unit tests.</p>
<p>What are automated unit tests? They are a type of testing performed by developers to ensure that the code they write works as intended. They are automated: tests are expressed as test code that automatically invokes the code to be tested and verifies that the results of execution match the developer's expectations. The scope of testing is individual units of functionality within the code base, such as a single class or method.</p>
<p>This tutorial is oriented towards beginners and therefore is prescriptive. It provides a single step-by-step method for setting up and writing unit tests, usually without explaining the reasons behind why a particular method was used or what some alternative options may be.</p>
<p>This tutorial assumes you already have a Java project with some code (maybe only a single class) set up in the Eclipse IDE. As you go through the tutorial, you should apply the instructions to your project. The tutorial uses a sample project called <code>UnitTestingTutorial</code> to provide concrete examples when appropriate. Use the link below to download this project.</p>
<p><a href="http://www.basilv.com/psd/software-files/UnitTestingTutorialExampleProject-1.0.zip" class="download">Download the Unit Testing Tutorial Example Project v1.0</a></p>
<p>This tutorial is divided into a number of sections, each of which is presented as a separate page:</p>
<ul>
<li><a href="http://www.basilv.com/psd/blog/2009/java-unit-testing-tutorial-initial-setup">Section 1: Initial Setup</a>: How to install JUnit and configure your Java project to use it.</li>
<li><a href="http://www.basilv.com/psd/blog/2009/java-unit-testing-tutorial-write-your-first-unit-test">Section 2: Write Your First Unit Test</a>: Create your first automated unit test by writing test code using JUnit and watch the test pass.</li>
</ul>
<p>I plan to add additional sections to this tutorial over time. If there is particular content you are interested in let me know in the comments below.</p>
<p><strong>Next:</strong> <a href="http://www.basilv.com/psd/blog/2009/java-unit-testing-tutorial-initial-setup">Section 1: Initial Setup</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2009/java-unit-testing-tutorial/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Resume Calculator</title>
		<link>http://www.basilv.com/psd/blog/2008/resume-calculator</link>
		<comments>http://www.basilv.com/psd/blog/2008/resume-calculator#comments</comments>
		<pubDate>Mon, 15 Dec 2008 00:27:53 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[tools]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/?p=218</guid>
		<description><![CDATA[I have added a new software utility called Resume Calculator to my Software page. Resume Calculator reads resumes in XML format and calculates the total months and years of experience across specified resume entries. This is useful for responding to formal requests for proposal that require named resources to specify years of experience across various [...]]]></description>
			<content:encoded><![CDATA[<p>I have added a new software utility called <strong>Resume Calculator</strong> to my <a href="http://www.basilv.com/psd/software">Software</a> page. Resume Calculator reads resumes in XML format and calculates the total months and years of experience across specified resume entries. This is useful for responding to formal requests for proposal that require named resources to specify years of experience across various categories. </p>
<h3>Using Resume Calculator</h3>
<p>Resume Calculator is a Java program that is executed on the command-line. A sample resume file is listed below:</p>
<pre class="prettyprint">
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;resume name="Foo Bar"&gt;
  &lt;entries&gt;
    &lt;entry id="1" summary="Zoo Project" interval="2006-2007"/&gt;
    &lt;entry id="2" summary="Team Yak" interval="May, 2007 - Present"/&gt;
    &lt;entry id="Baz Project" summary="Team Lead for Baz" interval="Jan, 2008 - Mar, 2008"/&gt;
  &lt;/entries&gt;
&lt;/resume&gt;
</pre>
<p>Executing Resume Calculator against this file to calculate the duration of entries "1" and "Baz Project" produces the following output:</p>
<pre>
Resume Calculator
Copyright 2008 by Basil Vandegriend.  All rights reserved.

Executed on: 8-Dec-2008 6:29:45 AM
Working directory: C:\Dev\Java\ResumeCalculator\dist\release\example

Using resume "C:\Dev\Java\ResumeCalculator\dist\release\example\sampleResume.xml".
Calculating for "Foo Bar" the duration of the resume entries:
1: Zoo Project
Baz Project: Team Lead for Baz
<strong>Duration is 14.0 months or 1.2 years.</strong>

Finished execution successfully.
</pre>
<p>More details on how to use Resume Calculator and the full format of the XML file are specified in the user manual included with the distribution.</p>
<h3>Technical Details</h3>
<p>Resume Calculator uses <a href="https://jaxb.dev.java.net/">JAXB</a> for parsing XML (which I just <a href="http://www.basilv.com/psd/blog/2008/simple-xml-parsing-using-jaxb">wrote about</a>). I used the <a href="http://joda-time.sourceforge.net/">Joda Time</a> library for manipulating dates – it is far superior to the standard Java classes. The Joda library, however, does not include logic for calculating the duration of multiple intervals of time so I had to code that functionality myself. This proved to be an interesting algorithm to write – thank goodness for unit tests. The source code for Resume Calculator is included in the distribution if you want to learn more.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2008/resume-calculator/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Simple XML Parsing using JAXB</title>
		<link>http://www.basilv.com/psd/blog/2008/simple-xml-parsing-using-jaxb</link>
		<comments>http://www.basilv.com/psd/blog/2008/simple-xml-parsing-using-jaxb#comments</comments>
		<pubDate>Mon, 08 Dec 2008 23:12:32 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[dom4j]]></category>
		<category><![CDATA[JAXB]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/?p=210</guid>
		<description><![CDATA[I recently needed to parse a XML file using Java for a utility I was writing. A couple of years ago I used dom4j to parse XML (and wrote an article about it). I figured there had to be a more modern approach, similar to how Hibernate 3.0 can map POJOs (plain old Java objects) [...]]]></description>
			<content:encoded><![CDATA[<p>I recently needed to parse a XML file using Java for a utility I was writing. A couple of years ago I used <a href="http://www.dom4j.org/">dom4j</a> to parse XML (and wrote an <a href="http://www.basilv.com/psd/blog/2006/parsing-and-generating-xml-with-java">article</a> about it). I figured there had to be a more modern approach, similar to how <a href="http://www.hibernate.org/">Hibernate 3.0</a> can map POJOs (plain old Java objects) to relational database tables using <a href="http://www.basilv.com/psd/blog/2008/working-with-java-5-annotations">Java annotations</a>. To my surprise, a brief search on-line turned up nothing. I was sure I had heard of this being done, so I continued searching. I finally found an <a href="http://jvalentino.blogspot.com/2008/07/in-response-to-easiest-java-xml-binding.html">article</a>  with a comment by <a href="http://www.blogger.com/profile/06165108938304365215">zaeffi</a> that finally enlightened me. The answer was to use <a href="https://jaxb.dev.java.net/">JAXB 2.0</a>.</p>
<p>I had already looked at JAXB, but was turned off by the apparent requirement to generate the Java classes from a XML Schema definition of the XML format - I already had my Java POJOs and did not want to be bothered to write a schema definition. It turns out that JAXB does support my use case. The online documentation, unfortunately, seems strongly biased towards web services developers who more than likely do start with a schema definition, and possibly even have tooling that hides the use of JAXB entirely. I was also misled by older documentation referring to JAXB version 1, which does not support annotations and generates (from the schema) very ugly code that is difficult to maintain. This was fixed in JAXB version 2, which does support the use of annotations (and incidentally generates much cleaner code).</p>
<p>Using that <a href="http://jvalentino.blogspot.com/2008/07/in-response-to-easiest-java-xml-binding.html">article</a> as inspiration, I wrote a helper class for parsing XML into objects and vice-versa. The code for this helper class is shown below:</p>
<pre class="prettyprint">
// Copyright 2008 by Basil Vandegriend.  All rights reserved.

package com.basilv.examples.jaxb;

import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.*;

import org.w3c.dom.Node;

/**
 * Tools for working with the JAXB (XML Binding) library.
 */
public class XmlBindingTools
{
  /**
   * Parse the XML supplied by the reader into the
   * corresponding tree of Java objects.
   *
   * @param reader Cannot be null. The source of the XML.
   * @param rootElementClass Cannot be null. The type of the
   *        root element.
   * @return the Java object that is the root of the tree,
   *         of type rootElement.
   * @throws JAXBException if an error occurs parsing the
   *         XML.
   */
  @SuppressWarnings("unchecked")
  public static &lt;E extends Object&gt; E parseXML(
    Reader reader, Class&lt;E&gt; rootElementClass)
    throws JAXBException {

    if (rootElementClass == null)
      throw new IllegalArgumentException("rootElementClass is null");
    if (reader == null)
      throw new IllegalArgumentException("reader is null");

    JAXBContext context = JAXBContext.newInstance(rootElementClass);
    Unmarshaller unmarshaller = context.createUnmarshaller();

    CollectingValidationEventHandler handler =
      new CollectingValidationEventHandler();
    unmarshaller.setEventHandler(handler);

    E object = (E) unmarshaller.unmarshal(reader);
    if (!handler.getMessages().isEmpty()) {
      String errorMessage = "XML parse errors:";
      for (String message : handler.getMessages()) {
        errorMessage += "\n" + message;
      }
      throw new JAXBException(errorMessage);
    }

    return object;
  }

  /**
   * Generate XML using the supplied root element as the
   * root of the object tree and write the resulting XML to
   * the specified writer
   *
   * @param rootElement Cannot be null.
   * @param writer Cannot be null.
   * @throws JAXBException
   */
  public static void generateXML(Object rootElement,
    Writer writer) throws JAXBException {

    if (rootElement == null)
      throw new IllegalArgumentException("rootElement is null");
    if (writer == null)
      throw new IllegalArgumentException("writer is null");

    JAXBContext context = JAXBContext.newInstance(rootElement.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(
      Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(rootElement, writer);
  }

  private static class CollectingValidationEventHandler
    implements ValidationEventHandler
  {
    private List&lt;String&gt; messages = new ArrayList&lt;String&gt;();

    public List&lt;String&gt; getMessages() {
      return messages;
    }

    public boolean handleEvent(ValidationEvent event) {
      if (event == null)
        throw new IllegalArgumentException("event is null");

      // calculate the severity prefix and return value
      String severity = null;
      boolean continueParsing = false;
      switch (event.getSeverity()) {
        case ValidationEvent.WARNING:
          severity = "Warning";
          continueParsing = true; // continue after warnings
          break;
        case ValidationEvent.ERROR:
          severity = "Error";
          continueParsing = true; // terminate after errors
          break;
        case ValidationEvent.FATAL_ERROR:
          severity = "Fatal error";
          continueParsing = false; // terminate after fatal errors
          break;
        default:
          assert false : "Unknown severity.";
      }

      String location = getLocationDescription(event);
      String message = severity + " parsing " + location
        + " due to " + event.getMessage();
      messages.add(message);

      return continueParsing;
    }

    private String getLocationDescription(ValidationEvent event) {
      ValidationEventLocator locator = event.getLocator();
      if (locator == null) {
        return "XML with location unavailable";
      }

      StringBuffer msg = new StringBuffer();
      URL url = locator.getURL();
      Object obj = locator.getObject();
      Node node = locator.getNode();
      int line = locator.getLineNumber();

      if (url != null || line != -1) {
        msg.append("line " + line);
        if (url != null) msg.append(" of " + url);
      } else if (obj != null) {
        msg.append(" obj: " + obj.toString());
      } else if (node != null) {
        msg.append(" node: " + node.toString());
      }

      return msg.toString();
    }
  }
}
</pre>
<p>One important refinement I made to the code was to add better error handling. Parser warnings and errors are collected along with location information (such as line numbers) and the caller notified via an exception. This was necessary because the JAXB default error handling simply wrote messages to the console and reported nothing back to the caller. Due to my basic needs, I simply collected these errors and warnings into string messages. If your error-handling requirements are more sophisticated then you can collect the errors and warnings into proper objects that are returned back to the caller, who can then traverse the information and decide how to report it.</p>
<p>The unit test for this helper class is shown below. It includes a utility class with JAXB annotations to map it to XML that the tests use to exercise the functionality of the helper class. The last test exercises (in a trivial way) the additional error-handling functionality.</p>
<pre class="prettyprint">
// Copyright 2008 by Basil Vandegriend.  All rights reserved.

package com.basilv.examples.jaxb;

import static org.junit.Assert.*;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

import org.junit.Test;

public class XmlBindingToolsTest
{
  @XmlRootElement(name = "xmltest")
  static class XmlTest
  {
    private String id;

    @XmlAttribute
    public String getId() {
      return id;
    }

    public void setId(String id) {
      this.id = id;
    }

  }

  @Test
  public void parseXml() throws Exception {

    String xml = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n"
      + "&lt;xmltest id=\"test\"/&gt;";

    StringReader reader = new StringReader(xml);
    XmlTest xmlTest = XmlBindingTools.parseXML(reader,
      XmlTest.class);
    assertNotNull(xmlTest);
    assertEquals("test", xmlTest.getId());
  }

  @Test
  public void generateXml() throws Exception {
    XmlTest xmlTest = new XmlTest();
    xmlTest.setId("test");
    StringWriter writer = new StringWriter();
    XmlBindingTools.generateXML(xmlTest, writer);
    assertEquals(
      "&lt;?xml version=\"1.0\" encoding=\"UTF-8\" "
        + "standalone=\"yes\"?&gt;\n&lt;xmltest id=\"test\"/&gt;\n",
      writer.getBuffer().toString());
  }

  @Test
  public void parseInvalidXml() throws Exception {

    String xml = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n"
      + "&lt;xmltest id=\"test\"&gt;&lt;fake/&gt;&lt;/xmltest&gt;";

    StringReader reader = new StringReader(xml);
    try {
      XmlBindingTools.parseXML(reader, XmlTest.class);
      fail("Expected Exception");
    } catch (JAXBException e) {
      // Expected case.
    }
  }

}
</pre>
<p>I did encounter some surprises when using JAXB. JAXB imposes a number of coding limitations on the mapped POJOs which are actually quite similar to Hibernate, but not documented as well. So as a general guideline, use Hibernate's restrictions and you will probably do okay. One limitation I encountered involved handling a parent-child relationship between two mapped entities. The parent class will need to have a collection of child entities. The getter method to return the collection must return the actual collection used by the class and not a copy or wrapper: JAXB appears to add children to the parent by calling the add() method on the collection returned by the getter. (See <a href="http://www.basilv.com/psd/blog/2008/exposing-mutable-objects-as-public-properties">this article</a> for why you might want to return a copy of the underlying collection.)</p>
<p>JAXB shares more than just limitations with Hibernate. Many of the mapping capabilities offered by Hibernate have analogues in JAXB. JAXB can map both fields and methods, either of which may be private. JAXB also supports mapping to custom types using the <code>@XmlJavaTypeAdapter</code> annotation. </p>
<p>Despite the limitations and surprises I ran into, I was generally pleased with JAXB. It met my original goal of providing simple parsing of XML into Java POJOs using annotations to specify the mapping. My biggest disappointment with JAXB was the poor documentation. In fact, for all but trivial parsing tasks, I would recommend writing a XML Schema definition and generating the POJOs instead of doing what I did - the documentation just does not support the approach I took.</p>
<p>The source code listed in this article is provided in the <em>Java Examples</em> project which can be downloaded from the <a href="http://www.basilv.com/psd/software">Software</a> page.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2008/simple-xml-parsing-using-jaxb/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Exposing Mutable Objects as Public Properties</title>
		<link>http://www.basilv.com/psd/blog/2008/exposing-mutable-objects-as-public-properties</link>
		<comments>http://www.basilv.com/psd/blog/2008/exposing-mutable-objects-as-public-properties#comments</comments>
		<pubDate>Mon, 11 Aug 2008 14:09:25 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[design]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[software development]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/?p=129</guid>
		<description><![CDATA[I recently had an interesting design discussion with a coworker in which we discussed the pros and cons of exposing mutable objects as public properties of a class. This article provides my thoughts on the subject. An immutable class (or object) is one whose state cannot be changed once the instance is constructed. Mutable objects [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had an interesting design discussion with a coworker in which we discussed the pros and cons of exposing mutable objects as public properties of a class. This article provides my thoughts on the subject.</p>
<p>An immutable class (or object) is one whose state cannot be changed once the instance is constructed. Mutable objects do allow state changes, typically via setter methods. </p>
<p>Below is an example of such a mutable class:</p>
<pre class="prettyprint">
import java.util.Calendar;

public class Order
{
  private Calendar date = Calendar.getInstance();

  public Calendar getDate() {
    return date;
  }

  public void setDate(Calendar calendar) {
    this.date = calendar;
  }
}
</pre>
<p>The public property <code>date</code> uses Calendar as its type. Calendar is itself a mutable class – it has methods such as setters that modify the state of the class. So what is the issue?  Consider the following code:</p>
<pre class="prettyprint">
  public static void exampleOne(Order order) {
    Calendar cal = order.getDate();
    // Is order past due?
    cal.add(Calendar.DAY_OF_YEAR, -10);
    if (cal.before(Calendar.getInstance())) {
      // Order past due logic...
    }
  }
</pre>
<p>Can you spot the problem? I must admit that I failed to notice anything wrong at first glance. Here is the same example instrumented with print statements. Can you predict what will happen when you run it?</p>
<pre class="prettyprint">
import java.text.DateFormat;
import java.util.*;

public class ExampleTwo
{
  public static void exampleTwo(Order order) {
    print("Original order date = "
      + convertToText(order.getDate()));
    Calendar cal = order.getDate();

    // Is order past due?
    cal.add(Calendar.DAY_OF_YEAR, -10);
    if (cal.before(Calendar.getInstance())) {
      // Order past due logic...
    }
    print("Ending order date = "
      + convertToText(order.getDate()));
  }

  private static void print(String message) {
    System.out.println(message);
  }

  private static String convertToText(Calendar calendar) {
    return DateFormat.getDateInstance().format(
      new Date(calendar.getTimeInMillis()));
  }

  public static void main(String[] args) {
    Order order = new Order();
    exampleTwo(order);
  }
}
</pre>
<p>Below is the console output from executing this code:</p>
<pre class="box">
Original order date = 8-Aug-2008
Ending order date = 29-Jul-2008
</pre>
<p>This clearly shows the problem: the date contained in the <code>Order</code> instance is changed within the <code>exampleTwo</code> method, which is likely incorrect behavior. The trap for a developer using the <code>Order</code> class (i.e. to write <code>exampleTwo()</code>) is that they would not necessarily consider or realize that changing the <code>Calendar</code> object returned by <code>order.getDate()</code> would change the value within the instance. The <code>Order</code> class is correct (no defects) but the implementation is unsafe because a mutable object is returned from the getter. How can this be addressed?</p>
<p>One solution for this particular example is to take the order-past-due check and turn it into a method on the <code>Order</code> class that does not change the date. While I would likely employ this solution, it unfortunately does not address the underlying issue. Even if the <code>getDate()</code> method is removed, the underlying problem can occur with the <code>setDate()</code> method. The following example demonstrates this:</p>
<pre class="prettyprint">
import java.text.DateFormat;
import java.util.*;

public class ExampleThree
{
  public static void exampleThree() {
    Calendar calendar = Calendar.getInstance();
    Order firstOrder = new Order();
    Order secondOrder = new Order();
    firstOrder.setDate(calendar);

    calendar.add(Calendar.DAY_OF_YEAR, 10);
    secondOrder.setDate(calendar);

    print("First date = "
      + convertToText(firstOrder.getDate()));
    print("Second date = "
      + convertToText(secondOrder.getDate()));
  }

  private static void print(String message) {
    System.out.println(message);
  }

  private static String convertToText(Calendar calendar) {
    return DateFormat.getDateInstance().format(
      new Date(calendar.getTimeInMillis()));
  }

  public static void main(String[] args) {
    exampleThree();
  }
}
</pre>
<p>The console output is:</p>
<pre class="box">
First date = 19-Aug-2008
Second date = 19-Aug-2008
</pre>
<p>The same instance of <code>Calendar</code> is added to both <code>Order</code> instances, so when the calendar's date value is changed for assignment to the <code>secondOrder</code> instance, it also changes within the <code>firstOrder</code> instance. This is likely incorrect behavior which occurs because the setter directly stores the provided mutable object. So we end up with two references to <code>Calendar</code> in each of the <code>Order</code> instances that point to the same instance. This is called <a href="http://en.wikipedia.org/wiki/Aliasing_(computing)">aliasing</a>. </p>
<p>By now you are perhaps convinced that getters and setters should never deal with mutable objects. It is unfortunately not that simple. Consider the following example:</p>
<pre class="prettyprint">
public class Order
{
  private Customer customer;

  public Customer getCustomer() {
    return customer;
  }

  public void setCustomer(Customer customer) {
    this.customer = customer;
  }
}

public class Customer
{
  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

public class ExampleFour
{
  public static void exampleFour(Order order) {

    print("Original order customer name = "
      + order.getCustomer().getName());

    String newName = "New name";
    Customer customer = order.getCustomer();
    customer.setName(newName);

    print("Final order customer name = "
      + order.getCustomer().getName());
  }

  private static void print(String message) {
    System.out.println(message);
  }

  public static void main(String[] args) {
    Customer customer = new Customer();
    customer.setName("Starting name");
    Order order = new Order();
    order.setCustomer(customer);
    exampleFour(order);
  }
}
</pre>
<p>The console output is:</p>
<pre class="box">
Original order customer name = Starting name
Final order customer name = New name
</pre>
<p>In this case it seems perfectly reasonable to update the customer's name and have this change remembered within the <code>Order</code> instance. Before I explain the difference between these scenarios, I will show one more example involving a collection property:</p>
<pre class="prettyprint">
import java.util.*;

public class Order
{
  private Customer customer;

  public Customer getCustomer() {
    return customer;
  }

  public void setCustomer(Customer customer) {
    this.customer = customer;
  }
}

public class Customer
{
  private List<Order> orders = new ArrayList<Order>();

  public List<Order> getOrders() {
    return orders;
  }

  public void addOrder(Order order) {
    if (order == null) {
      return;
    }
    orders.add(order);
    order.setCustomer(this);
  }
}
</pre>
<p>In this case, calling <code>customer.getOrders().add(order)</code> might seem reasonable but is not correct as this will not invoke the extra logic in <code>customer.addOrder(order)</code> to call <code>order.setCustomer()</code>. </p>
<p>The following table summarizes the above discussion.</p>
<table class="fancy" cellspacing="0">
<tr>
<th>Property Type</th>
<th>Expect Modification of Original Object When Getter Result is Changed?</th>
<th>Is Actual Behavior Correct?</th>
</tr>
<tr>
<td>Calendar</td>
<td>No</td>
<td>No</td>
</tr>
<tr>
<td>Customer</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>List</td>
<td>Maybe</td>
<td>No</td>
</tr>
</table>
<p>Given that the code for the <code>Calendar</code> property compared to the <code>Customer</code> property is basically identical, why should we have different expectations of their behavior? The reason has to do with the nature of the two types. <code>Calendar</code> is a <a href="http://martinfowler.com/eaaCatalog/valueObject.html">Value Object</a> - a simple object whose equality is based on its value rather than its identity. Changing a calendar means ending up with a new date that is not equal to the original. <code>Customer</code> is a Reference Object – an object with business meaning whose identity is the basis for equality. Two customers can have the same values but be distinct. Changing a customer is just that – a change to that customer that should propagate throughout the system. For a fuller discussion of value objects see page 486 of the book <a href="http://www.amazon.ca/gp/product/0321127420?ie=UTF8&#038;tag=basilvandegri-20&#038;linkCode=as2&#038;camp=15121&#038;creative=330641&#038;creativeASIN=0321127420">Patterns of Enterprise Application Architecture</a><img src="http://www.assoc-amazon.ca/e/ir?t=basilvandegri-20&#038;l=as2&#038;o=15&#038;a=0321127420" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> by Martin Fowler. This discussion includes a short section on the risk of encountering aliasing defects if a value object is mutable and recommends that they be immutable.</p>
<p>I agree with this recommendation and I think it should be a design principle for classes: class properties that are value objects should be immutable. Many typical value object types in Java such as <code>String</code> and <code>Long</code> are already immutable, but we have already seen that others such as <code>Calendar</code> and <code>List</code> are not. What are the options in such circumstances? I will address this question in a future article.</p>
<p>The source code listed in this article is provided in the <em>Java Examples</em> project which can be downloaded from the <a href="http://www.basilv.com/psd/software">Software</a> page.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2008/exposing-mutable-objects-as-public-properties/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Working with Java 5 Annotations</title>
		<link>http://www.basilv.com/psd/blog/2008/working-with-java-5-annotations</link>
		<comments>http://www.basilv.com/psd/blog/2008/working-with-java-5-annotations#comments</comments>
		<pubDate>Sun, 04 May 2008 21:09:17 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[software development]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/blog/2008/working-with-java-5-annotations</guid>
		<description><![CDATA[Annotations are a new language feature introduced in Java 5 that allows Java code elements such as classes or methods to be annotated with structured metadata. This metadata can then be used at compile-time or at run-time by other code. Annotations are commonly used to provide configuration information for infrastructure frameworks to provide cross-cutting functionality. [...]]]></description>
			<content:encoded><![CDATA[<p>Annotations are a new language feature introduced in Java 5 that allows Java code elements such as classes or methods to be annotated with structured metadata. This metadata can then be used at compile-time or at run-time by other code. Annotations are commonly used to provide configuration information for infrastructure frameworks to provide cross-cutting functionality.</p>
<p>Using existing annotations is fairly easy - an annotation is prefixed with '@' and optionally suffixed with one or more arguments of the form (&lt;single-arg-value&gt;) or (&lt;arg1=value1&gt;, &lt;arg2=value2&gt;, ...). The annotation is added immediately before the Java code element being annotated. Below is an example using two of the standard annotations defined in Java 5 (<code>@Override</code> and <code>@SuppressWarnings</code>):</p>
<pre class="prettyprint">
package com.basilv.examples.annotations;

import java.util.ArrayList;

@SuppressWarnings("unchecked")
public class AnnotationUsage
{
  public void doStuff() {
    ArrayList list = new ArrayList();
    list.add("");
  }

  @Override
  public String toString() {
    return "AnnotationUsage";
  }
}
</pre>
<p>Defining a new annotation is a little more complicated. While an annotation is conceptually fairly similar to a Java interface - it has a name and a set of properties - the syntax for defining an annotation is, unfortunately, somewhat different from the syntax used to define an interface. This is best explained with an example:</p>
<pre class="prettyprint">
package com.basilv.examples.annotations;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target( {ElementType.TYPE, ElementType.METHOD })
public @interface MyAnnotation {
  int myProperty();
  String myPropertyWithDefault() default "";
}
</pre>
<p>The <code>@interface</code> syntax is bizarre - I mentally convert it to <code>annotation</code> - i.e. <code>public annotation MyAnnotation</code>. (I am not sure why Sun did not go with this more intuitive syntax, but I assume they did not want to create another reserved keyword for compatibility reasons - a weak reason considering that they added the assert keyword in v1.4 and the enum keyword in v5.) The declaration of individual properties is similar to the declaration of methods in interfaces except for the optional default clause that can be supplied. If no default is supplied then a value for the property must be supplied when using the annotation. </p>
<p>Annotations with no properties are allowed and are called marker annotations. Annotations with a single property by convention name the property <code>value()</code>, which allows the annotation property value to be declared without the name of the property: i.e. <code>@Annotation("value")</code>.</p>
<p>The other interesting quirk is that Java annotations (<code>@Retention</code> and <code>@Target</code>) are used when declaring an annotation. The <code>@Retention</code> annotation defines how the metadata for uses of this annotation is retained via the <code>RetentionPolicy</code> enum. The default is <code>RetentionPolicy.CLASS</code>, which stores the metadata in the compiled class file but does not necessarily load it at runtime. I think the default should have been <code>RetentionPolicy.RUNTIME</code>, which ensures that the Java VM will load the metadata and make it available at runtime. When coding my first annotation I forgot to specify the runtime retention policy. This leads to the puzzling error of not being able to retrieve at runtime the metadata on the classes using the annotation, with no indication of what was wrong. The <code>@Target</code> annotation specifies the Java code elements that this annotation can be applied to.</p>
<p>Annotation metadata is only useful if we can retrieve and act on it. The Reflections API in Java 5 was augmented with methods to retrieve annotation metadata for java code elements such as classes and methods. These annotation related methods are defined by a common interface <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/AnnotatedElement.html"><code>java.lang.reflect.AnnotatedElement</code></a>. Examples include <code>boolean isAnnotationPresent(Class)</code> and <code>T getAnnotation(Class<T>)</code>.</p>
<p>The following example shows a somewhat-realistic example of an annotation being used. I define an annotation to map URLs to methods on a class, and then implement a servlet to delegate requests to the appropriate method using this metadata. First the definition of the annotation:</p>
<pre class="prettyprint">
package com.basilv.examples.annotations;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)// or source or compile
@Target( { ElementType.TYPE, ElementType.METHOD })
public @interface WebAction {
  String url();
}
</pre>
<p>Now the class which uses this annotation to map URLs to particular methods:</p>
<pre class="prettyprint">
package com.basilv.examples.annotations;

import javax.servlet.http.*;

public class ActionExecutor
{
  private HttpServletRequest request;
  private HttpServletResponse response;

  public ActionExecutor(HttpServletRequest request,
    HttpServletResponse response) {
    this.request = request;
    this.response = response;
  }

  private void setMessage(String message) {
    request.setAttribute("message", message);
  }

  @WebAction(url = "/test")
  public void doTest() {
    // Set a message for demo purposes. Normally execute some logic.
    setMessage("test");
  }

  @WebAction(url = "/submit")
  public void submitWork() {
    // Set a message for demo purposes. Normally execute some logic.
    setMessage("submit");
  }
}
</pre>
<p>Now the servlet to handle requests by executing the appropriate method on <code>ActionExecutor</code>:</p>
<pre class="prettyprint">
package com.basilv.examples.annotations;

import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.*;
import javax.servlet.http.*;

public class DispatchingServlet extends HttpServlet
{
  @Override
  protected void doGet(HttpServletRequest request,
    HttpServletResponse response) throws ServletException,
    IOException {
    response.setContentType("text/html;charset=UTF-8");

    String url = request.getPathInfo();
    ActionExecutor executor = new ActionExecutor(request,
      response);

    boolean actionFound = false;
    for (Method method : ActionExecutor.class.getMethods()) {
      if (method.isAnnotationPresent(WebAction.class)) {
        WebAction action = method.getAnnotation(
          WebAction.class);
        if (url.equals(action.url())) {
          actionFound = true;
          try {
            method.invoke(executor, (Object[]) null);
          } catch (Exception e) {
            throw new RuntimeException(
              "Error invoking method " + method.getName()
                + ".", e);
          }
        }
      }
    }
    if (!actionFound) {
      request.setAttribute("message", "No action found");
    }
    RequestDispatcher rd = request
      .getRequestDispatcher("/view.jsp");
    rd.forward(request, response);
  }

  @Override
  protected void doPost(HttpServletRequest request,
    HttpServletResponse response) throws ServletException,
    IOException {
    doGet(request, response);
  }
}
</pre>
<p>There are a number of scenarios for which it would be useful to iterate over all classes with a particular annotation. The above example of the dispatching servlet would be more useful if it could dispatch to methods on any number of classes based on a class-level annotation. The Reflections API, however, does not provide a mechanism for obtaining all classes with a particular annotation. This is difficult functionality to provide for a <a href="http://bill.burkecentral.com/2008/01/14/scanning-java-annotations-at-runtime/">number of reasons explained by Bill Burke</a>, but since it is so valuable various open source projects such as Spring and JBoss have implemented solutions to this.</p>
<p>The source code listed in this article is provided in the <em>Java Examples</em> project which can be downloaded from the <a href="http://www.basilv.com/psd/software">Software</a> page.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2008/working-with-java-5-annotations/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

