(转)Springboot 中filter 注入对象
问题:我建立一个全局拦截器,当然,这是测试的时候建立的,我把它命名为LogFilter,它继承了Filter,web应用启动的顺序是:listener->filter->servlet,而因为项目应用了springboot,所以我们项目启动时,先初始化listener,因此注解的bean会被初始化和注入;然后再来就filter的初始化,再接着才到我们的dispathServlet的初始化,因此,当我们需要在filter里注入一个注解的bean时,就会注入失败,因为filter初始化时,注解的bean还没初始化,没法注入。
那么,解决方法如下:
public FilterRegistrationBean filterProxy(){
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
DelegatingFilterProxy httpBasicFilter = new DelegatingFilterProxy();
registrationBean.setFilter(httpBasicFilter);
Map<String,String> m = new HashMap<String,String>();
m.put("targetBeanName","logFilter");
m.put("targetFilterLifecycle","true");
registrationBean.setInitParameters(m);
List<String> urlPatterns = new ArrayList<String>();
urlPatterns.add("/*");
registrationBean.setUrlPatterns(urlPatterns);
return registrationBean;
}
这样你就可以在LogFilter里面添加@Autowired下的bean了。
public class LogFilter implements Filter {
@Autowired
@Qualifier(value="rztRabbitTemplate")
private RabbitTemplate rabbitTemplate;
@Bean
public FilterRegistrationBean securityFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new DelegatingFilterProxy("SecurityFilter"));
registration.addInitParameter("targetFilterLifecycle", "true");
registration.addUrlPatterns("/*");
registration.setOrder(102);
return registration;
} @Bean(name = "SecurityFilter")
public SecurityFilter getSecurityFilter(){
return new SecurityFilter();
}
转自:http://blog.csdn.net/u013030980/article/details/55270818#comments
(转)Springboot 中filter 注入对象的更多相关文章
- Filter注入对象
由于没有在web.xml文件中加上<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter ...
- SpringBoot中yaml配置对象
转载请在页首注明作者与出处 一:前言 YAML可以代替传统的xx.properties文件,但是它支持声明map,数组,list,字符串,boolean值,数值,NULL,日期,基本满足开发过程中的所 ...
- springboot中filter的用法
一.在spring的应用中我们存在两种过滤的用法,一种是拦截器.另外一种当然是过滤器.我们这里介绍过滤器在springboot的用法,在springmvc中的用法基本上一样,只是配置上面有点区别. 二 ...
- SpringBoot:SpringBoot中@Value注入失败
1. 第一步检测语法是否正确 @Value("${test}") private String test; 2.第二步检测配置文件中是否有进行配置 url=testusername ...
- springboot中filter的配置和顺序执行
项目结构 springboot版本 <parent> <groupId>org.springframework.boot</groupId> <artifac ...
- @Mapper注解在springboot中无法注入
问题① @Mapper注解报红无法注入 方法 在pom文件中添加依赖
- 记一个SpringBoot中属性注入失败的问题Consider defining a bean of type ''' in your configuration
今天遇到的一个问题: 代码检查了好几次,都没有错误,但是启动时就会报错Consider defining a bean of type ''' in your configuration. 启动类在c ...
- SpringBoot中service注入失败(A component required a bean of type 'XXService' that could not found)
先写了JUnit,发现启动不了,注释掉有问题的service也不可以.可能是因为spring开始时会加载所有service吧. 按照网友们的说法,一般需要检查: 1.入口类有没有写MapperScan ...
- spring项目中如何添加定时器以及在定时器中自动生成sprng注入对象
最近做了一个java的项目,部门领导给了一套代码让我尽快掌握,说心里话本人真心不喜欢java的这种项目方式,各种配置各种xml文件简直头都大了,下面就将我遇到的其中一个我认为是坑的地方整理出来,希望能 ...
随机推荐
- Web服务器配置Gzip压缩提升网站性能
前言: HTTP协议上的GZIP编码是一种用来改进WEB应用程序性能的技术.大流量的WEB站点常常使用GZIP压缩技术来让用户感受更快的速度.这一般是指WWW服务器中安装的一个功能,当有人来访问这个服 ...
- eagle学习汇总
一.原理图编辑器 1. 编辑->全局属性->可以设置全局变量,选择“文本框”,以‘>’开头代表引用全局属性的值. 2. 绘制->Frame->可绘制原理图边框,一般选择“ ...
- linux下文件描述符的介绍
当某个程序打开文件时,操作系统返回相应的文件描述符,程序为了处理该文件必须引用此描述符.所谓的文件描述符是一个低级的正整数.最前面的三个文件描述符(0,1,2)分别与标准输入(stdin),标准输出( ...
- LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)
题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description 姊妹篇:http://www. ...
- sencha touch 扩展官方NavigationView 灵活添加按钮组,导航栏,自由隐藏返回按钮(2014-5-15)
扩展视频讲解:http://www.cnblogs.com/mlzs/p/3652094.html官方NavigationView详解:http://www.cnblogs.com/mlzs/p/35 ...
- JavaScript 闭包(Closure)
闭包(closure)是掌握Javascript从人门到深入一个非常重要的门槛,它是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 闭包-无处不在 在前端编程中,使 ...
- Nginx学习之keepalive
当然,在nginx中,对于http1.0与http1.1也是支持长连接的.什么是长连接呢?我们知道,http请求是基于TCP协议之上的,那么,当客户端在发起请求前,需要先与服务端建立TCP连接,而每一 ...
- CF510B Fox And Two Dots(搜索图形环)
B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 使用DnsCat反弹shell
DnsCat技术特点 Dns隧道反弹shell DnsCat服务器的安装 #git clone https://github.com/iagox86/dnscat2.git #cd dnscat2 # ...
- 【咸鱼教程】Wing动画编辑器创建精美(一般-_-)开场动画
游戏中会用着一些简单的动画,公司一般使用的dragonbones制作,导出二进制格式或者MC来使用.感觉一些简单动画直接使用动画编辑器更加简便些. 引擎版本:5.0.14wing版本:4.1.0 一 ...