spring如何把service注入listener或者filter?
来源:互联网 发布:传奇霸业魂珠活动数据 编辑:程序博客网 时间:2024/06/18 08:24
答案是不可以。
但listener中需要使用service方法怎么办?
下面介绍三种方式:1、用qurartz的一次性任务的实现方式 2、 也可以用下面的方式来获取applicationContext,然后自己加载bean。3、 还可以自己解析配置文件,单独生成一个context,但这样浪费太大,不建议这样做。
下面贴上第二种方法的实现代码
在listener中
在filter或者jsp、servlet中类似,public void contextInitialized(ServletContextEvent arg0) {ServletContext context = arg0.getServletContext();ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);ArticleInfoService articleinfoService = (ArticleInfoService) ctx.getBean("articleinfoService");articleinfoService.init();}
使用HttpServletRequest获取SerlvetContext的方法是:request.getSession().getServletContext();
------------------------------------------------------------------------------------------------
另外扩展一下,spring定义的或者注解的各个bean,如何在普通类(没有被spring管理的类,比如自己new一个)中获取呢?
在spring中有一个org.springframework.context.ApplicationContextAware,只要继承并实现这个接口即可使用spring的全局context。
使用这个的前提是spring已经初始化完毕,所以在listener中还不能使用这个方法。
需要两个步骤:
1、实现这个接口
2、把实现该接口的类,配置到spring的配置文件(application-all.xml)中。import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;public class GlobalContext implements ApplicationContextAware {private static ApplicationContext context;@Overridepublic void setApplicationContext(ApplicationContext contex)throws BeansException {context = contex;}public static ApplicationContext getContext() {return context;}public final static Object getBean(String beanName) {return context.getBean(beanName);}public final static Object getBean(String beanName, Class<?> requiredType) {return context.getBean(beanName, requiredType);}}
这样就可以在任何地方使用globalcontext来获取spring定义的bean了<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans><!-- getGlobalContext --><bean class="com.netease.extration.util.GlobalContext"></bean></beans>
- spring如何把service注入listener或者filter?
- listener或者filter注入service
- filter获取 spring bean 或者service
- 如何让servelt和Filter注入到spring容器或者在servlet中调用spring中的bean
- 在filter注入或者是得到spring托管的bean
- 在Servlet(或者Filter,或者Listener)中使用spring的IOC容器
- 在Servlet(或者Filter,或者Listener)中使用spring的IOC容器
- 在Servlet(或者Filter,或者Listener)中使用spring的IOC容器
- 在Servlet(或者Filter,或者Listener)中使用spring的IOC容器
- (转)在Servlet(或者Filter,或者Listener)中使用spring的IOC容器
- 在Servlet(或者Filter,或者Listener)中使用spring的IOC容器
- 在servlet(或者filter或者listener)中使用spring的IOC容器
- spring-boot如何定义一个servlet,filter,listener
- 如何在Java的Filter中注入Service???
- spring boot-servlet、filter、listener
- spring--filter如何注入 spring管理的bean(54)
- spring的filter注入
- Spring 注入 Filter
- HTML各种兼容性
- 对象的三大特征
- java利用ant压缩指定文件夹
- Hibernate报错(user is not mapped)
- 面向对象2
- spring如何把service注入listener或者filter?
- C:内存使用
- display:inline-block兼容ie6/7的写法
- leetcode 191 Number of 1 Bits
- 第1章 使用petalinux参考BSP
- 第2章 测试Petalinux BSP镜像
- Maximum Subarray 和最大的子数组
- 计算复杂性-现代方法
- 《C专家编程》:如何检测链表里的环(附1)