赞
踩
1、读取application.ym文件配置直接在类属性上@Value("${application.yml里的字段}");
2、读取自定义Properties文件的配置:
在类上使用@PropertySource(可同时读取多个properties文件);
例子:
自定义两个properties文件:
wkx.properties:
wkx2.properties:
Prop实体类:
- @Component
- @PropertySource(value={"classpath:wkx.properties","classpath:wkx2.properties"})
- //@PropertySource("classpath:wkx.properties")
- public class Prop {
- @Value("${my.wkx.name}")
- private String name;
- @Value("${demo.password}")
- private String password;
- @Value("${my.wkx2.address}")
- private String address;
- public String getAddress() {
- return address;
- }
-
- public void setAddress(String address) {
- this.address = address;
- }
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- }
测试接口:
- @RestController
- @RequestMapping("/wkx1")
- public class PropController {
- @Autowired
- private Prop prop;
-
- @GetMapping("/test")
- public String show() {
- return "username" +":"+ prop.getName() +"--"+ "password" +":"+ prop.getPassword()+"--"+
- "address"+":"+prop.getAddress();
- }
- }
测试结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。