Spring在Java Filter注入Bean为Null的问题解决
From: https://www.cnblogs.com/EasonJim/p/7666009.html
在Spring的自動(dòng)注入中普通的POJO類都可以使用@Autowired進(jìn)行自動(dòng)注入,但是除了兩類:Filter和Servlet無(wú)法使用自動(dòng)注入屬性。(因?yàn)檫@兩個(gè)歸Web容器管理)可以用init(集承自HttpServlet后重寫init方法)方法中實(shí)例化對(duì)象。
解決方法:
其中涉及到五種Spring實(shí)例化容器對(duì)象:
方法一(這種方式不符合Web工程,不要使用):在初始化時(shí)保存ApplicationContext對(duì)象
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean("beanId");說(shuō)明:這種方式適用于采用Spring框架的獨(dú)立應(yīng)用程序,需要程序通過(guò)配置文件手工初始化Spring的情況。
方法二(這種方式最簡(jiǎn)單):通過(guò)Spring提供的工具類獲取ApplicationContext對(duì)象
import org.springframework.web.context.support.WebApplicationContextUtils;ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc); ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc); ac1.getBean("beanId"); ac2.getBean("beanId");說(shuō)明:這種方式適合于采用Spring框架的B/S系統(tǒng),通過(guò)ServletContext對(duì)象獲取ApplicationContext對(duì)象,然后在通過(guò)它獲取需要的類實(shí)例。上面兩個(gè)工具方式的區(qū)別是,前者在獲取失敗時(shí)拋出異常,后者返回null。
實(shí)例:
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"); }注意:如果在Spring Boot項(xiàng)目上XmlWebApplicationContext可以不用要,直接使用WebApplicationContext替代。
方法三:繼承自抽象類ApplicationObjectSupport
說(shuō)明:抽象類ApplicationObjectSupport提供getApplicationContext()方法,可以方便的獲取到ApplicationContext。
Spring初始化時(shí),會(huì)通過(guò)該抽象類的setApplicationContext(ApplicationContext context)方法將ApplicationContext對(duì)象注入。
方法四:繼承自抽象類WebApplicationObjectSupport
說(shuō)明:類似上面方法,調(diào)用getWebApplicationContext()獲取WebApplicationContext
方法五:實(shí)現(xiàn)接口ApplicationContextAware
說(shuō)明:實(shí)現(xiàn)該接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 對(duì)象。Spring初始化時(shí),會(huì)通過(guò)該方法將ApplicationContext對(duì)象注入。
?
參考:
http://blog.csdn.net/angel708884645/article/details/51148865
http://www.cnblogs.com/digdeep/p/4770004.html
==>如有問(wèn)題,請(qǐng)聯(lián)系我:easonjim#163.com,或者下方發(fā)表評(píng)論。<==
總結(jié)
以上是生活随笔為你收集整理的Spring在Java Filter注入Bean为Null的问题解决的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: fgui的ui管理框架_FGUI,UGU
- 下一篇: Java中遍历HashMap的5种方式