RefreshScopeRefreshedEvent侦听器未调用

时间:2018-08-19 20:41:22

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

@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侦听器。

1 个答案:

答案 0 :(得分:0)

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

    @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信息1 --- [main] o.s.b.f.s.DefaultListableBeanFactory:用不同的定义覆盖bean'refreshEventListener'的bean定义:替换[Generic bean:class [x.x.x.RefreshEventListener];作用域=单个abstract = false; lazyInit = false; autowireMode = 0; dependencyCheck = 0; autowireCandidate = true; primary = false; factoryBeanName = null; factoryMethodName = null; initMethodName = null; destroyMethodName = null;在URL [jar:file:/app.jar!/ BOOT-INF / classes!/x/x/x/RefreshEventListener.class]中定义,并带有[Root bean:class [null]; scope =; abstract = false; lazyInit = false; autowireMode = 3; dependencyCheck = 0; autowireCandidate = true; primary = false; factoryBeanName = org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; factoryMethodName = refreshEventListener; initMethodName = null; 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'以外的其他名称,并且按预期方式工作。