A 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
Code:
public interface SayHello
{
public String Helloworld(String a);
}
Here is implementation class SayHelloImpl
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 XML Configuration File
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>
Here is Client Program
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); }
}
}
Now run the program
Suppose you have all files in springdemo directory
Code:
f:\springdemo>javac say*.java
f:\springdemo>javac helloimpl.java
f:\springdemo>javac helloclient.java
f:\springdemo>java helloclient "sam"