That is a simple problem.
I hava a application.yml
:
server:
port: 852
user:
name: Jack
name1: Tom
description: ${user.name}
And a User Java class:
@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:
@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:
BG221726--------BG221726----Tom
Note that,"BG221726" is my computer name! What's wrong with my code????
You are using, effectively, user.name
which is a special system property set by 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With