如何在Java的Filter中注入Service???
来源:互联网 发布:英语口语听力训练软件 编辑:程序博客网 时间:2024/05/22 11:38
首先,本文 绝对是好文!不止本文,作者的文章都是很经典的,希望大家给小编一个赞,关注一下!
今天在做用户使用cookie自动登录的时候,发现在LoginFilter中读取到cookie以后要进行查询数据库然后进行用户名和密码的比对,查询数据库肯定要用到Service和Dao,一开始我以为在service属性上直接使用注解就能搞定,没想到啊,还是报了空指针异常。类似下面这样(这样是注入不进来的)
@Autowiredprivate UserService userService;
先给大家说说为什么在Filter中注入不进来service
我建立一个全局拦截器,当然,我把它命名为LoginFilter,它继承了Filter,web应用启动的顺序是:listener->filter->servlet,而因为项目应用了spring mvc,所以我们会有一个配置文件(applixationContext.xml),我们在配置spring时会用到spring的listener,它会读取application.xml里的配置对spring context进行初始化;项目启动时,先初始化listener,因此配置在applicationContext.xml里的bean会被初始化和注入;然后再来就filter的初始化,再接着才到我们的dispathServlet的初始化,因此,当我们需要在filter里注入一个注解的bean时,就会注入失败,因为filter初始化时,注解的bean还没初始化,没法注入。
遇到一个问题,我们要知其然,并且知其所以然,知道原因以后下面就是解决了!
今天就来教大家怎么在Java的Filter中注入Service
1、在我们的LoginFilter中加入需要注入的属性
/*** userService属性*/private UserService userService;
2、在LoginFilter的init()方法中获取 ServletContext
①这里有三种方式获取ServletContext
1)在javax.servlet.Filter中直接获取
ServletContext context = config.getServletContext();
2)在HttpServlet中直接获取
this.getServletContext()
3)在其他方法中,通过HttpServletRequest获得
request.getSession().getServletContext();
3、然后在LoginFilter中获取我们的WebApplicationContext上下文
WebApplicationContext cxt =WebApplicationContextUtils.getWebApplicationContext(sc);
从下面spring源码的可以看到WebApplicationContextUtils.getWebApplicationContext(sc)需要传入一个ServletContext对象(就是我们上面获取的)我们把sc放进去
public static WebApplicationContext getWebApplicationContext(ServletContext sc) {return getWebApplicationContext(sc, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);}
4、从WebApplicationContext上下文中获取bean,赋值给我们的userService
userService = (UserService) cxt.getBean(UserService.class);
这样就完成注入了 ,是不是很方便呢!
下面小编把部分源码放上面,方便大家参考:
public class LoginFilter implements Filter {protected static final Logger logger = Logger.getLogger(LoginFilter .class);
private FilterConfig config;
/**
* userService属性(作者的企鹅群:162582394密码1欢迎进群交流学习)
*/
private UserService userService;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
config = filterConfig;
/**
* 注入service 用户查询数据库
*/
ServletContext sc = filterConfig.getServletContext();
WebApplicationContext cxt =WebApplicationContextUtils.getWebApplicationContext(sc);
if(cxt != null && cxt.getBean(UserService.class) != null && userService == null){
userService = (UserService) cxt.getBean(UserService.class);
}
}}}
然后我们就可以调用userService中的方法了:
JSONObject data =userService.userLogin(userName,passWord);
觉得给力,给小编一个赞呗!作者的企鹅群:162582394密码1欢迎进群交流学习
- 如何在Java的Filter中注入Service???
- 在Filter中注入Service
- 在Java Filter 中注入 Service方案一
- 在如何在Filter 中注入
- 如何在filter中注入bean
- 如何在filter中注入bean
- 如何在utils工具类中注入mapper service
- 如何使用springmvc框架在普通的servlet注入service
- spring如何把service注入listener或者filter?
- listener或者filter注入service
- Filter里面注入Service方法
- <翻译> 如何利用Bloom Filter在Java中构建大规模基于内存的缓存
- SpringMVC项目中偶尔用到servlet,如何在servlet中注入service笔记
- 如何在Service层获取用户session中保存的用户信息的方法(Filter+ThreadLocal)
- java 静态工具类中注入service
- Filter中注入spring
- 如何让servelt和Filter注入到spring容器或者在servlet中调用spring中的bean
- 在jdbc中,preparedStatement是如何防止SQL注入的
- python:time模块用法
- Vue2.0使用props传递数据---3
- 使用int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();可以获取屏幕旋转的方向
- Android 之 activity活动(二、Toast,menu,活动销毁)
- 求单词长度
- 如何在Java的Filter中注入Service???
- MapReduce工作原理图文详解
- 第一次作业
- no2 神经网络
- weblogic服务器部署的程序,如何直接通过IP访问(即URL中去掉工程名)
- C++,在UE4中写Hello World!!
- SQL 语句的多表查询方式
- printf()中的数据类型%h后为什么要加上d才能表示短整型
- Django小结