①在官网https://spring.io/下载或者http://maven.springframework.org/release/org/springframework/spring/下载spring开发包
②新建javaweb工程
③从刚才下载的spring开发包解压后的文件处导入所需要的spring包,也可以选择全部导入
④从http://commons.apache.org/proper/commons-logging/download_logging.cgi或者
http://www.java2s.com/Code/Jar/c/Downloadcommonslogging113jar.htm
下载commons-logging-1.1.3-jar.zip
⑤从下载的包里解压后选择commons-logging-1.1.3-jar导入javaweb项目
选择项目右键Build Path -----》configure Build Path -----》Liability ----》AddEAddExternal Jars ----》选择所需要的包
⑥编写测试类测试环境能否运行
在test包下新建接口testInter.java文件
package test;
public interface testInter {
public void setUsername(String string);
public void setPassword(String string);
public String getUsername();
public String getPassword();
}
在test包下新建testImpl.java文件
package test;
public class testImpl implements testInter {
private String Username;
private String Password;
@Override
public void setUsername(String string) {
// TODO Auto-generated method stub
this.Username = string;
}
@Override
public void setPassword(String string) {
// TODO Auto-generated method stub
this.Password = string;
}
@Override
public String getUsername() {
// TODO Auto-generated method stub
return Username;
}
@Override
public String getPassword() {
// TODO Auto-generated method stub
return Password;
}
}
在src目录下新建配置xml文件
编写配置文件
<?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.xsd">
//给bean设置唯一id(testBean)和class(test.testImpl)
<bean id="testBean" class="test.testImpl">
<property name="Username"> //给bean的属性username设置初始化值Charlie
<value>Charlie</value>
</property>
<property name="Password"> //给bean的属性Password设置初始化值12345678
<value>12345678</value>
</property>
</bean>
</beans>
新建一个class文件testmain.java
package test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testmain {
public static void main(String[] args) {
// 建立一个容器
BeanFactory factory = new ClassPathXmlApplicationContext("tt.xml");
//从容器里通过getBean()与刚才设置好的Bean的id获取bean实例
testImpl tml = (testImpl)factory.getBean("testBean");
//使用实例的方法输出信息
System.out.println("我是"+tml.getUsername()+"\n"+"我的密码是"+tml.getPassword());
}
}
输出结果如下:
到此环境搭建及测试完毕。