application.properties 里面的值

    1. server.port=10000
    2. common.name = zhangsan
    3. common.age = 10

    Java代码

    1. public static void main(String[] args) throws InterruptedException {
    2. ConfigurableApplicationContext applicationContext = SpringApplication.run(NacosConfigApplication.class, args);
    3. while (true) {
    4. //当动态配置刷新时,会更新到 Enviroment中,因此这里每隔一秒中从Enviroment中获取配置
    5. String userName = applicationContext.getEnvironment().getProperty("common.name");
    6. String userAge = applicationContext.getEnvironment().getProperty("common.age");
    7. System.err.println("common name :" + userName + "; age: " + userAge);
    8. TimeUnit.SECONDS.sleep(3);
    9. }
    10. }

    执行结果

    1. common name :zhangsan; age: 10