程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 如何在Java Filter 中注入 Service,javafilter

如何在Java Filter 中注入 Service,javafilter

編輯:JAVA綜合教程

如何在Java Filter 中注入 Service,javafilter


在項目中遇到一個問題,在 Filter中注入 Serivce失敗,注入的service始終為null。如下所示:

復制代碼
public class WeiXinFilter implements Filter{
    
    @Autowired
    private UsersService usersService;

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse resp = (HttpServletResponse)response;
     Users users = this.usersService.queryByOpenid(openid);
}
復制代碼

上面的 usersService 會報空指針異常。

解決方法一

復制代碼
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse resp = (HttpServletResponse)response;
        ServletContext sc = req.getSession().getServletContext();
        XmlWebApplicationContext cxt = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(sc);
        
        if(cxt != null && cxt.getBean("usersService") != null && usersService == null)
            usersService = (UsersService) cxt.getBean("usersService");
        
        Users users = this.usersService.queryByOpenid(openid);
復制代碼

這樣就行了。

方法二

復制代碼
public class WeiXinFilter implements Filter{
    
    private UsersService usersService;
    
    public void init(FilterConfig fConfig) throws ServletException {
        ServletContext sc = fConfig.getServletContext(); 
        XmlWebApplicationContext cxt = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(sc);
        
        if(cxt != null && cxt.getBean("usersService") != null && usersService == null)
            usersService = (UsersService) cxt.getBean("usersService");        
    }
復制代碼

相關原理:

1. 如何獲取 ServletContext

1)在javax.servlet.Filter中直接獲取
ServletContext context = config.getServletContext();

2)在HttpServlet中直接獲取
this.getServletContext()

3)在其他方法中,通過HttpServletRequest獲得
request.getSession().getServletContext();

2. WebApplicationContext 與 ServletContext (轉自:http://blessht.iteye.com/blog/2121845):

Spring的 ContextLoaderListener是一個實現了ServletContextListener接口的監聽器,在啟動項目時會觸發contextInitialized方法(該方法主要完成ApplicationContext對象的創建),在關閉項目時會觸發contextDestroyed方法(該方法會執行ApplicationContext清理操作)。

ConextLoaderListener加載Spring上下文的過程

①啟動項目時觸發contextInitialized方法,該方法就做一件事:通過父類contextLoader的initWebApplicationContext方法創建Spring上下文對象。

②initWebApplicationContext方法做了三件事:創建 WebApplicationContext;加載對應的Spring文件創建裡面的Bean實例;將WebApplicationContext放入 ServletContext(就是Java Web的全局變量)中

③createWebApplicationContext創建上下文對象,支持用戶自定義的上下文對象,但必須繼承自ConfigurableWebApplicationContext,而Spring MVC默認使用ConfigurableWebApplicationContext作為ApplicationContext(它僅僅是一個接口)的實 現。

④configureAndRefreshWebApplicationContext方法用 於封裝ApplicationContext數據並且初始化所有相關Bean對象。它會從web.xml中讀取名為 contextConfigLocation的配置,這就是spring xml數據源設置,然後放到ApplicationContext中,最後調用傳說中的refresh方法執行所有Java對象的創建。

⑤完成ApplicationContext創建之後就是將其放入ServletContext中,注意它存儲的key值常量。

方法三

直接使用spring mvc中的HandlerInterceptor或者HandlerInterceptorAdapter 來替換Filter:

復制代碼
public class WeiXinInterceptor implements HandlerInterceptor {
    @Autowired
    private UsersService usersService;   
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
            throws Exception {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // TODO Auto-generated method stub
        
    }
}
復制代碼

配置攔截路徑:

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean class="net.xxxx.interceptor.WeiXinInterceptor" />
        </mvc:interceptor>     
    </mvc:interceptors>

 

Filter 中注入 Service 的示例:

復制代碼
public class WeiXinFilter implements Filter{    
    private UsersService usersService;    
    public void init(FilterConfig fConfig) throws ServletException {}
    public WeiXinFilter() {}
    public void destroy() {}

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse resp = (HttpServletResponse)response;
        
        String userAgent = req.getHeader("user-agent");
if(userAgent != null && userAgent.toLowerCase().indexOf("micromessenger") != -1){ // 微信浏覽器 String servletPath = req.getServletPath(); String requestURL = req.getRequestURL().toString(); String queryString = req.getQueryString(); if(queryString != null){ if(requestURL.indexOf("mtzs.html") !=-1 && queryString.indexOf("LLFlag")!=-1){ req.getSession().setAttribute("LLFlag", "1"); chain.doFilter(request, response); return; } } String openidDES = CookieUtil.getValueByName("openid", req); String openid = null; if(StringUtils.isNotBlank(openidDES)){ try { openid = DesUtil.decrypt(openidDES, "rxxxxxxxxxde"); // 解密獲得openid } catch (Exception e) { e.printStackTrace(); } } // ... ... String[] pathArray = {"/weixin/enterAppFromWeiXin.json", "/weixin/getWeiXinUserInfo.json", "/weixin/getAccessTokenAndOpenid.json", "/sendRegCode.json", "/register.json", "/login.json", "/logon.json", "/dump.json", "/queryInfo.json"}; List<String> pathList = Arrays.asList(pathArray); String loginSuccessUrl = req.getParameter("path"); String fullLoginSuccessUrl = "http://www.axxxxxxx.cn/pc/"; if(requestURL.indexOf("weixin_gate.html") != -1){ req.getSession().setAttribute("loginSuccessUrl", loginSuccessUrl);
          // ... ... }
        ServletContext sc = req.getSession().getServletContext();
XmlWebApplicationContext cxt = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(sc);
        
         if(cxt != null && cxt.getBean("usersService") != null && usersService == null)
            usersService = (UsersService) cxt.getBean("usersService");
        
         Users users = this.usersService.queryByOpenid(openid); // ... ... if(pathList.contains(servletPath)){ // pathList 中的訪問路徑直接 pass chain.doFilter(request, response); return; }else{ if(req.getSession().getAttribute(CommonConstants.SESSION_KEY_USER) == null){ // 未登錄 String llFlag = (String) req.getSession().getAttribute("LLFlag"); if(llFlag != null && llFlag.equals("1")){ // 處理游客浏覽 chain.doFilter(request, response); return; } // ... ...// 3. 從騰訊服務器去獲得微信的 openid , req.getRequestDispatcher("/weixin_gate.html").forward(request, response); return; }else{ // 已經登錄 // 4. 已經登錄時的處理 chain.doFilter(request, response); return; } } }else{ // 非微信浏覽器 chain.doFilter(request, response); } } }
復制代碼

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved