网站策划书背景介绍,wordpress标签页,淄博桓台网站建设方案,博客网站的建设有的时候根据我们业务的需要#xff0c;我们需要在web项目中定义一个自己的filter,并想在这个filter中使用Autowired注入bean供我们使用。如果直接使用的话是不行的#xff0c;需要我们在xml文件中进行配置。下面就根据我的一个项目写一个示例:步骤一、定义一个ClientSession… 有的时候根据我们业务的需要我们需要在web项目中定义一个自己的filter,并想在这个filter中使用Autowired注入bean供我们使用。如果直接使用的话是不行的需要我们在xml文件中进行配置。下面就根据我的一个项目写一个示例: 步骤一、定义一个ClientSessionFilter在这个Filter中注入我们想要的bean public class ClientSessionFilter implements Filter {private static Logger log Logger.getLogger(ClientSessionFilter.class);Autowiredprivate RequestData requestData; //我们想要注入的beanOverridepublic void init(FilterConfig filterConfig) throws ServletException {}Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {ObjectMapper mapper new ObjectMapper();HttpServletRequest httpServletReq (HttpServletRequest) request;String session httpServletReq.getHeader(S);ClientSession cs;if(StringUtils.isNotBlank(session)) {try {cs mapper.readValue(session, ClientSession.class);} catch (Exception e) {log.error(Session序列化错误 e);throw new BusinessException(401, Session序列化错误);}if(log.isDebugEnabled()) {log.debug(cs);}requestData.setClientSession(cs);}chain.doFilter(request, response);}Overridepublic void destroy() {}
} 步骤二、在spring的配置文件application.xml中配置我们想要的bean和自定义的filter bean idrequestData scoperequest classcn.ucmed.common.cache.RequestDataaop:scoped-proxy/ //这个标签可以参考上一遍博客
/beanbean idclientSessionFilter classcn.ucmed.baseline.d2d.filter.ClientSessionFilter /步骤三、在web.xml中配置fileter filterfilter-namefilterProxy/filter-namefilter-classorg.springframework.web.filter.DelegatingFilterProxy/filter-classasync-supportedtrue/async-supportedinit-paramparam-nametargetBeanName/param-nameparam-valueclientSessionFilter/param-value/init-paraminit-paramparam-nametargetFilterLifecycle/param-nameparam-valuetrue/param-value/init-param
/filter
filter-mappingfilter-namefilterProxy/filter-nameurl-pattern/registeryuyue/*/url-pattern
/filter-mapping 转载于:https://www.cnblogs.com/happyflyingpig/p/7998449.html