【Spring】容器

 定制bean特性:https://docs.spring.io/spring-framework/reference/core/beans/factory-nature.html

Lifecycle Callbacks:The InitializingBean and DisposableBean callback interfaces、Custom init() and destroy() methods、 @PostConstruct and @PreDestroy annotations

  执行顺序:Multiple lifecycle mechanisms configured for the same bean, with different initialization methods, are called as follows:

    1. Methods annotated with @PostConstruct

    2. afterPropertiesSet() as defined by the InitializingBean callback interface

    3. A custom configured init() method

  Destroy methods are called in the same order:

    1. Methods annotated with @PreDestroy

    2. destroy() as defined by the DisposableBean callback interface

    3. A custom configured destroy() method

Shutting Down the Spring IoC Container Gracefully in Non-Web Applications

    If you use Spring’s IoC container in a non-web application environment (for example, in a rich client desktop environment), register a shutdown hook with the JVM. Doing so ensures a graceful shutdown and calls the relevant destroy methods on your singleton beans so that all resources are released. You must still configure and implement these destroy callbacks correctly.

To register a shutdown hook, call the registerShutdownHook() method that is declared on the ConfigurableApplicationContext interface, as the following example shows:

public final class Boot {

    public static void main(final String[] args) throws Exception {
        ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        // add a shutdown hook for the above context...
        ctx.registerShutdownHook();
        // app runs here...
        // main method exits, hook is called prior to the app shutting down...
    }
}

 Aware接口

当一个类实现这些接口时,将具有感知有spring容器的能力。意味着容器将通过相同的回调方法向该类提供一些特定信息


容器扩展点:

BeanPostProcessor:   https://docs.spring.io/spring-framework/reference/core/beans/factory-extension.html

      

事件

基于发布-订阅模式

发送事件   ApplicationEventPublisher::publishEvent(ApplicationEvent event)

事件(Event):ApplicationEvent类继承了JDK中的事件基类EventObject。因而自定义容器内事件除了需要继承ApplicationEvent之外,还要传入事件源作为构造参数。

常用事件:if (applicationEvent instanceof ContextClosedEvent) 

  • ContextRefreshedEvent:上下文更新事件,该事件会在 ApplicationContext 被初始化或者更新时发布。也可以在调用 ConfigurableApplicationContext 接口中的 refresh() 方法时被触发。
  • ContextStartedEvent:上下文开始事件,当容器调用ConfigurableApplicationContext 的 Start() 方法开始/重新开始容器时触发该事件。
  • ContextStoppedEvent:上下文停止事件, 当容器调用ConfigurableApplicationContext 的 Stop() 方法停止容器时触发该事件。// 可以执行一些资源释放清理
  • ContextClosedEvent:上下文关闭事件,当 ApplicationContext 被关闭时触发该事件。容器被关闭时,其管理的所有单例 Bean 都被销毁。// 可以执行一些资源释放和关闭连接
  • RequestHandledEvent:请求处理事件,在 Web 应用中,当一个http请求(request)结束触发该事件。

实现有2种方式:实现接口(基于ApplicationListener) 和 基于注解(使用@EventListener)

1、基于接口实现自定义事件:继承ApplicationEvent

public class CustomEvent extends ApplicationEvent {
    public CustomEvent(Object source, final String msg) {
        super(source);
        System.out.println("Created a Custom event");
    }
}

创建事件监听类:

public class CustomEventListener extends ApplicationEventListener<CustomEvent> {
@Override
public void onApplicationEvent(customEvent event) {
System.out.println("Receive custom event: " + event.toString);
} }

2、基于注解实现自定义事件

创建监听器类,方法上使用@EventListener注解

@Component
public class AnnotationCustomEventListener {

    @EventListener
    public void onApplicationEvent(customEvent event) {
       System.out.println("Receive custom event: " + event.toString);
    }
}

 

发布事件:实现ApplicationEventPublisherware接口,实现发送自定义应用事件

@Component
public class EventPublisher implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    //发布事件
    public void publishEvent(ApplicationEvent event) {
        applicationContext.publishEvent(event);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

 

posted @ 2023-03-24 20:44  飞翔在天  阅读(9)  评论(0编辑  收藏  举报