web项目的servlet和filter中获取spring上下文
2013-07-19 11:30
465 查看
之前一直是在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
Java代码
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
//实例化一个ContextLoader,然后调用它的initWebApplicationContext方法
Java代码
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
protected ContextLoader createContextLoader() {
return new ContextLoader();
}
or ContextLoaderServlet。它的大概意思就是这个方法通过ContextLoaderListener获取WebApplicationContext。所以在servlet中就可以通过如下方法实现获取spring上下文
Java代码
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());
}
}
使用方法
Java代码
public void test(){
TestBean testBean=(TestBean)this.getApplicationContext().getBean("testBean");
}
public ApplicationContext getApplicationContext() {
return WebApplicationContextUtils.getWebApplicationContext(filterConfig.getServletContext());
}
转自:http://blog.csdn.net/zhiweiv/archive/2008/10/21/3118857.aspx
在spring中的org.springframework.beans.factory.BeanFactory接口getBean(String id)用来获取spring管理的bean。而我们一般使用实现了该接口的ApplicationContext接口。所以只要我们在servlet中取得了ApplicationContext接口的实现类,就可以获取spring管理的bean了。
首先是在web.xml中配置listener
Java代码
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>这个类的核心代码如下
//实例化一个ContextLoader,然后调用它的initWebApplicationContext方法
Java代码
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
protected ContextLoader createContextLoader() {
return new ContextLoader();
}
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上下文
Java代码
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());
}
}
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()); } }
使用方法
Java代码
public void test(){
TestBean testBean=(TestBean)this.getApplicationContext().getBean("testBean");
}
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
相关文章推荐
- 在普通web项目的servlet和filter中获取spring上下文
- 在maven项目中使用apache cxf中遇到异常 java.lang.ClassCastException: org.springframework.web.filter.CharacterEncodingFilter cannot be cast to javax.servlet.Filter
- 在web项目中获取spring上下文
- Web项目中获取SpringBean——在非Spring组件中获取SpringBean
- Web项目中获取SpringBean——在非Spring组件中获取SpringBean
- web.xml中servlet, spring, filter, listenr的配置
- IDEA中Maven项目的web.xml中报错:cannot resolve servlet 'spring-mvc'
- Servlet之ServletContext获取获取web项目的资源文件
- Spring Boot - Web 应用开发 - Servlet, Filter, Listener
- IDEA启动Spring Boot项目提示Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServlet...
- Web项目中获取SpringBean——在非Spring组件中获取SpringBean
- web项目中获取spring的bean对象
- web项目之BBS流程解析(无servlet、filter)
- Web项目中获取SpringBean——在非Spring组件中获取SpringBean
- Servlet内获取spring上下文环境
- Servlet之ServletContext获取web上下文路径、全局参数、和Attribute(域)
- maven org.springframework.web.filter.CharacterEncodingFilter cannot be cast to javax.servlet.Filter
- Web项目中web.xml 中的listener、 filter、servlet 加载顺序
- SpringMVC Maven项目 java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServle
- SpringMVC Maven项目 java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServle