文章目录
前言
- junit载入类
- 配置类读取配置文件 - .properties- .yml
实现
junit测试
pom文件
<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- 自定义配置需要的依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency></dependencies>
AppTest.java文件
importstaticorg.junit.Assert.assertTrue;importcom.ruoyi.iot.aliyun.config.AliyunUserConstant;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringRunner;/**
* Unit test for simple App.
*/@RunWith(SpringRunner.class)@SpringBootTest(classes =AliyunUserConstant.class)publicclassAppTest{/**
* Rigorous Test :-)
*/@TestpublicvoidshouldAnswerWithTrue(){System.out.println(AliyunUserConstant.accessKey);assertTrue(true);}}
读取配置yml/properties
方法一 @PropertySource + properties
aliyun-config.properties文件
aliyun.accessKey=123
aliyun.accessSecret=123
aliyun.iotInstanceId=123
aliyun.clientId=123
aliyun.host=123
AliyunUserConstant.java文件
importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.PropertySource;importorg.springframework.stereotype.Component;@Component@PropertySource(value ={"classpath:aliyun-config.properties"})publicclassAliyunUserConstant{publicstaticString regionId ="";//public static String accessKey = "";publicstaticString accessKey ="";publicstaticString accessSecret ="";publicstaticString productKey ="";publicstaticString deviceName ="";publicstaticString instanceId ="";publicstaticString clientId ="";publicstaticString host ="";@Value("${aliyun.regionId}")publicvoidsetRegionId(String regionId){AliyunUserConstant.regionId = regionId;}@Value("${aliyun.accessKey}")publicvoidsetAccessKey(String accessKey){AliyunUserConstant.accessKey = accessKey;}@Value("${aliyun.accessSecret}")publicvoidsetAccessSecret(String accessSecret){AliyunUserConstant.accessSecret = accessSecret;}@Value("${aliyun.productKey}")publicvoidsetProductKey(String productKey){AliyunUserConstant.productKey = productKey;}@Value("${aliyun.deviceName}")publicvoidsetDeviceName(String deviceName){AliyunUserConstant.deviceName = deviceName;}@Value("${aliyun.instanceId}")publicvoidsetInstanceId(String instanceId){AliyunUserConstant.instanceId = instanceId;}@Value("${aliyun.clientId}")publicvoidsetClientId(String clientId){AliyunUserConstant.clientId = clientId;}@Value("${aliyun.host}")publicvoidsetHost(String host){AliyunUserConstant.host = host;}}
方法二 @PropertySource + yml + YamlPropertySourceFactory
aliyun:
accessKey: 123
accessSecret: 123
iotInstanceId: 123
clientId: 123
host: 123
AliyunUserConstant .java文件
importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.PropertySource;importorg.springframework.stereotype.Component;@Component//以下ok@PropertySource(value ={"classpath:application-aliyun-config.yml"},factory =YamlPropertySourceFactory.class)publicclassAliyunUserConstant{publicstaticString regionId ="cn-shanghai";//public static String accessKey = "LTAI5tP4G9fobpyPFUughQaZ";publicstaticString accessKey ="";publicstaticString accessSecret ="gNbmPE7AqfUvoDZdSXcduzvCBhLJc6";publicstaticString productKey ="hf3fsfEX3CF";publicstaticString deviceName ="test_device";publicstaticString instanceId ="iot-06z00fu20bxiv4s";publicstaticString clientId ="123456";publicstaticString host ="iot-06z00fu20bxiv4s.amqp.iothub.aliyuncs.com";@Value("${aliyun.regionId}")publicvoidsetRegionId(String regionId){AliyunUserConstant.regionId = regionId;}@Value("${aliyun.accessKey}")publicvoidsetAccessKey(String accessKey){AliyunUserConstant.accessKey = accessKey;}@Value("${aliyun.accessSecret}")publicvoidsetAccessSecret(String accessSecret){AliyunUserConstant.accessSecret = accessSecret;}@Value("${aliyun.productKey}")publicvoidsetProductKey(String productKey){AliyunUserConstant.productKey = productKey;}@Value("${aliyun.deviceName}")publicvoidsetDeviceName(String deviceName){AliyunUserConstant.deviceName = deviceName;}@Value("${aliyun.instanceId}")publicvoidsetInstanceId(String instanceId){AliyunUserConstant.instanceId = instanceId;}@Value("${aliyun.clientId}")publicvoidsetClientId(String clientId){AliyunUserConstant.clientId = clientId;}@Value("${aliyun.host}")publicvoidsetHost(String host){AliyunUserConstant.host = host;}}
YamlPropertySourceFactory.java文件
importorg.springframework.boot.env.YamlPropertySourceLoader;importorg.springframework.core.env.PropertySource;importorg.springframework.core.io.support.EncodedResource;importorg.springframework.core.io.support.PropertySourceFactory;importjava.io.IOException;/**
* @author Erwin Feng
* @since 2020/8/13
*/publicclassYamlPropertySourceFactoryimplementsPropertySourceFactory{@OverridepublicPropertySource<?>createPropertySource(String name,EncodedResource resource)throwsIOException{// 返回 yaml 属性资源returnnewYamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource()).get(0);}}
方法三 @ConfigurationProperties + yml
方法四 @ActiveProfiles + yml
参考
@ActiveProfiles(value ="aliyun-config")// 载入application-aliyun-config.yml文件
总结
本文转载自: https://blog.csdn.net/qq1113673178/article/details/126691904
版权归原作者 二进制怪兽 所有, 如有侵权,请联系我们删除。
版权归原作者 二进制怪兽 所有, 如有侵权,请联系我们删除。