如今開發主要使用微服務框架springboot,在springboot中常常遇到讀取application.yml文件的情形。web
開發過程當中常常遇到要讀取application.yml文件中的屬性值,本文總結幾種讀取的方式,供參考。spring
我這裏使用的是springboot-2.1.2.RELEASE版本,這裏使用的是application.properties的配置方式,和使用application.yml的方式是同樣的。下面是application.properties文件的內容springboot
cn.com.my.test1=test1
cn.com.my.test2=test2
這種方式是spring最先提供的方式,經過@Value註解的方式,該註解用在屬性上,可是要求該屬性所在的類必需要被spring管理。app
package com.example.demo.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class TestController { @Value("${cn.com.my.test1}") private String test1; @Value("${cn.com.my.test2}") private String test2; @RequestMapping("/test1/test") @ResponseBody public String getTest(){ return "hello:"+test1+",test2:"+test2; } }
在標記有@Controller類中使用了帶有@Value註解的test1和test2的屬性,首先標記有@Controller註解即可以使該類被spring管理。其次,使用@Value標記了屬性,則能夠得到application.properties(application.yml)文件中的屬性,這裏使用${cn.com.my.test1},屬性的名稱必須是所有的名稱,測試結果以下,框架
@ConfigurationProperties註解是springboot提供的,在springboot中大量使用,下面看其用法,微服務
這裏須要定義一個類,測試
package com.example.demo.properties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "cn.com.my") public class ApplicationPro { private String test1; private String test2;
private String testName; //必須有set方法 public void setTest1(String test1) { this.test1 = test1; } //必須有set方法 public void setTest2(String test2) { this.test2 = test2; } public String getTest1() { return test1; } public String getTest2() { return test2; }
public void setTestName(String testName) {
this.testName = testName;
}
public String getTestName() {
return testName;
}
}
該類上使用了@ConfigurationProperties註解,且配置了prefix屬性,指定了要獲取屬性的前綴,這裏的前綴是cn.com.my,在類中定義的屬性名最好和application.properties文件中的一致,不過這種方式能夠採用稀疏匹配,把application.properties修改成下面的內容,this
cn.com.my.test1=test1 cn.com.my.test2=test2 cn.com.my.test-name="hello world"
另外,在ApplicationPro類上標記有@Component註解,標記該註解的意思是要把該類交給spring管理,也就是說要讓spring管理此類,其實也可使用其餘註解,如,@Service等spa
下面看測試類,code
package com.example.demo.controller; import com.example.demo.properties.ApplicationPro; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class TestController3 { @Autowired private ApplicationPro ap; @RequestMapping("test3/test") @ResponseBody public String getTest(){ return ap.getTest1()+","+ap.getTest2()+","+ap.getTestName(); } }
看測試結果,
從上面的結果能夠看出已經得到了application.properties文件中的值,而且得到了test-name的值。具體匹配規則能夠自行百度,這裏強烈建議配置文件中的屬性和類中的保持一致。
使用該註解在ApplicationPro類中便不須要使用@Component註解,
package com.example.demo.properties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; //@Component @ConfigurationProperties(prefix = "cn.com.my") public class ApplicationPro { private String test1; private String test2; private String testName; //必須有set方法 public void setTest1(String test1) { this.test1 = test1; } //必須有set方法 public void setTest2(String test2) { this.test2 = test2; } public String getTest1() { return test1; } public String getTest2() { return test2; } public void setTestName(String testName) { this.testName = testName; } public String getTestName() { return testName; } }
再看啓動類,在啓動類上標記了@EnableConfigurationProperties({ApplicationPro.class}),也就是使@ConfigurationProperties註解生效,並標記了標有@ConfigurationProperties註解的類Application.class
package com.example.demo; import com.example.demo.properties.ApplicationPro; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; @SpringBootApplication @EnableConfigurationProperties({ApplicationPro.class}) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
下面看測試結果,
使用Environment對象,該對象是spring提供的一個對象,且是spring內部建立的對象,
package com.example.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class TestController2 { @Autowired private Environment environment; @RequestMapping("/test2/test") @ResponseBody public String getTest(){ return "hello,"+environment.getProperty("cn.com.my.test1")+","+"test2:"+environment.getProperty("cn.com.my.test2"); } }
能夠看到,能夠直接注入該對象的實例,經過其getProperty方法得到相應的屬性值。
本文總結了,在使用springboot的過程當中獲取配置文件中的幾種方式,
@Value
@ConfigurationProperties
Environment對象
有不當之處,歡迎指正,謝謝。