Saturday, January 12, 2008

Simplest Spring Example

This entry contains a very simple example using the Spring Framework that is meant to show a minimum amount of code while at the still same time illustrating how Spring's XML configuration interacts with Spring Beans and with the Java application driving the entire example.

The SimpleSpringBean.java code for the simple bean class is shown next. Note that there is nothing Spring-specific about this class.

package marx.spring;

/**
 * Simplistic example of a Spring-loaded bean.  Perhaps most important to notice
 * for this example is that there is no use of Spring-specific code in this bean
 * and "Spring" only appears in the class name and package name and neither
 * requires its presence.  In other words, this is a boring old Java class, but
 * it can still be easily Springified.
 *
 * @author Dustin
 */
public class SimpleSpringBean
{
   private String action = "DefaultAction";
   public String getAction()
   {
      return this.action;
   }

   public void setAction(final String aNewAction)
   {
      System.out.println("Changing " + this.action + " to " + aNewAction + "!");
      this.action = aNewAction;
   }
}

The class to start the Spring container and take advantage of our bean and Spring configuration, SimpletonSpringExample.java, is shown next:

package marx.spring;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

/**
 * Simple example of Spring Framework in action.
 *
 * @author Dustin
 */
public class SimpletonSpringExample
{
   /**
    * Main application for showing simple Spring example.
    *
    * @param aArgs Command-line arguments; only expecting Strings to be set to
    *              Spring-loaded bean.
    */
   public static void main(String[] aArgs)
   {
      BeanFactory factory =
         new XmlBeanFactory(new FileSystemResource("config/SimpleSpring.xml"));
      SimpletonSpringExample simpleton =
         (SimpletonSpringExample) factory.getBean("beansIsBeans");
      final int numberOfCommandLineArgs = aArgs.length;
      for ( int i=0; i < numberOfCommandLineArgs; ++i )
      {
         simpleton.setAction(aArgs[i]);
      }
   }

The executable class whose code is shown immediately above starts up a Spring container via the XmlBeanFactory, which provides an instantiation of the bean whose code was shown earlier.

The beansIsBeans string in the code above ties to an ID in the XML configuration file and that is how Spring knows which bean to instantiate. Here is that XML file (config/SimpleSpring.xml):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
   <bean id="beansIsBeans"
      class="marx.spring.SimpleSpringBean">
      <property name="action" value="Spring Default" />
   </bean>
</beans>

Spring supports configuration with mechanisms other than XML (such as via Java, properties files, and Groovy; books, articles, blog entries, and discussions with colleagues lead me to believe that XML (including XML Schema-based configuration) is still the primarily used format for Spring configuration.

The output confirms Spring's injection of the bean (click on image to see larger version).

The output shows that the bean was originally instantiated with its single property set to "DefaultAction" as set in the class member declaration. The output showing this value changing from "DefaultAction" to "Spring Default" is the Spring container injecting the "Spring Default" value as prescribed in the Spring XML configuration file. The remainder of the value changes are from the setAction calls performed directly on the bean instance returned from Spring.

This example has shown a minimal Spring-based application. As shown in the bean source code, that bean did not need to know anything about its being used as a Spring bean. Spring is designed to be as least intrusive as possible. This was also a main point of my coverage of the Spring Framework used with Oracle JDBC in the Oracle Technology Network (OTN) article Add Some Spring to Your Oracle JDBC Access (November 2005).

No comments: