一、准备需要的jar包:
核心jar包:下载的spring-framework-3.X.X.RELEASE-with-docs.zip中dist
依赖的jar包:下载的spring-framework-3.X.X.RELEASE-dependencies.zip
二、创建标准Java工程:
1.创建标准的java工程(JRE环境选择1.6);
2.将下载的jar包导入到工程中;
3.在src目录下创建一个xml文件,可命名为applicationContext.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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> </beans>
4.创建HelloWorld.java文件
public class HelloWorld { public void sayHello(){ System.out.println("hello world!!!"); } }
5.在xml中添加
<bean id="hello" class="chenhao.HelloWorld"></bean>
6.给工程添加junit3,然后写测试类Test
import junit.framework.TestCase; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import chenhao.HelloWorld; public class Test extends TestCase { private BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); public void hello(){ HelloWorld hello = (HelloWorld) factory.getBean("hello"); hello.sayHello(); } }
测试hello方法:
结果: hello world!!!
时间: 2024-10-08 13:28:49