一,开发环境
工具:eclipse
Spring:4.3.1
Commons-logging:1.1.1
二,建立工程
a,新建动态web工程:file-new-dynamic web project
b,复制spring的jar包到工程:
jar包位置:spring-framework-4.3.1.RELEASE\libs,下的所有jar包
拷贝至:项目的WebContent/WEB-INF/lib
c,复制Commons-logging的jar包到工程
jar包位置:commons-logging-1.1.1目录下,只需commons-logging-1.1.1.jar一个即可
拷贝至:项目的WebContent/WEB-INF/lib
d,至此环境已建立完成
三,测试spring环境
a,建立测试bean
1,新建包org.com.xsx.beans
2,在包下新建class:User.java
package org.com.xsx.beans;
public class UserBean {
private int id;
private String account;
private String password;
private String name;
private String sex;
private int age;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
3,在src下新建spring配置文件,右键-new-others-Spring-Spring Bean Configuration File,并配置一个id为TestUser的bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<bean id="test_user" class="org.com.xsx.beans.UserBean">
<property name="account" value="xsx"></property>
<property name="password" value="xsx127"></property>
</bean>
</beans>
4,新建包org.com.xsx.Test,并新建Test.java,进行Units测试
package org.com.xsx.unit;
import org.com.xsx.beans.UserBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestUnits {
private ApplicationContext ctx = null;
private UserBean user = null;
{
ctx = new ClassPathXmlApplicationContext("ApplicationContextBean.xml");
user = ctx.getBean(UserBean.class);
}
@Test
public void test1(){
System.out.println("账号:"+user.getAccount());
System.out.println("密码:"+user.getPassword());
}
}
5,右键ran as--JUnit Test,没报错,即证明spring环境建立完成,成功则显示一下信息
八月 13, 2016 6:22:55 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@61baa894: startup date [Sat Aug 13 18:22:55 CST 2016]; root of context hierarchy
八月 13, 2016 6:22:55 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [ApplicationContextBean.xml]
账号:xsx
密码:xsx127