简体   繁体   English

为什么application.yml中的user.name属性值错误?

[英]Why the value of user.name property in application.yml is wrong?

That is a simple problem. 那是一个简单的问题。 I hava a application.yml : 我有一个application.yml

server:
  port: 852

user:
  name: Jack
  name1: Tom
  description: ${user.name}

And a User Java class: 和一个用户Java类:

@Component
@ConfigurationProperties(prefix = "user")
public class User {

    private String name;
    private String description;
    private String name1;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getName1() {
        return name1;
    }
    public void setName1(String name1) {
        this.name1 = name1;
    }   
}

And finally there is a MVC Controller java class: 最后是一个MVC Controller java类:

@RestController
public class HelloController {
    @Autowired User user;   

    @RequestMapping("/user")
    public String getUser() {
        return user.getName() + "--------" + user.getDescription() + "----" + user.getName1();
    }   
}

Ok,there is seem correct, but when entered "localhost:852/user" at browser address,I got wrong result as follow: 好的,似乎正确,但是在浏览器地址输入“ localhost:852 / user”时,出现以下错误结果:

BG221726--------BG221726----Tom

Note that,"BG221726" is my computer name! 请注意,“ BG221726”是我的计算机名称! What's wrong with my code???? 我的代码有什么问题????

You are using, effectively, user.name which is a special system property set by java. 您正在有效地使用user.name ,它是java设置的特殊系统属性 And when using the property resolving mechanism system properties take precedence over the ones loaded from property files, that one is taken. 当使用属性解析机制时,系统属性优先于从属性文件加载的属性,系统属性被优先使用。

To overcome use a different prefix. 要克服,请使用其他前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM