如果我们的系统不是分布式的(在分布式里,我一般自己加载spring的配置文件),不是一般的application,通过自己加载Spring的配置文件的方式。而是一般的web应用,我们通过在web.xml里配置spring的配置文件。我们怎么方便的得到一个Bean的实例呢?当然,web应用启动后,它已经创建好一个WebApplicationContext(这个是接口,其实也是ApplicationContext类型的,因为WebApplicationContext继承自ApplictionContext这个接口)类型的实例对象,通过org.springframework.web.context.support.WebApplicationContextUtils里的
getWebApplicationContext(ServletContext sc)可以得到这个对象的引用(这个就像我们一般的java application下得到ApplicationContext类型的引用一样),我们就可以通过它的getBean方法得到我们的bean实例了。但是这里有个问题getWebApplicationContext(ServletContext sc)这个方法的参数ServletContext代表的是你web应用的环境,也就是说,也就是说web应用环境下特有的。这个时候如果你想得到一个bean的话,必须要有这个ServletContext对象存在,如果你每个类里都写一个方法来接受ServletContext对象,从而得到WebApplicationContext类型实例的引用,之后再得到bean,进行你要的操作,这个是不是很麻烦?这个不是要写很多代码么?我觉得可以把获得bean的这个操作的功能代码放在一个Servlet里,让这个Servlet在web应用启动的时候加载,我们之后把这个Servlet当作普通类使用,调用里面的getBean方法就可以了。这个servlet的代码如下:
package
jimmee.cn.edu.zju.pdm.framework.server;

import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
org.springframework.web.context.WebApplicationContext;
import
org.springframework.web.context.support.WebApplicationContextUtils;
public
class
GetBeanServlet
extends
HttpServlet
...
{
private static WebApplicationContext context;
public void init() throws ServletException
...{
context=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
}
public static Object getBean(String id)
...{
Object bean = context.getBean(id);
return bean;
}
}
.Spring在web应用
<?
xml version
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
<
web
-
app version
=
"
2.5
"
xmlns
=
"
http://java.sun.com/xml/ns/javaee
"
xmlns:xsi
=
"
http://www.w3.org/2001/XMLSchema-instance
"
xsi:schemaLocation
=
"
http://java.sun.com/xml/ns/javaee
http:
//
java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!--
Spring的配置
-->
<
context
-
param
>
<
param
-
name
>
contextConfigLocation
</
param
-
name
>
<
param
-
value
>/
WEB
-
INF
/
beans.xml
</
param
-
value
>
</
context
-
param
>
<
context
-
param
>
<
param
-
name
>
log4jConfigLocation
</
param
-
name
>
<
param
-
value
>/
WEB
-
INF
/
log4j.properties
</
param
-
value
>
</
context
-
param
>
<
servlet
>
<
servlet
-
name
>
springInitServlet
</
servlet
-
name
>
<
servlet
-
class
>
org.springframework.web.context.ContextLoaderServlet
</
servlet
-
class
>
<
load
-
on
-
startup
>
1
</
load
-
on
-
startup
>
</
servlet
>
<
servlet
>
<
servlet
-
name
>
log4jInitServlet
</
servlet
-
name
>
<
servlet
-
class
>
org.springframework.web.util.Log4jConfigServlet
</
servlet
-
class
>
<
load
-
on
-
startup
>
2
</
load
-
on
-
startup
>
</
servlet
>
<
servlet
>
<
servlet
-
name
>
GetBeanServlet
</
servlet
-
name
>
<
servlet
-
class
>
jimmee.cn.edu.zju.pdm.framework.server.GetBeanServlet
</
servlet
-
class
>
<
load
-
on
-
startup
>
3
</
load
-
on
-
startup
>
</
servlet
>
<!--
Spring配置结束
-->
<
servlet
-
mapping
>
<
servlet
-
name
>
GetBeanServlet
</
servlet
-
name
>
<
url
-
pattern
>/
servlet
/
GetBeanServlet
</
url
-
pattern
>
</
servlet
-
mapping
>
<
welcome
-
file
-
list
>
<
welcome
-
file
>
index.jsp
</
welcome
-
file
>
</
welcome
-
file
-
list
>
</
web
-
app
>
使用示例:
假使我有一个Person的类,属性有name和age,在spring配置文件里配置的id为“person”
你在你的servlet或者jsp想得到这个实例的时候,直接这么做就可以了:
Person person=(Person)GetBeanServlet.getBean("person");
本文介绍如何在Web应用中便捷地获取Spring管理的Bean实例。通过在web.xml中配置Spring上下文,并利用自定义Servlet(GetBeanServlet)简化Bean实例的获取过程。

1631

被折叠的 条评论
为什么被折叠?



