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
KBC Lottery Number Check
KBC Winner List 25 Lakh
Kbc Head Office Real Number
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.
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.
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.