1. 程式人生 > >在filter中獲取spring容器中的bean物件

在filter中獲取spring容器中的bean物件

在工作中的專案需要,要在filter中使用服務介面,但是採用傳統的new 和注入方式都是空指標異常,最後的解決方式是
@Override
public void init(FilterConfig config) throws ServletException {
ServletContext context = config.getServletContext();
ApplicationContext ac = WebApplicationContextUtils .getWebApplicationContext(context);
userService1 = (UserService)ac.getBean("userService");
}