Spring 专题
专题目录
您的位置:java > Spring专题 > Spring hello world实例
Spring hello world实例
作者:--    发布时间:2019-11-20


本教程介绍如何在spring3 中创建一个简单的 hello world 例子。
在这篇文章中使用的技术或工具:
  1. spring 3.1
  2. myeclipse 10
  3. jdk 1.6
提示: spring 3 至少jdk1.5才能正常工作。
spring 3.0 dependencies
在spring2.5.x中,几乎整个spring模块分组在一个单独的 spring.jar 文件中。由于spring3中每模块被分成一个单独的 jar 文件,例如,spring-core, spring-expression, spring-context, spring-aop等

1. 创建一个java工程

打开 myeclipse 创建一个java工程:helloword,并添加spring支持类库,右键工程名称,选择"myeclipse"->"add spring capabilites",如下图:

2. spring bean

一个简单的 spring bean.

package com.h3.core;

/**
 * spring bean
 * 
 */
public class helloworld {
	private string name;

	public void setname(string name) {
		this.name = name;
	}

	public void printhello() {
		system.out.println("spring 3 : hello ! " + name);
	}
}

4. spring bean 配置文件

创建spring配置文件,并声明所有可用的spring bean。

file : applicationcontext.xml

<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-3.0.xsd">

	<bean id="hellobean" class="com.h3.core.helloworld">
		<property name="name" value="h3" />
	</bean>

</beans>

5. 项目结构

查看目录结构如下:

6. 执行代码

package com.h3.core;

import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;

public class app {
	public static void main(string[] args) {
		applicationcontext context = new classpathxmlapplicationcontext(
				"applicationcontext.xml"); helloworld obj = (helloworld) context.getbean("hellobean");
		obj.printhello();
	}
}

7. 输出结果

spring 3 : hello ! h3

下载源代码

http://pan.baidu.com/s/1qwzqopm



网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册