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 }