利用Spring怎么实现一个监听器功能

发布时间:2021-01-21 15:11:52 作者:Leah
来源:亿速云 阅读:160

这篇文章将为大家详细讲解有关利用Spring怎么实现一个监听器功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

1、在web.xml中声明

<!-- 自定义监听 启动加载系统参数 -->
 <listener>
 <listener-class>com.cn.framework.constant.OmsConfigLoader</listener-class>
</listener>

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> {
 //监听rainevent事件,调用当前方法
 @Override
 public void onApplicationEvent(RainEvent event) {
 Object source = event.getSource();
 System.out.println("监听器1:"+source); 
 }
}
public class RainEventListener2 implements ApplicationListener<RainEvent> {
 //监听rainevent事件,调用当前方法
 @Override
 public void onApplicationEvent(RainEvent event) {
 Object source = event.getSource();
 System.out.println("监听器2:"+source); 
 }
}

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

<!-- 配置监听器,向spring容器发布事件,自动触发监听器的onApplicationEvent方法 -->
<bean class="com.briup.ioc.event.RainEventListener1"></bean>
<bean class="com.briup.ioc.event.RainEventListener2"></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();
 }
}

关于利用Spring怎么实现一个监听器功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 如何在Spring中利用REST实现一个分页功能
  2. 利用spring框架怎么实现一个邮件发送功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring

上一篇:网络无internet访问的解决方法

下一篇:怎样制作系统U盘

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》