使用
@Component
@ConfigurationProperties
注解。
application.properties:
1 2
| girl.name=lucy girl.age=25
|
或
application.yml :
1 2 3
| girl: name: lucy age: 25
|
新建 GirlProperties.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| @Component @ConfigurationProperties( prefix = "girl") public class GirlProperties { private String name ; private Integer age ; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
|
新建HelloController:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @RestController public class HelloController { @Autowired private GirlProperties girlProperties ; @Value("${girl.name}") private String name ; @RequestMapping(value = "girl/name",method = RequestMethod.GET) public String girl() { return name; } @RequestMapping(value = "girl/name2",method = RequestMethod.GET) public String girl() { return girlProperties.getName(); } }
|