1. 程式人生 > >springboot整合filter之在filter中如何獲取spring容器中的bean物件

springboot整合filter之在filter中如何獲取spring容器中的bean物件

本人在專案使用的是springboot,具業務需要本人使用了filter過濾器,進行一個路徑攔截,本人配置的是攔截所有/*,然後對路徑中包含的特有欄位進行處理。

filter中需要使用service的bean物件,我使用@Autowired註解注入物件,然後啟動,訪問時報空指標異常,filter中獲取不到bean物件。

後面我發現spring容器初始化bean物件的順序是listener-->filter-->servlet,之後程式碼示範

package com.server.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.server.module.system.adminUser.AdminConstant;
import com.server.module.system.adminUser.AdminUserBean;
import com.server.module.system.adminUser.AdminUserService;
import com.server.module.system.adminUser.AdminUserServiceImp;
import com.server.module.system.logsManager.exportLog.ExportLogBean;
import com.server.module.system.logsManager.exportLog.ExportLogService;
import com.server.util.ReturnDataUtil;

public class ExportFilter implements Filter{
	public static Logger log = LogManager.getLogger(WebFilter.class);
	@Autowired
	private AdminUserService adminUserService;
	@Autowired
	private ExportLogService exportLogServiceImpl;
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
        //初始化filter時手動注入bean物件
		ServletContext context = filterConfig.getServletContext(); 
		ApplicationContext ac = WebApplicationContextUtils .getWebApplicationContext(context); 
		adminUserService = ac.getBean(AdminUserService.class); 
		exportLogServiceImpl=ac.getBean(ExportLogService.class);
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		log.info("=========<ExportFilter>==========<doFilter>========start");
		//只對匯出做調整
		HttpServletRequest req=(HttpServletRequest) request;
		//對請求路徑進行攔截
		boolean falg=req.getRequestURI().contains("export")||req.getRequestURI().contains("Export");
		if(req.getRequestURI().contains("exportLog")) {//不進行處理
			falg=false;
		}
		ExportLogBean bean=new ExportLogBean();
		if(falg) {
		//對bean的資料進行封裝
		Long userId = (Long) req.getAttribute(AdminConstant.LOGIN_USER_ID);
		ReturnDataUtil data = adminUserService.findUserById(userId);
		AdminUserBean loginUser = (AdminUserBean)data.getReturnObject();
		bean.setOperator(userId);//設定匯出人
		bean.setOperatorName(loginUser.getName());//設定匯出人姓名
		bean.setCompanyId((long)loginUser.getCompanyId());//設定匯出人公司
		req.setAttribute("exportBean", bean);
		}
		chain.doFilter(req, response);//放行
		if(falg) {
		//對資料進行處理
		exportLogServiceImpl.add(bean);
		}
		log.info("=========<ExportFilter>==========<doFilter>========end");
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
	
}