gpt4 book ai didi

java - 在所有 SpringBeans 和 ApplicationContext 都被初始化之后调用方法

转载 作者:IT老高 更新时间:2023-10-28 13:53:25 25 4
gpt4 key购买 nike

我在一个复杂的 java 程序中有一个方法,需要在 web ApplicationContext 和 SpringBeans 初始化后立即调用。

我试过玩弄 <bean id="..." class="..." init-method="initialize">但是这个方法会调用 applicationContext.get().getBean(beanId);方法。

我想知道是否有人知道如何做到这一点。

谢谢。

最佳答案

从 Spring 4.2 开始,您可以使用注释将事件监听器附加到 Springs 生命周期事件(和您自己的)。只需将@EventListener 添加到方法中,并将事件类型作为第一个(也是唯一一个)参数包含在内,Spring 会自动检测它并将其连接起来。

https://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2

@Component
public class MyListener {

@EventListener
public void handleContextRefresh(ContextRefreshedEvent event) {
...
}
}

关于java - 在所有 SpringBeans 和 ApplicationContext 都被初始化之后调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3409605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com