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

Anonymous 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.
KBC is india's Most awaiting TV Show to give you chance to win bumper cash prizes in KBC Show, Register in KBC and Win extra cash to change your fate. Its all about KBC.
KBC Lottery Number Check
KBC Winner List 25 Lakh
Kbc Head Office Real Number
Kevin Davidson said…
It's very useful blog post with inforamtive and insightful content and i had good experience with this information. We, at the CRS info solutions ,help candidates in acquiring certificates, master interview questions, and prepare brilliant resumes.Find top Salesforce admin interview questions in 2020.
These Salesforce developer interview questions are highly helpful in 2020. You can read these Salesforce lightning interview questions and Salesforce integration interview questions which are prepared by industry experts.
Aadhya said…
Myself so glad to establish your blog entry since it's actually quite instructive. If it's not too much trouble continue composing this sort of web journal and I normally visit this blog. Examine my administrations.  
Read these Salesforce Admin Certification Topics which are really helpful. I read these Salesforce Admin and Developer Certification Dumps and very much useful for me.   
Vennala said…
I am so happy to found your blog post because it's really very informative. Please keep writing this kind of blogs and I regularly visit this blog. Have a look at my services.
I have found this Salesforce training in India worth joining course. Try this Salesforce training in Hyderabad with job assistance. Join Salesforce training institutes in ameerpet with certification. Enroll for Salesforce online training in hyderabad with hands on course. 

Popular posts from this blog

My Yahoo! Answers Science Post

Using PyCrypto with Spring Crypto/Spring Security Default Encoders