java web自定义监听器_Spring 实现自定义监听器案例

应用场景:

在一般的javaWeb项目中经常有一些缓存是需要再项目启动的时候加载到内存中,这样就可以使用自定义的监听器来实现。

1、在web.xml中声明

com.cn.framework.constant.OmsConfigLoader

2、创建类OmsConfigLoader 实现接口 ServletContextListener,项目启动的时候service还没有注入,此时调用service的方法会报错,因为在web容器中无论是servlet还是Filter都不是Spring容器来管理的。

listener的生命周期是web容器维护的,bean的生命周期是由Spring容器来维护的,所以在listener中使用@Resource,listener不认识,

可以沟通过如下方法来解决:

使用WebApplicationContextUtils工具类,该工具类的作用是获取到spring容器的引用,进而获取到我们需要的bean实例。

package com.cn.framework.constant;

import javax.servlet.ServletContextEvent;

import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;

import org.springframework.web.context.support.WebApplicationContextUtils;

import com.kxs.service.systemService.ISystemService;

public class OmsConfigLoader implements ServletContextListener {

private static Logger LOG = Logger.getLogger(OmsConfigLoader.class);

@Override

public void contextDestroyed(ServletContextEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void contextInitialized(ServletContextEvent arg0) {

LOG.info("==> 加载OMS系统配置信息 Start ==");

try {

ISystemService iSystemService = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext())

.getBean(ISystemService.class);

iSystemService.refreshCache();

} catch (Exception e) {

e.printStackTrace();

LOG.info(e.toString());

}

LOG.info("==> 加载OMS系统配置信息 End ==");

}

}

补充:Spring-xml配置自定义事件监听器

一、自定义事件

Spring中使用自定义事件类型:

第一步:自定义事件类型:自定义类需要继承Spring中org.springframework.context.ApplicationEvent类

第二步:设置事件监听器,实现org.springframework.context.ApplicationListener接口,重写onApplicationEvent方法监听事件源

第三步:将事件监听器配置到Spring中,通过xml配置文件将事件监听器配置到bean容器中

第四步:Spring容器(container容器发布事件)发布事件

自定义事件类型

public class RainEvent extends ApplicationEvent {

private static final long serialVersionUID = 1L;

public RainEvent(Object source) {

super(source);

}

}

监听器:可以创建多个监听器

public class RainEventListener1 implements ApplicationListener {

//监听rainevent事件,调用当前方法

@Override

public void onApplicationEvent(RainEvent event) {

Object source = event.getSource();

System.out.println("监听器1:"+source);

}

}

public class RainEventListener2 implements ApplicationListener {

//监听rainevent事件,调用当前方法

@Override

public void onApplicationEvent(RainEvent event) {

Object source = event.getSource();

System.out.println("监听器2:"+source);

}

}

xml配置文件将监听器配置到bean容器中

bean容器发布事件

public void ioc_event() {

try {

String path = "com/briup/ioc/event/event.xml";

ApplicationContext container =

new ClassPathXmlApplicationContext(path);

container.publishEvent(new RainEvent("打雷了,下雨了!"));

} catch (Exception e) {

e.printStackTrace();

}

}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值