【问题标题】:RefreshScopeRefreshedEvent listener not getting called未调用 RefreshScopeRefreshedEvent 侦听器
【发布时间】:2019-01-26 01:10:01
【问题描述】:
@SpringBootApplication
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

@Component
class ApplicationEventListener implements ApplicationListener<EnvironmentChangeEvent> {
    @Override
    public void onApplicationEvent(EnvironmentChangeEvent event) {
        System.out.println("event listener");
    }
}

@Component
class RefreshEventListener implements ApplicationListener<RefreshScopeRefreshedEvent> {

    @Override
    public void onApplicationEvent(final RefreshScopeRefreshedEvent event) {
        System.out.println("refresh scope refresh event");
    }

}
}

@Component
@ConfigurationProperties("person")
@RefreshScope
public class PersonConfiguration {
    public String name;
//getter and setter for name

}

尝试测试是否在 RefreshScopeRefreshedEvent 监听器上调用了 onApplicationEvent。

更新了 git 中的配置,但未调用 RefreshScopeRefreshedEvent 侦听器。但是虽然调用了 EnvironmentChangeEvent 侦听器。

尝试调用/actuator/refresh,即使这里没有调用RefreshScopeRefreshedEvent监听器,但是调用了EnvironmentChangeEvent监听器。

这就是我在 git 中的配置方式

person:
    name: first last 

多次更新配置,每次只调用 EnvironmentChangeEvent。

我应该在此处更改什么以调用 RefreshScopeRefreshedEvent 侦听器。

【问题讨论】:

    标签: spring spring-boot spring-cloud spring-cloud-config


    【解决方案1】:

    我刚刚遇到了以下类无法正常工作的类似问题:

        @Slf4j
        @Configuration
        public class RefreshEventListener {
    
            @EventListener(RefreshScopeRefreshedEvent.class)
            public void onRefresh(RefreshScopeRefreshedEvent event) {
                log.info("Refresh detected")       
            }
        }
    

    直到我在日志中注意到 spring 覆盖了那个 bean 之前,我无法弄清楚它为什么不起作用。

    2018-10-25 16:03:44.146 INFO 1 --- [main] o.s.b.f.s.DefaultListableBeanFactory:用不同的定义覆盖 bean 'refreshEventListener' 的 bean 定义:替换 [Generic bean: class [x.x.x.RefreshEventListener];范围=单例;摘要=假;懒惰初始化=假;自动线模式=0;依赖检查=0;自动接线候选=真;主要=假;工厂BeanName=空;工厂方法名=空;初始化方法名=空;销毁方法名=空;在 URL [jar:file:/app.jar!/BOOT-INF/classes!/x/x/x/RefreshEventListener.class]] 中定义为 [Root bean: class [null];范围=;摘要=假;懒惰初始化=假;自动线模式=3;依赖检查=0;自动接线候选=真;主要=假; factoryBeanName=org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; factoryMethodName=刷新事件监听器;初始化方法名=空; destroyMethodName=(推断);在类路径资源中定义[org/springframework/cloud/autoconfigure/RefreshAutoConfiguration.class]]

    RefreshEventListener bean 已在此处定义:https://github.com/spring-cloud/spring-cloud-commons/blob/master/spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshAutoConfiguration.java

    我将类名更改为“RefreshEventListener”以外的其他名称,它按预期工作。

    【讨论】:

      猜你喜欢
      • 2012-04-30
      • 2019-07-25
      • 1970-01-01
      • 2012-04-08
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多