Spring Framework Sample Main Method

So my desk-mate* and I were having a conversation about an upcoming coding project, and he remarked how nice it was for someone, somewhere to post a complete sample on their blog for the spiders to index, so that he can find it. So I thought I would also, pay it forward and hopefully someone in like 2012 comes by and needs a sample main method using the spring spring framework for application level dependency injection.

Everyone out there seems to have used the MVC and deploying spring based apps in a web server WAR file. Except for this link: Levarage Spring Web development with Offspring. [Offtopic: it uses classes in the com.offspring.* package, like the band's website!] It uses a simple bean file and a main method really close to the following snippet to instantiate (that's my new favorite word) the application using the spring context**:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.blogger.tmarthal.listener.SampleListenerInterface;

/**
 * simple class showing the a listener interface
 * starting listening
 */
public class simpleParserAndStorage
{
  /**
   * The spring context main method
   * @param args
   */
  public static void main(String[] args)
  {
    if (args.length != 1) {
      System.out.println("Usage: java parser.java ");
      System.exit(1);
    }

    String springContextFile = args[0];
    System.out.println(springContextFile);
    ApplicationContext context = new ClassPathXmlApplicationContext(springContextFile);

    // the listener just starts listening
    SampleListenerInterface listener = (SampleListenerInterface) context.getBean("listener");
    listener.startListening();
  }
}

Now, the wiring, or what sort of implementation is the SampleListenerInterface? This is where dependency injection comes in. Depending on the type of thing you are listening for (or what the example is doing) the implementation class is going to change. The main method is going to remain the same, the bean wiring takes place in the xml file, rather than in the application logic.

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 <beans>
    <bean id="listenerHelper" class="com.blogger.tmarthal.listener.SampleListenerHelperInterfaceImpl">
    </bean>

    <bean id="listener" class="com.blogger.tmarthal.listener.SampleListenerInterfaceImpl">
      <!-- location of what it is listening on, passed in as constructor arguments -->
      <constructor-arg><value>localhost</value></constructor-arg>
      <constructor-arg><value>10001</value></constructor-arg>
      <!-- and it refers to a different class that would help it listen -->
      <constructor-arg><ref bean="listenerHelper"/></constructor-arg>
    </bean>
 </beans>

So, assuming that there is a Interface implementation of SampleListenerInterfaceImpl and SampleListenerHelperInterfaceImpl in the classpath, then the spring framework will call the listen method of the SampleListenerInterfaceImpl class!

Pretty neat. Hopefully someone, sometime stumbles upon this example trying to get into writing a non-WAR spring framework application, and this post will be helpful to them. I wish I found something like this first. :)

* no, not my office mate: we share a desk
** i know the code formatting sucks

Comments

Tirza said…
Good post.
Anonymous said…
In order to address this issue, I have created https://jira.springsource.org/browse/SPR-9044. If you like the proposed approach, please vote for it.

Popular posts from this blog

CSS Templates and Blog Spam

my foray into xmlrpc - calling perl from java

gmail chat