jApplicationListener<ContextRefreshedEvent>
时间: 2023-07-08 21:30:49 浏览: 45
`ApplicationListener<ContextRefreshedEvent>` 是 Spring Framework 提供的一个监听器接口,用于监听应用程序上下文(Application Context)的刷新事件(`ContextRefreshedEvent`)。当应用程序上下文初始化或刷新完成后,Spring 会发布一个 `ContextRefreshedEvent` 事件。你可以通过实现 `ApplicationListener<ContextRefreshedEvent>` 接口,并重写 `onApplicationEvent` 方法,在应用程序上下文刷新事件发生时执行一些操作。
例如,以下代码展示了如何在应用程序上下文刷新事件发生时打印一条日志:
```java
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
public class MyContextRefreshedListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
System.out.println("Application Context has been refreshed.");
}
}
```
请注意,`onApplicationEvent` 方法会在应用程序上下文(包括 Web 应用程序上下文)初始化或刷新完成后被调用,因此该方法可能会被调用多次。如果你只想在 Web 应用程序上下文初始化或刷新完成后执行操作,可以使用 `WebApplicationInitializer` 接口或 `ServletContextListener` 接口。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)