[英]Spring SmartLifeCycle does not seem to work with post construct
I am using Spring SmartLifeCycle to specify bean order as follows: 我正在使用Spring SmartLifeCycle来指定bean顺序,如下所示:
public class MyBean implements SmartLifecycle {
@PostConstruct
public void init() {
//Do stuff
}
private boolean isRunning = false;
@Override
public boolean isAutoStartup() {
LOGGER.warn("************** is autostartp");
return true;
}
@Override
public void stop(final Runnable callback) {
stop();
callback.run();
}
@Override
public void start() {
LOGGER.warn("************** start ");
isRunning = true;
}
@Override
public void stop() {
LOGGER.warn("************** stop");
isRunning = false;
}
@Override
public boolean isRunning() {
LOGGER.warn("************** is running" + isRunning);
return isRunning;
}
@Override
public int getPhase() {
LOGGER.warn("************** phase " + (Integer.MIN_VALUE));
return Integer.MIN_VALUE;
}
}
The order does not seem to be running correctly. 该订单似乎无法正常运行。 All the beans seem to be constructed before the phase is even considered, which is causing by application to fail startup.
所有bean似乎都在考虑该阶段之前就已构建,这是由应用程序启动失败引起的。
How can I fix this and correctly specify the order? 如何解决此问题并正确指定顺序? Am I using this incorrectly?
我使用不正确吗?
Looks like you are missing the getPhase() method. 好像您缺少getPhase()方法。 This is used to control the order that the beans are instantiated.
这用于控制实例化bean的顺序。 During shutdown, the order is reversed.
在关机期间,顺序相反。
Any beans that don't have an explicit phase are either assigned phase 0 (most common) or given a phase by Spring (if it's something managed by Spring like a JMS listener). 没有显式阶段的任何bean都会由Spring分配阶段0(最常见),或者被赋予一个阶段(如果它像JMS侦听器一样由Spring管理)。
Unless you explicitly assign a phase, Spring will make a "best guess" which may not yield the desired result. 除非您明确分配一个阶段,否则Spring将做出“最佳猜测”,这可能不会产生期望的结果。
@Override
public int getPhase() {
return 1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.