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.   
Maradona Jons said…
Along with it also has a live casino 24 hours a day. สมัคร ufa Bet online gambling website is available to play with all casinos, baccarat, slots, roulette, dice and many other card games. We have gathered it here. With many promotions Apply for UEFA Bet now, we are happy to serve you all. With a professional team ready to solve problems And serve all of you There is a team to support the service. And answer your questions 24 hours a day.

If you are looking for a good website, ufa, UEFA BET casino site. Which can be played as a complete solution, able to answer Both in terms of quality and performance that are quite diverse It is best to have a lot of these things.


Hi Every One said…
WsCube Tech is a top-class institute for learning Ethical Hacking, Penetration Testing, and more cybersecurity skills. Wifi Hack Online
ShareMouse Crack 2022 is the perfect utility for providing the ability to share the keyboard and mouse among various systems and devices. ShareMouse Crack 2022
Silent Girl said…
In this lesson, you'll learn how to participate in Christmas festivities, the German way: There are plenty of interesting Christmas greetings Romantic Christmas Love Quotes For Him

Popular posts from this blog

My Yahoo! Answers Science Post

Tailgates

Blog Code Formatting