springboot nacos @NacosConfigListener不起作用无法监听配置改动

我一开始的代码

@Component
@Data
@NacosPropertySource(dataId = "config",autoRefreshed = true,type = ConfigType.YAML)
public class SystemConfig {
    @NacosValue(value = "${market_data_collector.ip:127.0.0.1}",autoRefreshed = true)
    private String marketDataCollectorIP;
    @NacosValue(value = "${market_data_collector.port:12001}",autoRefreshed = true)
    private int marketDataCollectorPort;

    @NacosConfigListener(dataId = "config")
    private void onMessage(String msg){
        System.out.println("配置变动"+msg);
    }
}

配置参数能够正常注入进去,但是配置改动的监听没有监听到。后来和之前学习写的demo做对比,发现了如果我把这个监听的函数修饰符使用private则会导致监听无法生效,改为public则正常

@Component
@Data
@NacosPropertySource(dataId = "config",autoRefreshed = true,type = ConfigType.YAML)
public class SystemConfig {
    @NacosValue(value = "${market_data_collector.ip:127.0.0.1}",autoRefreshed = true)
    private String marketDataCollectorIP;
    @NacosValue(value = "${market_data_collector.port:12001}",autoRefreshed = true)
    private int marketDataCollectorPort;

    @NacosConfigListener(dataId = "config")
    public void onMessage(String msg){
        System.out.println("配置变动"+msg);
    }
}

结果:
在这里插入图片描述

posted @ 2021-03-11 11:02  HumorChen99  阅读(33)  评论(0编辑  收藏  举报  来源