Spring Framework Sample Main Method

2/05/2008 Posted by tmarthal link

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

Labels: , , , , , ,

Post a Comment (2 comment(s))




Comments:

Anonymous Tirza At 11/10/2008 11:13 PM:

Good post.

Anonymous Anonymous At 1/21/2012 7:59 AM:

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.


[ April 2005 | May 2005 | June 2005 | July 2005 | August 2005 | September 2005 | October 2005 | November 2005 | December 2005 | January 2006 | February 2006 | June 2006 | August 2006 | September 2006 | January 2007 | May 2007 | July 2007 | September 2007 | October 2007 | November 2007 | February 2008 | March 2008 | April 2008 | November 2008 | ]
Subscribe to Posts [Atom]

Creative Commons License tmarthal
This work is licensed under a Creative Commons Attribution 3.0 United States License.
[ written blog : moblog ]