【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:
-
Methods annotated with
@PostConstruct
-
afterPropertiesSet()
as defined by theInitializingBean
callback interface -
A custom configured
init()
method
-
Destroy methods are called in the same order:
-
Methods annotated with
@PreDestroy
-
destroy()
as defined by theDisposableBean
callback interface -
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; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)