---
title: "简单易懂的spring例子"
author: "ldb2741"
date: "2009-08-26T19:57:00Z"
tags: []
slug: "spring"
draft: false
lastmod: "2009-08-26T19:57:00Z"
id: 580730
url: "https://ldb2741.iteye.com/blog/580730"
---

test.java

package my;  
public class test {

private String helloWord = "Hello!World!";

public void setHelloWord(String helloWord) {  
this.helloWord = helloWord;  
}

public String getHelloWord() {  
return helloWord;  
}

}

web.xml

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;web-app id="WebApp\_ID" version="2.4"  
xmlns="[http://java.sun.com/xml/ns/j2ee](http://java.sun.com/xml/ns/j2ee)" xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
xsi:schemaLocation="[http://java.sun.com/xml/ns/j2ee](http://java.sun.com/xml/ns/j2ee) [http://java.sun.com/xml/ns/j2ee/web-app\_2\_4.xsd](http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd)"&gt;  
&lt;display-name&gt;pro1&lt;/display-name&gt;  
&lt;welcome-file-list&gt;  
&lt;welcome-file&gt;index.html&lt;/welcome-file&gt;  
&lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;  
&lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;  
&lt;welcome-file&gt;default.html&lt;/welcome-file&gt;  
&lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;  
&lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;  
&lt;/welcome-file-list&gt;

&lt;context-param&gt;  
&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;  
&lt;!-- 配置加载的bean factory xml --&gt;  
&lt;param-value&gt;  
WEB-INF/bean.xml  
&lt;/param-value&gt;  
&lt;/context-param&gt;  
&lt;listener&gt;  
&lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;  
&lt;/listener&gt;

&lt;/web-app&gt;

bean.xml

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "[http://www.springframework.org/dtd/spring-beans.dtd](http://www.springframework.org/dtd/spring-beans.dtd)"&gt;  
&lt;beans&gt;  
&lt;bean id="helloBean" class="my.test"&gt;  
&lt;property name="helloWord"&gt;  
&lt;value&gt;successful!&lt;/value&gt;

&lt;/property&gt;  
&lt;/bean&gt;  
&lt;/beans&gt;

index.jsp

&lt;%@ page language="java" contentType="text/html; charset=UTF-8"  
pageEncoding="UTF-8"%&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
"[http://www.w3.org/TR/html4/loose.dtd](http://www.w3.org/TR/html4/loose.dtd)"&gt;  
&lt;%@ page import="my.test"%&gt;  
&lt;[%@page](mailto:%@page) import="org.springframework.beans.factory.BeanFactory"%&gt;  
&lt;[%@page](mailto:%@page) import="org.springframework.web.context.WebApplicationContext"%&gt;&lt;html&gt;  
&lt;head&gt;  
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;  
&lt;title&gt;Insert title here&lt;/title&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;%  
BeanFactory beanFactory = (BeanFactory) pageContext  
.getServletContext()  
.getAttribute(  
WebApplicationContext.ROOT\_WEB\_APPLICATION\_CONTEXT\_ATTRIBUTE);  
test hello = (test) beanFactory.getBean("helloBean");  
out.print(hello.getHelloWord());  
%&gt;

&lt;/body&gt;  
&lt;/html&gt;