Spring web应用下怎么方便的获得bean?
来源:互联网 发布:排队叫号管理系统源码 编辑:程序博客网 时间:2024/04/28 22:37
如果我们的系统不是分布式的(在分布式里,我一般自己加载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的代码如下:
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应用
<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");
- Spring web应用下怎么方便的获得bean?
- Spring web应用下怎么方便的获得bean?
- Spring web应用下方便的获得bean(转)
- Spring在web应用中获得Bean的方法
- Spring在web应用中获得Bean的方法
- Spring在web应用中获得Bean的方法 实现getBean方法
- Spring在web应用中获得Bean的方法 实现getBean方法
- spring bean的获得方式
- web应用中spring 获取bean的方式
- Spring WebApplicationContext当web容器已经启动时获得当前线程的bean
- Spring中获得bean的方法
- Spring中获得bean的方法
- Spring中获得bean的方法
- 获得spring初始化的bean和ServletContext
- 获得Spring Bean的方法(Z)
- Spring中获得bean的方法
- Spring中获得bean的方法
- spring 几种获得bean的方法
- 如何确定网站的定位
- 给作业加个数字签名
- linux的文件系统
- 实例:用Servlet开发和配置过滤器(字符集设置)
- wxWidgets学习日记
- Spring web应用下怎么方便的获得bean?
- vbs的两个源码
- 《奋斗》人物的性格分析
- 串行接口SPI接口应用设计
- vi显示中文的设置
- 使用AJAX实时显示数据
- select操作
- 简单日期处理
- java 学习步骤2