Results 1 to 1 of 1
Thread: Sample Spring Application
-
Sample Spring Application
For a sample application of spring copy following jar file in your class path settings
1. spring.jar from spring12\dist folder
2. commons-logging.jar from tomcat41\server\lib folder
Now we will make following files
1. An interface that defines the functions.
2. An Implementation of Interface containing properties, its setter and getter methods, functions etc.
3. A XML file called Spring configuration file.
4. Client program to use the function.
Here is the interface SayHello
Here is implementation class SayHelloImplJava Code:public interface SayHello { public String Helloworld(String a); }
Here is XML Configuration FileJava Code:public class SayHelloImpl implements SayHello { private String greeting ; public SayHelloImpl () { } public SayHelloImpl(String a) { greeting=a; } public String sayHelloImpl(String s) { return greeting+s; } public void setGreeting(String a) { greeting=a; } }
Here is Client ProgramJava Code:<?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="hello" class="SayHelloImpl"> <property name="greeting"> <value>Good Morning.</value> </property> </bean> </beans>
Now run the program. Suppose you have all files in springdemo directoryJava Code:import java.io.*; import org.springframework.beans.factory.* ; import org.springframework.beans.factory.xml.*; import org.springframework.core.io.*; public class SayHelloClient { public static void main(String args[]) throws Exception { try { System.out.println("please Wait."); Resource res = new ClassPathResource(" hello.xml"); BeanFactory factory = new XmlBeanFactory(res); sayhello bean1 = (sayhello)factory.getBean("hello"); String s = bean1.sayHello(args[0]); System.out.println(s); } catch(Exception e1) { System.out.println(""+e1); } } }
f:\springdemo>javac say*.java
f:\springdemo>javac helloimpl.java
f:\springdemo>javac helloclient.java
f:\springdemo>java helloclient "sam"
Similar Threads
-
Sample ANT Script
By Java Tip in forum Java TipReplies: 0Last Post: 01-24-2008, 03:22 PM -
Sample Session Beans
By Java Tip in forum Java TipReplies: 0Last Post: 12-29-2007, 04:53 PM -
Sample Entity Bean
By Java Tip in forum Java TipReplies: 0Last Post: 12-28-2007, 10:41 AM -
A Sample spring Application
By JavaBean in forum Java TipReplies: 0Last Post: 09-26-2007, 08:32 PM -
java Sample 77 3 when using Eclipse
By Albert in forum EclipseReplies: 1Last Post: 07-05-2007, 06:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks