Spring系列之——springboot解析resources.application.properties文件
摘要:本文通過講解如何解析application.properties屬性,介紹了幾個註解的運用@Value @ConfigurationProperties @EnableConfigurationProperties @Autowired @ConditionalOnProperty
1 準備
1.1 搭建springboot
1.2 寫一個controller類
package com.gbm.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;View Codeimport org.springframework.web.bind.annotation.ResponseBody; /** * Created by Administrator on 2019/2/20. */ @Controller public class IndexController { @RequestMapping("/index") @ResponseBody public String index() { return "我愛北京天安門!"; } }
2 幾種獲取屬性值方式
配置application.properties
value.local.province=Zhejiang value.local.city=Hangzhou complex.other.province=Jiangsu complex.other.city=Suzhou complex.other.flag=falseView Code
2.1 使用註解@Value("${xxx}")獲取指定屬性值
2.1.1 在controller類中獲取屬性值
package com.gbm.controller; import org.springframework.beans.factory.annotation.Value;View Codeimport org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * Created by Administrator on 2019/2/20. */ @Controller public class IndexController { @Value("${value.local.province}") private String province; @Value("${value.local.city}") private String city; @RequestMapping("/index") @ResponseBody public String index() { StringBuffer sb = new StringBuffer(); sb.append(this.city); sb.append(","); sb.append(this.province); sb.append(" "); return sb.toString(); } }
2.1.2 任何瀏覽器上運行 http://localhost:8080/index,結果如下
2.2 使用註解@ConfigurationProperties(prefix = "xxx")獲得前綴相同的一組屬性,並轉換成bean對象
2.2.1 寫一個實體類
package com.gbm.models; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * Created by Administrator on 2019/2/21. */ @Component @ConfigurationProperties(prefix = "complex.other") public class Complex { private String province; private String city; private Boolean flag; public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public Boolean getFlag() { return flag; } public void setFlag(Boolean flag) { this.flag = flag; } @Override public String toString() { return "Complex{" + "province=‘" + province + ‘\‘‘ + ", city=‘" + city + ‘\‘‘ + ", flag=" + flag + ‘}‘; } }View Code
2.2.2 在controller類中獲取屬性值
package com.gbm.controller; import com.gbm.models.Complex; 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; /** * Created by Administrator on 2019/2/20. */ @Controller public class IndexController { @Autowired private Complex complex; @RequestMapping("/index") @ResponseBody public String index() { return complex.toString(); } }View Code
2.2.3 在SpringBootApplication中使Configuration生效
package com.gbm.myspingboot; import com.gbm.models.Complex; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScans; @SpringBootApplication @ComponentScans({@ComponentScan("com.gbm.controller"), @ComponentScan("com.gbm.models")}) @EnableConfigurationProperties(Complex.class) @ConditionalOnProperty(value = "complex.other.town", matchIfMissing = true) public class MyspingbootApplication { public static void main(String[] args) { SpringApplication.run(MyspingbootApplication.class, args); } }View Code
2.2.4 任何瀏覽器上運行 http://localhost:8080/index,結果如下
3 總結
關於解析application.properties文件,最重要的是對註解的使用,本文主要涉及到如下幾個註解的運用
@Value("${xxx}")——獲取指定屬性值
@ConfigurationProperties(prefix = "xxx")——獲得前綴相同的一組屬性,並轉換成bean對象
@EnableConfigurationProperties(xxx.class)——使Configuration生效,並從IOC容器中獲取bean
@Autowired——自動註入set和get方法
@ConditionalOnProperty(value = "complex.other.town", matchIfMissing = true)——缺少該property時是否可以加載,如果是true,沒有該property也會正常加載;如果是false則會拋出異常。例如
package com.gbm.myspingboot; import com.gbm.models.Complex; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScans; @SpringBootApplication @ComponentScans({@ComponentScan("com.gbm.controller"), @ComponentScan("com.gbm.models")}) @EnableConfigurationProperties(Complex.class) @ConditionalOnProperty(value = "complex.other.town", matchIfMissing = false) public class MyspingbootApplication { public static void main(String[] args) { SpringApplication.run(MyspingbootApplication.class, args); } }matchIfMissing=false代碼
Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug‘ enabled.
2019-02-21 23:30:56.532 ERROR 3152 --- [ main] o.s.boot.SpringApplication : Application run failed
Spring系列之——springboot解析resources.application.properties文件