今天嘗試了一下從application.yml獲取配置的常量,因為干接觸不是很熟所以記錄下來。
要注意的地方是 application.yml 中不能用駝峰式寫法(systemParams)要改成system-params
要導入依賴
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
application.yml
system-params: image-uri: http://xxxxxxSystemParams
package com.shior.demoshior.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @Author: zhugf * @Description: * @Date created in 2018/5/14 */ @Component @ConfigurationProperties(prefix = "system-params") public class SystemParams { private String imageUri; public String getImageUri() { return imageUri; } public void setImageUri(String imageUri) { this.imageUri = imageUri; } }
測試
package com.shior.demoshior.common; import com.shior.demoshior.config.SystemParams; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; /** * @Author: zhugf * @Description: * @Date created in 2018/5/14 */ @RunWith(SpringRunner.class) @SpringBootTest public class TestConfig { @Autowired SystemParams systemParams; @Test public void testParam(){ System.out.println(systemParams.getImageUri()); } }
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。