之前一直是在web项目中使用struts2然后通过plugin集成spring,action生成的细节全部由plugin实现了,对于我们是透明的。过几天学校留个作业只能用普通的jsp+servlet做,之前一直是透明的使用spring,对spring的初始化及bean的获取一直没什么概念。这回正好用的上,就研究了一下ContextLoaderListener ContextLoader和StrutsSpringObjectFactory的源码。经过一番阅读对spring的初始化和获取bean有了一些印象。
在spring中的org.springframework.beans.factory.BeanFactory接口getBean(String id)用来获取spring管理的bean。而我们一般使用实现了该接口的ApplicationContext接口。所以只要我们在servlet中取得了ApplicationContext接口的实现类,就可以获取spring管理的bean了。
首先是在web.xml中配置listener
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
这个类的核心代码如下
//实例化一个ContextLoader,然后调用它的initWebApplicationContext方法
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
protected ContextLoader createContextLoader() {
return new ContextLoader();
}
ContextLoader这个类内的代码就不写了,看不太明白。然后一个必须的类是WebApplicationContextUtils,该类有一个方法getWebApplicationContext(ServletContext servletContext),这个方法的官方解释是: Find the root WebApplicationContext for this web application, which is typically loaded via ContextLoaderListener or ContextLoaderServlet。它的大概意思就是这个方法通过ContextLoaderListener获取WebApplicationContext。所以在servlet中就可以通过如下方法实现获取spring上下文
import javax.servlet.http.HttpServlet;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class NewServlet extends HttpServlet {
public ApplicationContext getApplicationContext(){
return WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
}
}
使用方法
public void test(){
TestBean testBean=(TestBean)this.getApplicationContext().getBean("testBean");
}
在filter里面server会给它注入一个FilterConfig对象,在filter里可以使用FilterConfig的filterConfig.getServletContext()方法获取servlet上下文,所以在filter里获取spring的上下文方法如下
public ApplicationContext getApplicationContext() {
return WebApplicationContextUtils.getWebApplicationContext(filterConfig.getServletContext());
}
转自:http://blog.csdn.net/zhiweiv/archive/2008/10/21/3118857.aspx
分享到:
相关推荐
Spring 管理filter 和servlet
一、Web项目中使用Spring ...在 Web 项目中使用 Spring 框架,首先要解决在 web 层(这里指 Servlet)中获取到 Spring容器的问题。只要在 web 层获取到了 Spring 容器,便可从容器中获取到 Service 对象
java web项目 servlet+jsp实现的网上在线报名系统 java web项目 servlet+jsp实现的网上在线报名系统 java web项目 servlet+jsp实现的网上在线报名系统 java web项目 servlet+jsp实现的网上在线报名系统 java web项目...
org.springframework.web.servlet-3.0.0.M4.jar
java servlet spring web filter listenr
赠送jar包:sentinel-web-servlet-1.8.0.jar; 赠送原API文档:sentinel-web-servlet-1.8.0-javadoc.jar...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。
Error creating bean with name 'org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0' defined in ServletContext resource [/WEB-INF/springMVC-servlet.xml]: Initialization of bean failed;...
《Java Web整合开发王者归来(JSP+Servlet+Struts+Hibernate+Spring)》附1张DVD光盘,内容为《Java Web整合开发王者归来(JSP+Servlet+Struts+Hibernate+Spring)》汲及的源代码和Java Web学习视频。 《Java Web整合...
jsp\servlet\jdbc\oracle四者的整合套路,增删查改(CRUD)的具体逻辑业务
Spring4 In Action-7.1.2-添加其他的Servlet和Filter,Spring4 In Action-7.1.2-添加其他的Servlet和Filter,Spring4 In Action-7.1.2-添加其他的Servlet和Filter
Servlet JSP和Spring MVC初学指南
Spring的注入在Servlet中使用:在Servlet中使用Spring注入的信息,需要WebApplicationContext这个专门为Web准备的应用上下文
Java.Web开发 JSP Servlet. Struts Spring Hibernate Ajax
org.springframework.web.servlet-2.5.6.SEC01.jar
Servlet上下文和侦听器
由于marven仓库中不存在此jar包 因此特地提供下载
org.springframework.web.servlet-3.1.0.RELEASE.jar.zip
是源代码,不是jar文件!!!!! /** * Holder for both Model and View in the web MVC framework. * Note that these are entirely ... * @see org.springframework.web.servlet.mvc.Controller#handleRequest */
org.springframework.web.servlet-3.1.0.M2.jar