天天看點

spring 環境

準備階段:

到Spring官網下載下傳所需的API包,其中spring-framework-X.X.X.RELEASE-with-docs.zip壓縮包需要下載下傳,裡面的dist目錄下有所需的API,還有一個是com.springsource.org.apache.commons.logging-1.1.1.jar需要下載下傳,下載下傳位址:​​http://www.springsource.org/download/community?project=Spring%2520Framework​​

圖解:

我已經下載下傳還了所需的API包,版本是3.1.1的,可以直接下載下傳使用:​​http://pan.baidu.com/share/link?shareid=28989&uk=2198762756​​

測試階段:

項目結構:

所需包導入:

其實有些jar包在這個測試中是不需要的,不過為了友善也不想逐一測試了。

com.sunflower.pojo.Student:

1 package com.sunflower.pojo; 2  3 /** 4  *  學生實體類 5  */ 6 public class Student { 7     private String name; 8     private int age; 9 10     public String getName() {11         return name;12     }13 14     public void setName(String name) {15         this.name = name;16     }17 18     public int getAge() {19         return age;20     }21 22     public void setAge(int age) {23         this.age = age;24     }25 26 }      

 這個類在這個測試中沒有實際用途,隻不過讓這個測試看起來有這個意思。

com.sunflower.yuan.dao.StudentDAO:

1 package com.sunflower.yuan.dao; 2  3  4 /** 5  * 學生資料存取接口,操作底層資料 6  */ 7 public interface StudentDAO { 8     public void saveStudent(); 9 10     public void removeStudent();11 }      

com.sunflower.yuan.daoimp.StudentDAOImp:

1 package com.sunflower.yuan.daoimp; 2  3 import com.sunflower.yuan.dao.StudentDAO; 4  5 /** 6  * 學生資料存取實作類 7  */ 8 public class StudentDAOImp implements StudentDAO { 9 10     @Override11     public void removeStudent() {12         System.out.println("Student deleted! Spring test success");13     }14 15     @Override16     public void saveStudent() {17         System.out.println("Student saved! Spring test success");18     }19 20 }      

com.sunflower.yuan.service.StudentService:

1 package com.sunflower.yuan.service; 2  3 /** 4  * 學生業務處理接口 5  */ 6 public interface StudentService { 7     public void saveStudent(); 8  9     public void removeStudnet();10 }      

 com.sunflower.yuan.serviceimp.StudentServiceImp:

1 package com.sunflower.yuan.serviceimp; 2  3 import com.sunflower.yuan.dao.StudentDAO; 4 import com.sunflower.yuan.daoimp.StudentDAOImp; 5 import com.sunflower.yuan.service.StudentService; 6  7 /** 8  * @author Caihanyuan 9  * @time 2012-9-11 下午07:47:5510  */11 public class StudentServiceImp implements StudentService {12     private StudentDAO studentDao = new StudentDAOImp();13 14     @Override15     public void removeStudnet() {16         studentDao.removeStudent();17     }18 19     @Override20     public void saveStudent() {21         studentDao.saveStudent();22     }23 24 }      

 com.sunflower.yuan.test.Test:

1 package com.sunflower.yuan.test; 2  3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5  6 import com.sunflower.yuan.service.StudentService; 7 import com.sunflower.yuan.serviceimp.StudentServiceImp; 8  9 /**10  * @author Caihanyuan11  * @time 2012-9-11 下午07:53:5712  */13 public class Test {14     public static void main(String[] args) {15         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");16         17         //得到注入的bean18         StudentService studentService = context.getBean("studentService", StudentServiceImp.class);19         studentService.saveStudent();20         studentService.removeStudnet();21     }22 }      

第15行是通過解析xml配置檔案擷取Spring容器,第18行得到在配置檔案中注入到Spring中的對象,注入的對象不能是接口。

applicationContext.xml:

1 <?xml version="1.0" encoding="UTF-8"?>2 <beans3     xmlns="http://www.springframework.org/schema/beans"4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"5     xmlns:p="http://www.springframework.org/schema/p"6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">7     8     <bean id="studentService" class="com.sunflower.yuan.serviceimp.StudentServiceImp"></bean>9 </beans>