spring 环境

引用:http://www.189works.com/article-96241-1.html

准备阶段:

到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>

 

posted @ 2012-09-17 15:23  镇水古月  阅读(153)  评论(0编辑  收藏  举报