简体   繁体   English

Spring SmartLifeCycle似乎不适用于后期构造

[英]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.

相关问题 嵌套列表中的属性发布在 spring mvc 中似乎不起作用 - Post of properties in a nested list does not seem to work in spring mvc Spring Boot Autowired 和 Post Construct 如何工作? - How Spring Boot Autowired and Post Construct work? Spring portlet mvc: @Valid 似乎不起作用 - Spring portlet mvc: @Valid does not seem to work Spring SmartLifecycle start() 方法未执行 [已解决] - Spring SmartLifecycle start() method not executed [resolved] Spring CorsFilter似乎在预检请求中仍然收到401仍然无法正常工作 - Spring CorsFilter does not seem to work still receiving a 401 on preflight requests 在Spring和init-method中嵌套抽象bean似乎不起作用 - Nesting abstract beans in Spring and init-method does not seem to work ExpireAfterWrite似乎不起作用 - ExpireAfterWrite does not seem to work 黄瓜+弹簧:如何通过测试触发SmartLifecycle事件? - Cucumber + spring: How to trigger SmartLifecycle events through tests? 为什么Spring的MessageDigestPasswordEncoder将`{}`放入盐中? 其javadoc中的示例似乎不起作用 - Why does Spring's MessageDigestPasswordEncoder take `{}` into the salt? The example in its javadoc does not seem to work 为什么此正则表达式似乎不起作用? - Why does this regex not seem to work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM