當(dāng)前位置:
                    首頁 >
                            前端技术
>                            javascript
>内容正文                
                        
                    javascript
SpringBoot项目中,获取配置文件信息
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                SpringBoot项目中,获取配置文件信息
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                1.在配置文件中設(shè)置信息,格式如下
wechat:mpAppId: wxdf2b09f280e6e6e2mpAppSecret: f924b2e9f140ac98f9cb5317a8951c71如果是多級目錄,則
project:url:sell: http://localhost:80802.獲取配置文件信息(三種方法)
2.1@ConfigurationProperties
package com.xiong.sell.config;import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;@Data @Component @ConfigurationProperties(prefix = "wechat") public class WechatAccountConfig {private String mpAppId;private String mpAppSecret; }2.2@Value
package com.xiong.sell.config;import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;@Component @Data //@ConfigurationProperties(prefix = "project.url") public class ProjectUrlConfig {@Value("${project.url.sell}")private String sell; }單元測試結(jié)果
2.3org.springframework.core.env.Environment;
package com.xiong.sell.config;import lombok.extern.slf4j.Slf4j; 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.core.env.Environment; import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @Slf4j public class ConfigTest {@Autowiredprivate Environment environment;@Testpublic void test2(){String sell = environment.getProperty("project.url.sell");log.info("project.url.sell = {}",sell);} }?
單元測試結(jié)果
?
轉(zhuǎn)載于:https://www.cnblogs.com/xzmxddx/p/10331502.html
總結(jié)
以上是生活随笔為你收集整理的SpringBoot项目中,获取配置文件信息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Webpack - 自学笔记
- 下一篇: 命名空间和程序集
