@Component
public class StartupListener {
private final Logger logger = LogManager.getLogger(getClass());
@EventListener(value = ContextRefreshedEvent.class, condition = "#event.source.displayName.startsWith(\"Root\")")
public void afterSpringStarted(ContextRefreshedEvent event) {
AbstractRefreshableConfigApplicationContext dd = (AbstractRefreshableConfigApplicationContext) event.getApplicationContext();
GateKeeperCentral gateKeeperCentral = dd.getBean("xxxx", GateKeeperCentral.class);
gateKeeperCentral.initContext();
}
}
1、 EventListener
ContextRefreshedEvent | 当ApplicationContext或者叫spring被初始化或者刷新initialized会触发该事件 |
ContextStartedEvent | spring初始化完,时触发 |
ContextStoppedEvent | spring停止后触发,一个停止了的动作,可以通过start() 方法从新启动 |
ContextClosedEvent | spring关闭,所有bean都被destroyed 掉了,这个时候不能被刷新,或者从新启动了 |
RequestHandledEvent | 请求经过DispatcherServlet 时被触发,在request完成之后 |
2、condition
可以使用SpEL表达式,就是当满足条件才执行