Magazine
 
Spring
 
 

parse bean definitions in properties files, and create
BeanFactories programmatically.

Each bean definition can be a POJO (defined by class name and JavaBean initialisation properties or constructor arguments), or a FactoryBean. The FactoryBean interface adds a level of indirection.

BeanFactories can optionally participate in a hierarchy, “inheriting” definitions from their ancestors. This enables the sharing of common configuration across a whole application, while individual resources such as controller servlets also have their own independent set of objects.

Through its bean factory concept, spring is an Inversion of Control container. A Spring BeanFactory is a container that can be created in a single line of code, and requires no special deployment steps. Spring is most closely identified with a flavor of Inversion of Control known as Dependency Injection. Now let’s develop a Hello example with Spring support.

D:\>md springdemo
D:\>cd springdemo

As the entire Spring Framework is included in spring.jar. We
use it to run our examples.

Copy spring.jar from spring1.2.9\dist folder to the working
folder (springdemo). Also copy commons-logging.jar from
apache-tomcat-6.0.10 to the working directory.

Set path for jdk1.4.2 and above versions only
Now Set the classpath as shown :

D:\>springdemo >set classpath=D:\springdemo;
D:\springdemo\spring.jar;
D:\springdemo\commons-logging.jar

For a typical Spring Application we need the following files

I An interface that defines the functions.
II An Implementation that contains properties, its setter
and getter methods, functions etc.,
III A XML file called Spring configuration file.
IV Client program that uses the function.

Create the following files


1 hello.java
2 helloimpl.java
3 hello.xml
4 helloclient.java


1. D:\springdemo\hello.java

public interface hello
{
public String sayhello(String a);
}

 

2. D:\springdemo\helloimpl.java

public class helloimpl implements hello {
private String greeting;
public helloimpl()
{}
public helloimpl(String a) {
greeting=a; }
public String sayhello(String s) {
return greeting+s; }
public void setGreeting(String a)
{
greeting=a;
}}

3. D:\springdemo\hello.xml

<?xml version=”1.0" encoding=”UTF-8"?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//
EN” “http://www.springframework.org/dtd/springbeans.
dtd”>
<beans>
<bean id=”hello” class=”helloimpl”>
<property name=”greeting”>
<value>Good Morning!...</value>
</property>
</bean>
</beans>

4. D:\springdemo\helloclient.java

import java.io.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;
import org.springframework.core.io.*;
public class helloclient {
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);
hello bean1 = (hello)factory.getBean(“hello”);
String s = bean1.sayhello(args[0]);
System.out.println(s);
}
catch(Exception e1) {
System.out.println(“”+e1);
}}}

Now, execute the programs:

D:\springdemo>javac hello.java
D:\springdemo>javac helloimpl.java
D:\springdemo>javac helloclient.java hello
D:\springdemo>java helloclient AMIT

August 2007 | Java Jazz Up |23
 
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40          Download PDF