搭建 Spring 2.5.6 开发环境

1、jar 包准备:

  spring 2.5.6 的 jar 包(链接: http://pan.baidu.com/s/1skVFfcx 密码: mbiz),如图:

  commons-logging-1.2(链接: http://pan.baidu.com/s/1dFw1Ukp 密码: t9g2),如图:

  JUnit 4.7(链接: http://pan.baidu.com/s/1qYdiIAw 密码: msfp),如图:

2、建立 User Library

  2.1 建立 spring 2.5.6 的 User Library

    2.1.1 通过路径 Window --> Preferences --> Java --> Build Path --> User Libraries

    2.1.2 创建步骤,如下列的图:

    这样 spring 2.5.6 Library 就创建好了,以后如果有新的jar包要加进来,可以选中library、点击 Add External JARs进行添加,想加多少加多少。

  2.2 建立 JUnit4 的 User Library

    2.2.1 通过路径 Window --> Preferences --> Java --> Build Path --> User Libraries

    2.2.2 创建步骤,如下列的图:

      进入添加User Library 的界面后

    建好的两个 Library 如上图。

3、创建相关的一些类和接口,本测试程序创建了 User、UserDAO、UserDAOImpl、UserService,代码如下:

  User

 1 package com.bjsxt.model;
 2 
 3 public class User {
 4     private String username;
 5     
 6     private String password;
 7 
 8     public String getUsername() {
 9         return username;
10     }
11 
12     public void setUsername(String username) {
13         this.username = username;
14     }
15 
16     public String getPassword() {
17         return password;
18     }
19 
20     public void setPassword(String password) {
21         this.password = password;
22     }
23 }
View Code

  UserDAO

1 package com.bjsxt.dao;
2 
3 import com.bjsxt.model.User;
4 
5 public interface UserDAO {
6     public void save(User u);
7 }
View Code

  UserDAOImpl

 1 package com.bjsxt.dao.impl;
 2 
 3 import com.bjsxt.dao.UserDAO;
 4 import com.bjsxt.model.User;
 5 
 6 public class UserDAOImpl implements UserDAO{
 7 
 8     public void save(User u) {
 9         System.out.println("a user saved!");
10     }
11 
12 }
View Code

  UserService

 1 package com.bjsxt.service;
 2 
 3 import com.bjsxt.dao.UserDAO;
 4 import com.bjsxt.model.User;
 5 
 6 public class UserService {
 7     
 8     private UserDAO userDAO;
 9     
10     public void addUser(User u){
11         this.userDAO.save(u);
12     }
13 
14     public UserDAO getUserDAO() {
15         return userDAO;
16     }
17 
18     public void setUserDAO(UserDAO userDAO) {
19         this.userDAO = userDAO;
20     }
21 }
View Code

4、编写与上面类对应的配置文件 beans.xml(名字随便取)

  beans.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 6 
 7   <bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl"></bean>
 8 
 9   <bean id="userService" class="com.bjsxt.service.UserService">
10       <property name="userDAO">
11           <ref bean="u"/>
12       </property>
13   </bean>
14 
15 </beans>
View Code

5、编写 spring 测试类:

   UserServiceTest

 1 package com.bjsxt.service;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import com.bjsxt.model.User;
 8 
 9 public class UserServiceTest {
10 
11     @Test
12     public void testAddUser() throws Exception {
13         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");//初始化ApplicationContext对象,加载配置文件beans.xml
14         
15         UserService service = (UserService)ctx.getBean("userService");//从容器中取
16         User u = new User();
17         u.setUsername("zhangsan");
18         u.setPassword("zhangsan");
19         service.addUser(u);//容器中已经配置了UserDAOImpl了,并把DAOImpl注入到service之中,所以service直接就可以保存了
20     }
21 
22 }

 结果:

代码链接: http://pan.baidu.com/s/1jHMzY30 密码: vp9w

posted @ 2017-05-19 11:47  流年如水烟雨随风  阅读(1409)  评论(0编辑  收藏  举报