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方法注入 三.通 ...
随机推荐
- JS 对象API之获取原型对象
1.从 构造函数 获得 原型对象: 构造函数.prototype 2.从 对象实例 获得 父级原型对象: 方法一: 对象实例.__proto__ [ 有兼容性问题,不建议使用] 方法二: ...
- android自定义文本框,后面带清空按钮
android常见的带清空按钮的文本框,获得焦点时如果有内容则显示,否则不显示 package com.qc.health.weight; import com.qc.health.R; import ...
- 史上最简单的js+css3实现时钟效果
今天我看到百度搜索的时间那个效果不错,于是就产生了模仿一下的效果,不过为了节省时间,就随便布了下局,废话不多说,先看看效果吧,顺便把百度的效果也拿过来. 对比样子差了好多啊,但是基本功能都是实现了的, ...
- kali系统教程:创建热点
1:把网卡改成混杂模式 sduo airmon-ng start wlan0 2:aircrack套件创建一个fake-ap airbase-ng -e fake-ap -c wlan0mon 此时, ...
- Java集合系列[1]----ArrayList源码分析
本篇分析ArrayList的源码,在分析之前先跟大家谈一谈数组.数组可能是我们最早接触到的数据结构之一,它是在内存中划分出一块连续的地址空间用来进行元素的存储,由于它直接操作内存,所以数组的性能要比集 ...
- 常见的Linux 的命令
rm命令 -f :就是force的意思,忽略不存在的文件,不会出现警告消息 -i :互动模式,在删除前会询问用户是否操作 -r :递归删除,最常用于目录删除,它是一个非常危险的参数 如: rm -i ...
- 基于input子系统的sensor驱动调试(二)
继上一篇:http://www.cnblogs.com/linhaostudy/p/8303628.html#_label1_1 一.驱动流程解析: 1.模块加载: static struct of_ ...
- Keepalived概述和安装(1)
一.Keepalived概述 本文主要了解开源高可用负载均衡集群利器keepalived,掌握keepalived的安装,运用keepalived配置高可用集群,并能够实现keepalived与负载均 ...
- Machine Learning - week 4 - 习题
1. 第一个.,是对的. 第二个.结果只会认出是一类,如图:.但这里是为了举例子,实际上不会恰好是 1.而是选择最大概率表示的类别.所以是错误的. 第三个:所有基于 0,1 的逻辑方程都可以使用神经网 ...
- bzoj:1675 [Usaco2005 Feb]Rigging the Bovine Election 竞选划区
Description It's election time. The farm is partitioned into a 5x5 grid of cow locations, each of wh ...