Spring ApplicationListener使用方法及问题

使用场景

在一些业务场景中,当容器初始化完成之后,需要处理一些操作,比如一些数据的加载、初始化缓存、特定任务的注册等等。这个时候我们就可以使用Spring提供的ApplicationListener来进行操作。

用法

本文以在Spring boot下的使用为例来进行说明。首先,需要实现ApplicationListener接口并实现onApplicationEvent方法。把需要处理的操作放在onApplicationEvent中进行处理:

package com.secbro.learn.context;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * Created by zhuzs on 2017/5/12.
 */
public class ApplicationStartListener implements ApplicationListener<ContextRefreshedEvent>{
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("我的父容器为:" + contextRefreshedEvent.getApplicationContext().getParent());
        System.out.println("初始化时我被调用了。");
    }
}

然后,实例化ApplicationStartListener这个类,在Spring boot中通过一个配置类来进行实例化:

package com.secbro.learn.conf;

import com.secbro.learn.context.ApplicationStartListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by zhuzs on 2017/5/12.
 */
@Configuration
public class ListenerConfig {

    @Bean
    public ApplicationStartListener applicationStartListener(){
        return new ApplicationStartListener();
    }
}

随后,启动Spring boot服务,打印出一下内容:

我的父容器为:null
初始化时我被调用了。

从打印的结果可以看出,ApplicationStartListener的onApplicationEvent方法在容器启动时已经被成功调用了。而此时初始化的容器为root容器。

二次调用问题

此处使用Spring boot来进行操作,没有出现二次调用的问题。在使用传统的application.xml和project-servlet.xml配置中会出现二次调用的问题。主要原因是初始化root容器之后,会初始化project-servlet.xml对应的子容器。我们需要的是只执行一遍即可。那么上面打印父容器的代码用来进行判断排除子容器即可。在业务处理之前添加如下判断:

if(contextRefreshedEvent.getApplicationContext().getParent() != null){
            return;
}

这样其他容器的初始化就会直接返回,而父容器(Parent为null的容器)启动时将会执行相应的业务操作。

关联知识

在spring中InitializingBean接口也提供了类似的功能,只不过它进行操作的时机是在所有bean都被实例化之后才进行调用。根据不同的业务场景和需求,可选择不同的方案来实现。

  • 10
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
Spring框架的ApplicationListener是一个接口,用于监听Spring的上下文事件。它可以用于在Spring容器启动和关闭时执行一些特定的任务。下面是使用ApplicationListener的一些步骤: 1. 创建一个实现ApplicationListener接口的类,并实现onApplicationEvent()方法。该方法会在Spring上下文中发生事件时被触发。 2. 在Spring配置文件中注册这个监听器。可以通过添加<context:component-scan>和@Component注解来实现自动扫描,或者通过手动声明一个<bean>标签来注册。 3. 如果需要监听多个事件,可以在onApplicationEvent()方法中根据事件类型进行判断,然后执行相应的操作。 下面是一个简单的例子,演示了如何使用ApplicationListener监听Spring上下文的启动和关闭事件: ```java import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextRefreshedEvent; public class MyListener implements ApplicationListener<ApplicationEvent> { @Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ContextRefreshedEvent) { // Spring容器启动事件 System.out.println("Spring容器启动了!"); } else if (event instanceof ContextClosedEvent) { // Spring容器关闭事件 System.out.println("Spring容器关闭了!"); } } } ``` 在Spring配置文件中注册这个监听器: ```xml <bean id="myListener" class="com.example.MyListener"/> ``` 这样,当Spring容器启动或关闭时,就会调用MyListener类的onApplicationEvent()方法,并输出相应的信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序新视界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值