webapplicationContext是spring的核心context,所有的bean在初始化后都会在里面,当项目被加载,applicationContext.xml已经被加载了一次,如果此时用new ClassPathXmlApplicationContext.getBean("applicationContext.xml")的方式获取bean对象,applicationContext就相当于被加载了两次,所以就会起冲突,如果要在程序中获取webapplicationContext有以下几种方式。
一 通过webapplicationContextUtils.getRequiredWebApplicationContext(servletContext);
import javax.servlet.ServletContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public final class WebAppUtils {
private static volatile WebApplicationContext wac;
private static volatile ServletContext servletContext;
public static void setServletContext(ServletContext servletContext) {
if (WebAppUtils.servletContext == null)
synchronized (WebAppUtils.class) {
if (WebAppUtils.servletContext == null)
WebAppUtils.servletContext = servletContext;
}
}
public static ServletContext getServletContext() {
return servletContext;
}
public static WebApplicationContext getInstance() {
if (wac == null) {
synchronized (WebAppUtils.class) {
if ((wac == null) && (servletContext != null)) {
wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
}
}
}
return wac;
}
}
这里需要获取servletContext,获取servletContext有两种方法
1.直接写一个filter,当filter在初始化的时候调用init()方法的时候,就会把servletConfig传进去,然后可以使用filterConfig.getServletContext();获取到servletContext 并设置到webapplication里面去就好了,代码如下。
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import com.nsu.pmes.utils.cache.memory.WebAppUtils;
/**
* 过滤器获取servletContext 用户获取webapplicationContext容器获取bean对象
* @author lixin
*
*/
public class DelegatingVisitFilterProxy implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
ServletContext servletConfig= filterConfig.getServletContext();
WebAppUtils.setServletContext(servletConfig);
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(request, response);
}
public void destroy() {
}
}
2.还有一种获取servletContext的方法比较简单,
直接通过 servletContext SC=ContextLoader.getCurrentWebApplicationContext().getServletContext();可以获取到
3.用Httprequest对象获取到
servletContext SC=request.getsession().getServletContext
二 借用ApplicationContextAware,ApplicationContext的帮助类能够自动装载ApplicationContext,只要你将某个类实现这个接口,并将这个实现类在Spring配置文件中进行配置,Spring会自动帮你进行注入 ApplicationContext.ApplicationContextAware的代码结构如下:
1 public interface ApplicationContextAware { 2 3 void setApplicationContext(ApplicationContext applicationContext) throws BeansException; 4 5 }
就这一个接口。可以这样简单的实现一个ApplicationContextHelper类:
1 public class ApplicationHelper implements ApplicationContextAware { 2 3 4 private ApplicationContext applicationContext; 5 6 public void setApplicationContext(ApplicationContext applicationContext) 7 throws BeansException { 8 this.applicationContext = applicationContext; 9 } 10 11 12 public ApplicationContext getApplicationContext(){ 13 return this.applicationContext; 14 } 15 }