问题:我建立一个全局拦截器,当然,这是测试的时候建立的,我把它命名为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 注入对象的更多相关文章

  1. Filter注入对象

    由于没有在web.xml文件中加上<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter ...

  2. SpringBoot中yaml配置对象

    转载请在页首注明作者与出处 一:前言 YAML可以代替传统的xx.properties文件,但是它支持声明map,数组,list,字符串,boolean值,数值,NULL,日期,基本满足开发过程中的所 ...

  3. springboot中filter的用法

    一.在spring的应用中我们存在两种过滤的用法,一种是拦截器.另外一种当然是过滤器.我们这里介绍过滤器在springboot的用法,在springmvc中的用法基本上一样,只是配置上面有点区别. 二 ...

  4. SpringBoot:SpringBoot中@Value注入失败

    1. 第一步检测语法是否正确 @Value("${test}") private String test; 2.第二步检测配置文件中是否有进行配置 url=testusername ...

  5. springboot中filter的配置和顺序执行

    项目结构 springboot版本 <parent> <groupId>org.springframework.boot</groupId> <artifac ...

  6. @Mapper注解在springboot中无法注入

    问题① @Mapper注解报红无法注入 方法 在pom文件中添加依赖

  7. 记一个SpringBoot中属性注入失败的问题Consider defining a bean of type ''' in your configuration

    今天遇到的一个问题: 代码检查了好几次,都没有错误,但是启动时就会报错Consider defining a bean of type ''' in your configuration. 启动类在c ...

  8. SpringBoot中service注入失败(A component required a bean of type 'XXService' that could not found)

    先写了JUnit,发现启动不了,注释掉有问题的service也不可以.可能是因为spring开始时会加载所有service吧. 按照网友们的说法,一般需要检查: 1.入口类有没有写MapperScan ...

  9. spring项目中如何添加定时器以及在定时器中自动生成sprng注入对象

    最近做了一个java的项目,部门领导给了一套代码让我尽快掌握,说心里话本人真心不喜欢java的这种项目方式,各种配置各种xml文件简直头都大了,下面就将我遇到的其中一个我认为是坑的地方整理出来,希望能 ...

随机推荐

  1. 微信redirect_uri 回调错误,scope权限错误

    scope权限错误以及微信redirect_uri回调错误 昨天修改项目的时候,初始时,因为项目最开始使用的是第三方授权处理,拿到的用户openid是第三方账号的,所以需要将获取对方信息的代码修改.只 ...

  2. angularjs笔记《二》

    小颖最近不知怎么了,老是犯困,也许是清明节出去玩,到现在还没缓过来吧,玩回来真的怕坐车了,报了个两日游得团,光坐车了,把人坐的难受得,去了也就是爬山,回来感觉都快瘫了,小颖去的时候还把我家仔仔抱着一起 ...

  3. linux mint 安装 SecureCRT

    最喜欢的ssh工具莫过于SecureCRT了,功能强大,方便易用.最近安装了mint17.1(基于ubuntu),就想安装一个SecureCRT来使用. [此SecureCRT仅作测试使用,不做商业用 ...

  4. vue使用sass

    一.安装sass依赖包 $ npm install sass-loader --save-dev //sass-loader依赖于node-sass $ npm install node-sass - ...

  5. Centos 7网络文件系统nfs服务的安装与配置

    实验环境>>>>>>>>> nfs服务端:(nfs-server)192.168.100.2 nfs客户端:(nfs-client)192.168 ...

  6. docker swarn集群笔记

    .安装Docker 三剑客: curl -L https://github.com/docker/machine/releases/download/v0.10.0/docker-machine-`u ...

  7. mysql判断一个字符串是否包含某几个字符

    使用locate(substr,str)函数,如果包含,返回>0的数,否则返回0

  8. np.tile 函数使用

    >>> import numpy>>> numpy.tile([0,0],5)#在列方向上重复[0,0]5次,默认行1次array([0, 0, 0, 0, 0, ...

  9. java字符串的替换

    split也是用到了正则表达式 replace 是没有用正则表达式,全部替换 replaceAll 和replaceFirst是用了正则表达式 replaceAll替换所有,replaceFirst是 ...

  10. hdu1166 敌兵布阵【线段树】

    C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于 ...