栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring的ApplicationListener和ApplicationContext的使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring的ApplicationListener和ApplicationContext的使用

Spring的ApplicationListener和ApplicationContext的使用
  • 1 引入ApplicationContext
  • 2 关于ApplicationListener的说明
    • 1 ApplicationListener简介
    • 2 ApplicationListener案列
        • 1 准备一个SpringBoot 环境
        • 2 创建一个自定义的监听器
        • 3 启动项目,查看日志
  • 3 关于**ApplicationContext**的说明
    • 1ApplicationContext的简介
    • 2ApplicationContext的案列
        • 1 准备一个SpringBoot 环境
        • 2 创建一个自定义的监听器
        • 3 创建一个自定义的事件
        • 4启动项目,查看日志

关于Spring的源码相关功能

1 引入ApplicationContext

ApplicationContext是Spring的一个核心接口,允许容器通过应用程序上下文环境创建,获取,管理bean.

public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
		MessageSource, ApplicationEventPublisher, ResourcePatternResolver {

	
	@Nullable
	String getId();

	
	String getApplicationName();

	
	String getDisplayName();

	
	long getStartupDate();

	
	@Nullable
	ApplicationContext getParent();

	AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;

}

ApplicationContext提供的功能:

  • 访问应用程序组件的Bean工厂方法. 从org.springframework.beans.factory.ListableBeanFactory继承而来.
public interface ListableBeanFactory extends BeanFactory {
    ......
}
  • 通用方式加载文件资源的能力.从org.springframework.core.io.support.ResourcePatternResolver继承而来.
package org.springframework.core.io.support;

import java.io.IOException;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

public interface ResourcePatternResolver extends ResourceLoader {
    String CLASSPATH_ALL_URL_PREFIX = "classpath*:";

    Resource[] getResources(String var1) throws IOException;
}
  • 向注册监听器发布事件的能力. 从org.springframework.context.ApplicationEventPublisher继承而来.
@FunctionalInterface
public interface ApplicationEventPublisher {
	default void publishEvent(ApplicationEvent event) {
		publishEvent((Object) event);
	}
	void publishEvent(Object event);

}
  • 解析消息的能力,支持国际化. 从org.springframework.context.MessageSource继承而来.
public interface MessageSource {

	@Nullable
	String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale);

	String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException;

	String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException;

}
  • 从父上下文继承,后代上下文中的定义总是优先级.单个父上下文可以被整个web应用程序使用,而每个servlet都有自己独立于任何其他servlet的子上下文.
2 关于ApplicationListener的说明 1 ApplicationListener简介

ApplicationContext事件机制是属于设计模式中的观察者设计模式,通过ApplicationEvent类和ApplicationListener接口实现事件处理.

当容器中有一个ApplicationListener对象, 当ApplicationContext发布ApplicationEvent事件时,ApplicationListener对象会被自动触发, 需要由程序来控制. 此外Spring中也内置了一下事件.

内置事件说明
ContextRefreshedEventApplicationContext 被初始化或刷新时,该事件被发布。这也可以在 ConfigurableApplicationContext接口中使用 refresh() 方法来发生。此处的初始化是指:所有的Bean被成功装载,后处理Bean被检测并激活,所有Singleton Bean 被预实例化,ApplicationContext容器已就绪可用
ContextStartedEventConfigurableApplicationContext (ApplicationContext子接口)接口中的 start() 方法启动 ApplicationContext 时,该事件被发布。你可以调查你的数据库,或者你可以在接受到这个事件后重启任何停止的应用程序
ContextStoppedEventConfigurableApplicationContext 接口中的 stop() 停止 ApplicationContext 时,发布这个事件。你可以在接受到这个事件后做必要的清理的工作
ContextClosedEventConfigurableApplicationContext 接口中的 close() 方法关闭 ApplicationContext 时,该事件被发布。一个已关闭的上下文到达生命周期末端;它不能被刷新或重启
RequestHandledEvent是 web-specific 事件,告诉所有 bean HTTP 请求已经被服务处理。只能应用于使用DispatcherServlet的Web应用。在使用Spring作为前端的MVC控制器时,当Spring处理用户请求结束后,系统会自动触发该事件
2 ApplicationListener案列 1 准备一个SpringBoot 环境 2 创建一个自定义的监听器
@Component
public class DemoApplicationListener implements ApplicationListener {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println(event);
        System.out.println("TestApplicationListener............................");
    }
}

根据上述可知, ContextRefreshedEvent内置事件, 是ApplicationContext 被初始化或刷新时会发布, 即监听器可以收到回调信息.

3 启动项目,查看日志

3 关于ApplicationContext的说明 1ApplicationContext的简介

从上述可知ApplicationContext具有发布事件的能力, 是从ApplicationEventPublisher接口继承来的. 而Spring中的事件使用,需要继承ApplicationEvent类或ApplicationContextEvent抽象类,抽象类中只有一个构造函数,且带有一个Object类型的参数作为事件源,且该事件源不能为null,因此我们需要在自己的构造函数中执行super(Object)。

public class EventObject implements java.io.Serializable {

    private static final long serialVersionUID = 5516075349620653480L;

    
    protected transient Object  source;

    
    public EventObject(Object source) {
        if (source == null)
            throw new IllegalArgumentException("null source");

        this.source = source;
    }
	....
}    
2ApplicationContext的案列 1 准备一个SpringBoot 环境
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

        testEvent();
    }

//    @Bean
//    public FeignInterceptor feignInterceptor(){
//        return new FeignInterceptor();
//    }

    // 测试事件
    public static void testEvent() {
        ApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class);
        DemoEvent demoEvent = new DemoEvent(context, "小明", 20);
        context.publishEvent(demoEvent);
    }
}

2 创建一个自定义的监听器
@Component
public class Demo2ApplicationListener implements ApplicationListener {
    @Override
    public void onApplicationEvent(ApplicationEvent event) {

        // 针对自定义事件做处理
        if (event instanceof DemoEvent) {
            System.out.println(event);
            DemoEvent demoEvent = (DemoEvent) event;
            System.out.println("姓名:" + demoEvent.getUsername() + ",年龄: " + demoEvent.getAge());

            System.out.println("自定义DemoEvent事件............................");
        }
    }
}

3 创建一个自定义的事件
public class DemoEvent extends ApplicationEvent {

    private String username;
    private int age;
    
    
    public DemoEvent(Object source, String username, int age) {
        super(source);
        this.username = username;
        this.age = age;
    }


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
4启动项目,查看日志

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/309471.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号