Category Archives: Spring

SIMPLE SPRING IOC EXAMPLE FOR BEGINNER THAT WORKS!!!

This Hello World program demonstrates basic features of Spring IoC.
This program shows:
1. How to load configurations from a XML resource file that is residing in the file system.
2. How to load configurations from many XML resource files that are residing in the file system.
3. How to load configurations from a XML resource file sitting in the class path.

If you already know how to create a Java Project in Eclipse then here’s the project structure.
Make sure following JAR files fall into your Build Path.

Project Structure
C:\workspace\HelloSpringIOC
|   .classpath
|   .project
|
+—bin
|       HelloSpringIoC.class
|       MyBeansInClassPathHere.xml
|       OtherPojo.class
|       SpecialPojo.class
|
+—lib
|       commons-logging-api-1.1.jar
|       spring-beans-2.0-m1.jar
|       spring-context-2.0-m1.jar
|       spring-core-2.0-m1.jar
|
\—src
HelloSpringIoC.java
MyBeansInClassPathHere.xml
OtherPojo.java
SpecialPojo.java

Make sure following two files exist in your C:\> drive.
1. C:\config\MyOtherBeansHere.xml
2. C:\config\MySpecialBeansHere.xml

Notice MyBeansInClassPathHere.xml highlighted in yellow.
It’s into the class path being in “src” folder.
You will need ClassPathXmlApplicationContext for loading this configuration file.
See method: public void usingClassPathXmlApplicationContext(String myBeanResource)

Run HelloSpringIoc as Java Application once you have created following files.

SpecialPojo.java
public class SpecialPojo {

private String name;

public SpecialPojo() {

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

OtherPojo.java
public class OtherPojo {

private String name;

public OtherPojo() {

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

C:\config\MySpecialBeansHere.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD// BEAN//EN” “http://www.springframework.org/dtd/spring-beans.dtd”&gt;
<beans>
<bean id=”specialPojo” class=”SpecialPojo”/>
</beans>

C:\config\MyOtherBeansHere.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD// BEAN//EN” “http://www.springframework.org/dtd/spring-beans.dtd”&gt;
<beans>
<bean id=”otherPojo” class=”OtherPojo”/>
</beans>

MyBeansInClassPathHere.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD// BEAN//EN” “http://www.springframework.org/dtd/spring-beans.dtd”&gt;
<beans>
<bean id=”otherPojo” class=”OtherPojo”/>
</beans>

HelloSpringIoc.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
* @author Anup Jani
* This Hello World program demonstrates basic features of Spring IoC.<br>
* This program shows:<br>
* 1. How to load configurations from a XML resource file that is residing in the file system.<br>
* 2. How to load configurations from many XML resource files that are residing in the file system.<br>
* 3. How to load configurations from a XML resource file sitting in the class path.<br>
*/
public class HelloSpringIoC {

/**
* Application Context pointing to the resource file in the file system location.
* @param myBeanResource
*/
public void usingFileSystemXmlApplicationContext(String myBeanResource) {
//Create an application context.
ApplicationContext ctx = new FileSystemXmlApplicationContext(myBeanResource);
SpecialPojo pojo = (SpecialPojo)ctx.getBean(“specialPojo”);

//Start using your bean.
pojo.setName(“This is my special bean, obtained via FileSystemXmlApplicationContext.”);
System.out.println(pojo.getName());
}

/**
* Application Context pointing to the multiple resource files in the file system location.
* @param myBeanResources
*/
public void usingFileSystemXmlApplicationContext(String[] myBeanResources) {
//Create an application context.
ApplicationContext ctx = new FileSystemXmlApplicationContext(myBeanResources);

//Get beans from multiple configurations.
String[] beanNames = ctx.getBeanDefinitionNames();
SpecialPojo specialPojo = (SpecialPojo)ctx.getBean(beanNames[0]);
OtherPojo otherPojo = (OtherPojo)ctx.getBean(beanNames[1]);

//Start using your beans.
specialPojo.setName(“Special bean obtained  from one configuration.”);
otherPojo.setName(“Other bean obtained from other configuration.”);
System.out.println(specialPojo.getName() + “\n” + otherPojo.getName());
}

/**
* Application Context pointing to the resource file in the class path.
* @param myBeanResource
*/
public void usingClassPathXmlApplicationContext(String myBeanResource) {
//Create an application context.
ApplicationContext ctx = new ClassPathXmlApplicationContext(myBeanResource);

//Get bean from application context.
OtherPojo pojo = (OtherPojo)ctx.getBean(“otherPojo”);

//Start using your bean.
pojo.setName(“This is my bean that was configured in class path, \n\t ” +
“obtained via ClassPathXmlApplicationContext.”);
System.out.println(pojo.getName());
}

/**
* Main method to test each method.
* @param args
*/
public static void main(String[] args) {
//Single file in file system.
String file = “C:\\config\\MySpecialBeansHere.xml”;

//Multiple files in file system.
String files[] = new String[]{“C:\\config\\MySpecialBeansHere.xml”, “C:\\config\\MyOtherBeansHere.xml”};

//Single file in class path.
String fileInClassPath = “MyBeansInClassPathHere.xml”;

HelloSpringIoC hello = new HelloSpringIoC();

hello.usingFileSystemXmlApplicationContext(file);
hello.usingFileSystemXmlApplicationContext(files);
hello.usingClassPathXmlApplicationContext(fileInClassPath);
}

}

Recommended source:
Java Development with the Spring Framework
– Rod Johnson, Juergen Hoeller, Alef Arendsen, Thomas Risberg, Colin Sampaleanu
Professional Seires – Wrox publication.

Anup Jani
(Bsc, MCP, GNIIT, SCJP, SCWCD, IBM OOAD/UML JCert.)
My profile: http://www.linkedin.com/in/anupjani
My Blog: http://www.anupjani.wordpress.com

Advertisements