无法在 Spring Boot 中拦截 ApplicationEnvironmentPreparedEvent [英] Unable to intercept ApplicationEnvironmentPreparedEvent in Spring Boot

查看:86
本文介绍了无法在 Spring Boot 中拦截 ApplicationEnvironmentPreparedEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以编程方式设置一些系统属性,我认为最好的方法是在 ApplicationEnvironmentPreparedEvent 被拦截后在事件侦听器中进行设置.但问题是我无法在侦听器中捕获该事件.

I need to set some system properties programmatically and I figured that the best way would be doing it in the event listener once ApplicationEnvironmentPreparedEvent was intercepted. But the problem is that I am not able to catch that event in my listener.

@Component
public class AppListener {

    @EventListener
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationEnvironmentPreparedEvent) {
            System.setProperty("java.util.concurrent.ForkJoinPool.common.threadFactory", MyThreadFactory.class.getName());
        }
    }

}

我做错了什么,为什么我无法捕捉到那个事件?

What am I doing wrong and why I cannot catch that event?

推荐答案

来自 Spring Boot 文档:

有些事件实际上是在ApplicationContext被触发之前触发的创建,因此您无法将侦听器注册为@Bean.你可以使用 SpringApplication.addListeners(… ) 方法注册它们或 SpringApplicationBuilder.listeners(… ) 方法.

Some events are actually triggered before the ApplicationContext is created, so you cannot register a listener on those as a @Bean. You can register them with the SpringApplication.addListeners(…​) method or the SpringApplicationBuilder.listeners(…​) method.

并且来自 Spring文档(适用于 5.1.7 版):

And from the Spring documentation (this for version 5.1.7):

@EventListener 注解的处理是通过内部进行的自动注册的 EventListenerMethodProcessor bean当使用 Java 配置或手动通过或元素时使用 XML 配置.

Processing of @EventListener annotations is performed via the internal EventListenerMethodProcessor bean which gets registered automatically when using Java config or manually via the or element when using XML config.

所以ApplicationEnvironmentPreparedEvent(以及所有的Spring Boot应用事件)发生在context和bean创建之前,并且@EventListener是由bean控制的,所以此时@EventListener注解无法被拾取.您必须专门创建一个侦听器类并显式添加它以捕获此事件(或在创建 bean 之前发生的任何事件).

So ApplicationEnvironmentPreparedEvent (and all of the Spring Boot application events) occur before context and bean creation, and @EventListener is controlled by a bean, so the @EventListener annotation cannot be picked up at this point. You will have to specifically create a listener class and add it explicitly to capture this event (or any event that occurs before bean creation).

你的班级:

public class AppListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent>
{
    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event)
    {
        System.setProperty("java.util.concurrent.ForkJoinPool.common.threadFactory",
                MyThreadFactory.class.getName());
    }
}

...以及相关的 SpringApplicationBuilder 代码:

...and the relevant SpringApplicationBuilder code:

ConfigurableApplicationContext context = new SpringApplicationBuilder(Launcher.class)
            .listeners(new AppListener()).run();

这篇关于无法在 Spring Boot 中拦截 ApplicationEnvironmentPreparedEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆