filter注入service報空指標
阿新 • • 發佈:2019-01-08
其實Spring中,web應用啟動的順序是:listener->filter->servlet,先初始化listener,然後再來就filter的初始化,再接著才到我們的dispathServlet的初始化,因此,當我們需要在filter裡注入一個註解的bean時,就會注入失敗,因為filter初始化時,註解的bean還沒初始化,沒法注入。
public class AuthenticationTokenFilter implements Filter {
private JedisUtil.Strings jedisStrings;
@Override
public void init(FilterConfig fConfig) throws ServletException {
ServletContext sc = fConfig.getServletContext();
XmlWebApplicationContext cxt = (XmlWebApplicationContext)
WebApplicationContextUtils.getWebApplicationContext(sc);
if (cxt != null && cxt.getBean ("jedisStrings") != null && jedisStrings == null)
jedisStrings = (JedisUtil.Strings) cxt.getBean("jedisStrings");
}
...
...
}