首页 > 编程语言 > 详细

Spring学习(一)开始Spring Hello World之旅

时间:2014-12-06 12:53:56      阅读:302      评论:0      收藏:0      [点我收藏+]

一、准备需要的jar包:

核心jar包:下载的spring-framework-3.X.X.RELEASE-with-docs.zipdist
依赖的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!!!


Spring学习(一)开始Spring Hello World之旅

原文:http://my.oschina.net/chenhao901007/blog/353049

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!