1. Import relevant jars for basic IoC purpose
1) Download spring-framework-3.2.2.RELEASE-dist.zip from http://www.springsource.org/download/community
2) Open eclipse and set up user library
3) Import relevant jars including:
(1) spring-core-3.2.2.RELEASE.jar
(2) spring-beans-3.2.2.RELEASE.jar
(3) spring-expression-3.2.2.RELEASE.jar
(4) spring-context-3.2.2.RELEASE.jar
4) Download commons-logging-1.1.2-bin.zip from http://commons.apache.org/proper/commons-logging/download_logging.cgi
5) Import relevant jars including:
(1) commons-logging-1.1.2.jar
2. Create new java project and import user library created before. And import JUnit4 lib for unit test.
3. Start coding:
1) beans.xml [In the root directory of src folder]
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="student" class="edu.xmu.domain.Student"> <!-- collaborators and configuration for this bean go here --> <property name="id" value="1"/> <property name="name" value="Davy"/> <property name="age" value="23"/> </bean> </beans>
2) POJO
package edu.xmu.domain;
public class Student {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
3) Test case
package edu.xmu.domain;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StudentTest
{
@Test
public void test()
{
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) ac.getBean("student");
System.out.println("Student info: ");
System.out.println("ID: " + student.getId() + ", Name: "
+ student.getName() + ", Age: " + student.getAge());
}
}