spring Filter中注入bean
来源:互联网 发布:java嵌入式web服务器 编辑:程序博客网 时间:2024/06/11 13:36
遇到朋友在filter中用@autowired注入,得到的对象是null,于是根据自己项目中的使用,提供了下列解决方法。
1.web.xml中配置
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class></listener><filter> <filter-name>AuthFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class></filter><filter-mapping> <filter-name>AuthFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping>
2.spring bean中配置:
<bean id="AuthFilter" class="**.**.***.******.filter.AuthFilter"> <property name="authService" ref="authService"></property></bean>
3.实现类
ps. 简单描述一下:
java web项目启动加载顺序为:
context-param -> listener -> filter -> servlet
项目启动时,先初始化listener,然后是filter的初始化,最后才是servlet的初始化(这和web.xml配置文件中他们所对应的先后顺序无关,但针对于某类配置而言,与他们出现的顺序是有关的),因此,当我们在filter里使用注解注入一个bean时,就会注入失败,因为filter在初始化的时候,注解的bean还没有被初始化,所以会出现注入对象为null的情况。
解决办法应该有多种,针对上述解决办法,filter加入org.springframework.web.filter.DelegatingFilterProxy类,DelegatingFilterProxy类是一个代理类,所有的请求都会首先发给这个代理,然后再按照“filter-name”委派到spring中的这个bean,因此要Spring中配置的bean的name和web.xml中的一样.
0 0
- spring Filter中注入bean
- Spring boot 中filter注入bean
- Spring-boot中Filter注入bean
- spring boot Filter 注入bean
- web Filter中使用spring注入的bean
- Web Filter中使用Spring注入bean elegatingFilterProxy
- web Filter中使用spring注入的bean
- Filter中注入spring
- 通过spring在filter注入bean(DelegatingFilterProxy与servlet filter)
- 通过spring在filter注入bean(DelegatingFilterProxy与servlet filter) .
- 如何在filter中注入bean
- 如何在filter中注入bean
- Spring中bean的注入
- spring中bean配置和bean注入
- spring中bean配置和bean注入
- spring中bean配置和bean注入
- spring中bean配置和bean注入
- spring--filter如何注入 spring管理的bean(54)
- Redis中的value包含中文显示为十六进制??
- android 学习----------------ipc知识点
- Android NDK 初体验
- Python 爬取数据接口
- #npm# extraneous问题
- spring Filter中注入bean
- 编辑 Ext 表格(一)——— 动态添加删除行列
- android shape 圆形背景
- 查看libcurl 内部DEBUG日志
- git 个人使用总结~
- excel 破解密码
- Android Color xml
- 一道关于下标排序的金山笔试题
- Mac 下使用 ZIP 分卷压缩