繁体   English   中英

将初始化代码添加到 Spring 引导应用程序的正确方法是什么?

[英]What's the proper way to add initialization code to a Spring Boot application?

TLDR:我希望我的 Spring 引导应用程序在启动时运行一些初始化代码。 代码需要访问 Spring bean 和值。

我正在编写一个 Spring 引导应用程序,它将同时使用队列中的多条消息。 为此,它需要实例化多个消费者对象。 Spring 是否有一种很好的方法来实例化相同 class 的可配置数量的实例?

我必须使用的队列客户端充当线程池。 它为我给它的每个消费者 object 创建一个线程。 消费者对象一次只接收一条消息,他们必须完全处理并确认该消息,然后才能接收另一条消息。 消费者不是线程安全的,所以我不能只使用 singleton 实例。

我考虑了下面的方法,但对我来说感觉不对。 这似乎是对@Component注释的滥用,因为Initializer实例在构造后未使用。 有什么更好的方法呢?

@Component
public class Initializer {

    public Initializer(ConsumerRegistry registry, @Value("${consumerCount}") int consumerCount) {
        for (int i = 0; i < consumerCount; i++) {
            // Each registered consumer results in a thread that consumes messages.
            // Incoming messages will be delivered to any consumer thread that's not busy.
            registry.registerConsumer(new Consumer());
        }
    }

}

ApplicationListener将满足您的需要。 它会在注册的事件上得到通知,例如当 ApplicationContext 准备好时。 您将拥有对所有 Bean 和注入的完全访问权限。

@Component
public class StartupApplicationListener implements ApplicationListener<ApplicationReadyEvent> {

    @Inject
    private ConsumerRegistry registry;

    @Inject
    @Value("${consumerCount}")
    private int consumerCount;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        //do your logic
        for (int i = 0; i < consumerCount; i++) {
            // Each registered consumer results in a thread that consumes messages.
            // Incoming messages will be delivered to any consumer thread that's not busy.
            registry.registerConsumer(new Consumer());
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM