spring2.5.6开发环境搭建的过程,供大家参考,具体内容如下
1、jar 包准备:
spring 2.5.6 的 jar 包(链接: https://pan.baidu.com/s/1gehm6d9 密码: himj),如图:
commons-logging-1.2(链接: 下载地址),如图:
junit 4.7(链接: ),如图:
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.bjsxt.model;
public class user {
private string username;
private string password;
public string getusername() {
return username;
}
public void setusername(string username) {
this .username = username;
}
public string getpassword() {
return password;
}
public void setpassword(string password) {
this .password = password;
}
}
|
userdao
1
2
3
4
5
6
7
|
package com.bjsxt.dao;
import com.bjsxt.model.user;
public interface userdao {
public void save(user u);
}
|
userdaoimpl
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.bjsxt.dao.impl;
import com.bjsxt.dao.userdao;
import com.bjsxt.model.user;
public class userdaoimpl implements userdao{
public void save(user u) {
system.out.println( "a user saved!" );
}
}
|
userservice
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.bjsxt.service;
import com.bjsxt.dao.userdao;
import com.bjsxt.model.user;
public class userservice {
private userdao userdao;
public void adduser(user u){
this .userdao.save(u);
}
public userdao getuserdao() {
return userdao;
}
public void setuserdao(userdao userdao) {
this .userdao = userdao;
}
}
|
4、编写与上面类对应的配置文件 beans.xml(名字随便取)
beans.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?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-2.5.xsd">
<bean id= "u" class = "com.bjsxt.dao.impl.userdaoimpl" ></bean>
<bean id= "userservice" class = "com.bjsxt.service.userservice" >
<property name= "userdao" >
<ref bean= "u" />
</property>
</bean>
</beans>
|
5、编写 spring 测试类:
userservicetest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.bjsxt.service;
import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
import com.bjsxt.model.user;
public class userservicetest {
@test
public void testadduser() throws exception {
applicationcontext ctx = new classpathxmlapplicationcontext( "beans.xml" ); //初始化applicationcontext对象,加载配置文件beans.xml
userservice service = (userservice)ctx.getbean( "userservice" ); //从容器中取
user u = new user();
u.setusername( "zhangsan" );
u.setpassword( "zhangsan" );
service.adduser(u); //容器中已经配置了userdaoimpl了,并把daoimpl注入到service之中,所以service直接就可以保存了
}
}
|
结果:
代码链接: spring 2.5.6 框架的搭建.zip
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。