SpringMVC的filter怎么使用Autowired依赖注入bean
步骤一、定义一个ClientSessionFilter,在这个Filter中注入我们想要的bean
public class ClientSessionFilter implements Filter {private static Logger log = Logger.getLogger(ClientSessionFilter.class);@Autowiredprivate RequestData requestData; //我们想要注入的bean@Overridepublic 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 id="requestData" scope="request" class="cn.ucmed.common.cache.RequestData"><aop:scoped-proxy/> //这个标签可以参考上一遍博客
</bean><bean id="clientSessionFilter" class="cn.ucmed.baseline.d2d.filter.ClientSessionFilter" />
步骤三、在web.xml中配置fileter
<filter><filter-name>filterProxy</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class><async-supported>true</async-supported><init-param><param-name>targetBeanName</param-name><param-value>clientSessionFilter</param-value></init-param><init-param><param-name>targetFilterLifecycle</param-name><param-value>true</param-value></init-param> </filter> <filter-mapping><filter-name>filterProxy</filter-name><url-pattern>/registeryuyue/*</url-pattern> </filter-mapping>
转载于:https://www.cnblogs.com/happyflyingpig/p/7998449.html
SpringMVC的filter怎么使用Autowired依赖注入bean相关推荐
- 【spring】依赖注入之@Autowired依赖注入
@Autowired依赖注入 本文源码基于spring-framework-5.3.10. 源码位置:org.springframework.beans.factory.annotation.Auto ...
- @Autowired依赖注入
依赖注入的三种方式: 基于字段注入 基于构造器注入 基于set方法注入 基于字段注入 @SpringBootTest class AnnotationApplicationTests {@Autowi ...
- Spring源码分析(十二)autowire和@Autowired 依赖注入源码解析总结
XML的autowire自动注入 在XML中,我们可以在定义一个Bean时去指定这个Bean的自动注入模式: byType byName constructor default no 比如: < ...
- Spring依赖注入Bean为空,注入失效场景
场景介绍 使用spring往一个bean(BeanB)注入另一个bean(BeanA),发现BeanA为null,注入失败了. 代码展示 /*** @author huangd* @date 2021 ...
- SpringAOP导致@Autowired依赖注入失败
用springAOP做操作日志记录,发现:方法为private修饰的,在AOP适配的时候会导致service注入失败,但是同一个service其他的public方法正常 public static b ...
- 使用Filter,Listener 时无法注入Bean的解决方法
相关文章: SpringBoot 之AOP切面的使用 SpringBoot之Listener注册到Spring容器中的多种方法 SpringBoot之Interceptor拦截器注入使用 Spring ...
- 解决Spring Boot集成Shiro,配置类使用Autowired无法注入Bean问题
为什么80%的码农都做不了架构师?>>> 如题,最近使用spring boot集成shiro,在shiroFilter要使用数据库动态给URL赋权限的时候,发现 @Autowi ...
- 依赖注入Bean属性——手动装配Bean
一.构造方法注入 其中,可以根据不同的参数列表调用不同的重载的构造方法: 其中,基本数据类型没有包,引用类型都有包路径,基本类型对应封装类: 二.通过property标签调用类的set方法注入 三.通 ...
- arg是什么函数_java后端开发三年!你还不了解Spring 依赖注入,凭什么给你涨薪...
前言 前两天和一个同学吃饭的时候同学跟我说了一件事,说他公司有个做了两年的人向他提出要涨薪资,他就顺口问了一个问题关于spring依赖注入的,那个要求涨薪的同学居然被问懵了...事后回家想了想这一块确 ...
最新文章
- Redis面试突击专用
- 康宁玻璃ct值计算公式_CT原理(一)
- leetcode 102.二叉树的层序遍历
- java final修饰的类_java final修饰符使用总结,final修饰类详解
- Django项目知识点(四)
- html5按钮样式具有子项目,如何使用HTML5+css3制作出12种常用的按钮开关样式(附完整代码)...
- 下载CIFAR-10、CIFAR-100数据集的方法
- 为 Nginx 添加模块
- 疯狂连连看之开发游戏界面组件一
- JAVA解决实例问题_解决java方法
- mysql c3p0 释放连接池_mysql – 如何阻止c3p0连接池隐藏连接异常的原因?
- MDSF:如何使用GMF来做TOGAF建模工具
- switchHost管理员身份运行出错问题
- Learning the Model Update for Siamese Trackers论文笔记
- Python学习笔记:part 1
- 计算机学院机考,东华大学计算机学院研究生复试上机考真题.doc
- 我想知道如何用风扇自制水空调?
- python语音唤醒功能_python语音唤醒-pocketsphinx
- 公孙殃同学是很有见地啊
- 数据结构 第5章 树和二叉树 课后答案