SpringMVC的filter怎么使用Autowired依赖注入bean
步骤一、定义一个ClientSessionFilter,在这个Filter中注入我们想要的bean
public class ClientSessionFilter implements Filter {
private static Logger log = Logger.getLogger(ClientSessionFilter.class);
@Autowired
private RequestData requestData; //我们想要注入的bean
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public 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);
}
@Override
public 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>
SpringMVC的filter怎么使用Autowired依赖注入bean的更多相关文章
- [转载]Spring下IOC容器和DI(依赖注入) @Bean及@Autowired
Spring下IOC容器和DI(依赖注入) @Bean及@Autowired自动装配 bean是什么 bean在spring中可以理解为一个对象.理解这个对象需要换一种角度,即可将spring看做一门 ...
- springmvc配置中,mapper一直依赖注入不进去的问题记录
问题还原: service层在引用mapper层接口时,一直依赖注入不进去.查看spring-context.xml配置,也未发现异常[因为以前就是这么配置],但是始终无法注入. 原因: 问题不出在s ...
- 依赖注入Bean属性
一.Bean属性依赖注入 对于类成员变量,注入方式有三种 •构造函数注入 •属性setter方法注入 •接口注入 Spring支持前两种 1.构造函数 属性注入 使用构造方法注入,在Spring配置文 ...
- servlet filter中使用autowired无法注入
问题: 我们为了避免未经授权的人直接通过url访问我们的页面,配置了如下filter <!-- 登录过滤器 --> <filter> <filter-name>se ...
- SpringAOP导致@Autowired依赖注入失败
之前用springAOP做了个操作日志记录,这次在往其他类上使用的时候,service一直注入失败,找了网上好多内容,发现大家都有类似的情况出现,但是又和自己的情况不太符合.后来总结自己的情况发现:方 ...
- listener中@Autowired无法注入bean的一种解决方法
背景:使用监听器处理业务,需要使用自己的service方法: 错误:使用@Autowired注入service对象,最终得到的为null: 原因:listener.fitter都不是Spring容器管 ...
- 解决Spring Boot集成Shiro,配置类使用Autowired无法注入Bean问题
如题,最近使用spring boot集成shiro,在shiroFilter要使用数据库动态给URL赋权限的时候,发现 @Autowired 注入的bean都是null,无法注入mapper.搜了半天 ...
- Spring 注解Autowired自动注入bean异常解决
错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xx' is defined ...
- 依赖注入Bean属性——手动装配Bean
一.构造方法注入 其中,可以根据不同的参数列表调用不同的重载的构造方法: 其中,基本数据类型没有包,引用类型都有包路径,基本类型对应封装类: 二.通过property标签调用类的set方法注入 三.通 ...
随机推荐
- 第一章:大数据 の Linux 基础 [更新中]
本课主题 Linux 休系结构图 Linux 系统启动的顺序 Linux 查看内存和 CPU 指令 环境变量加载顺序 Linux 内存结构 Linux 休系结构图 Linux 大致分为三个层次,第一层 ...
- while100以内的偶数
#显示100以内的偶数 #声明i i = 1 #开始循环条件为i不等于100,执行while代码块 while i != 100: #给i加1 i +=1 #如果循环到此时i的取余运算为0则打印i i ...
- (3两个例子)从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练
从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练 1综述http://www.cnblogs.com/jsxyhelu/p/7907241.html2环境架设http://www.cn ...
- spring boot系列03--spring security (基于数据库)登录和权限控制(下)
(接上篇) 后台 先说一下AuthConfig.java Spring Security的主要配置文件之一 AuthConfig 1 @Configuration 2 @EnableWebSecuri ...
- java_web学习(十) 显示mysql中的数据
一.建立数据库 create database animal; create table animal( sno int, name varchar(20), weight varcahr(20), ...
- linux(八)linux系统中查找文件二
前面介绍的是find命令,我们发现一个find命令居然有那么多的命令,我看到都要晕了,不管没有关系,加油.相信自己! 一.grep命令 1.1.作用 Linux系统中grep命令是一种强大的文本搜索工 ...
- 洛谷 P1177 【模板】快速排序【13种排序模版】
P1177 [模板]快速排序 题目描述 利用快速排序算法将读入的N个数从小到大排序后输出. 快速排序是信息学竞赛的必备算法之一.对于快速排序不是很了解的同学可以自行上网查询相关资料,掌握后独立完成.( ...
- Kruscal(最小生成树)算法模版
;//最大点数 ;//最大边数 int n,m;//n表示点数,m表示边数 struct edge{int u,v,w;} e[maxm];//u,v,w分别表示该边的两个顶点和权值 bool cmp ...
- BZOJ1294: [SCOI2009]围豆豆Bean
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1294 状压dp,dis[s][i][j]表示从(i,j)出发围的状态是s的最短路. 然后判断一 ...
- python笔记三(面向对象)
Python3 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本章节我们将详细介绍Python的面向对象编程. 如果你以前没有接触 ...