1) 工具: (1) jdk
(2) spring.jar 、commons-logging-1.1.1.jar (因为只是做的简单的demo,所以就只用这两个jar包)
spring.jar 是包含有完整发布模块的单个jar 包。但是不包括mock.jar, aspects.jar, spring-portlet.jar, and spring-hibernate2.jar。
commons-logging.jar 包是使用spring的必备包。用来记录程序运行时的活动的日志记录。
在IDE中导入jar后就可以开始编写测试了
2) 在src下写配置文件
相关文件如下:spring-demo.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-2.0.xsd"> <bean id="testBean" class="com.gdut.springdemo.Spring"> <property name="mySpring"> <value>This is my first spring!</value> </property> </bean> </beans>
测试类:Spring.java
package com.gdut.springdemo; public class Spring { private String mySpring; public void show(){ System.out.println("--message--"+getMySpring()); } public String getMySpring() { return mySpring; } public void setMySpring(String mySpring) { this.mySpring = mySpring; } }
TestSpring.java
package com.gdut.springdemo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class TestSpring { public static void main(String[] args) { ApplicationContext ctx=new FileSystemXmlApplicationContext("src/spring-demo.xml"); Spring spring=(Spring) ctx.getBean("testBean"); spring.show(); } }
控制台打印: --message--This is my first spring!