麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

如何在Java Filter 中注入 Service

2019-11-15 01:18:52
字體:
來源:轉載
供稿:網友
如何在java Filter 中注入 Service

在項目中遇到一個問題,在 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.VEvb.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值常量。

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);        }    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日韩黄色片网站 | 国产精品成人一区二区三区电影毛片 | 久久精品99北条麻妃 | 精品久久久久久久久久久久久久 | 在线看免费观看av | 爱性久久久久久久 | 在线看成人av | 成年免费看| 9797色| av免费在线观看av | 欧美日韩视频第一页 | 麻豆视频观看 | 99亚洲视频 | 美女视频大全网站免费 | 超级av在线 | 国产毛片aaa一区二区三区视频 | 日日草视频 | 国产一区二区三区四区精 | 精品在线视频播放 | 成人午夜精品 | 国产免费午夜 | 日日狠狠久久偷偷四色综合免费 | 午夜视频在线免费播放 | 一起草av在线 | 91av国产在线 | 国产精品免费一区二区 | 中文字幕网在线 | 99re66热这里只有精品8 | 啊~用cao嗯力cao烂我视频 | 成人网在线观看 | 国产一区二区三区在线视频 | 久久国产精 | 午夜国产福利 | 国产精品久久二区 | 久久久久一本一区二区青青蜜月 | 精品午夜久久 | www69xxxxx| 91久久极品少妇韩国 | 精品xxxx户外露出视频 | 99999久久久久久 | 欧美精品久久久久久久久久 |