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文件
import static org.junit.Assert.assertTrue;
import com.ruoyi.iot.aliyun.config.AliyunUserConstant;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Unit test for simple App.
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AliyunUserConstant.class)
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
System.out.println(AliyunUserConstant.accessKey);
assertTrue( true );
}
}
aliyun-config.properties文件
aliyun.accessKey=123
aliyun.accessSecret=123
aliyun.iotInstanceId=123
aliyun.clientId=123
aliyun.host=123
AliyunUserConstant.java文件
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = {"classpath:aliyun-config.properties"})
public class AliyunUserConstant {
public static String regionId = "";
//public static String accessKey = "";
public static String accessKey = "";
public static String accessSecret = "";
public static String productKey = "";
public static String deviceName = "";
public static String instanceId = "";
public static String clientId = "";
public static String host = "";
@Value("${aliyun.regionId}")
public void setRegionId(String regionId) {
AliyunUserConstant.regionId = regionId;
}
@Value("${aliyun.accessKey}")
public void setAccessKey(String accessKey) {
AliyunUserConstant.accessKey = accessKey;
}
@Value("${aliyun.accessSecret}")
public void setAccessSecret(String accessSecret) {
AliyunUserConstant.accessSecret = accessSecret;
}
@Value("${aliyun.productKey}")
public void setProductKey(String productKey) {
AliyunUserConstant.productKey = productKey;
}
@Value("${aliyun.deviceName}")
public void setDeviceName(String deviceName) {
AliyunUserConstant.deviceName = deviceName;
}
@Value("${aliyun.instanceId}")
public void setInstanceId(String instanceId) {
AliyunUserConstant.instanceId = instanceId;
}
@Value("${aliyun.clientId}")
public void setClientId(String clientId) {
AliyunUserConstant.clientId = clientId;
}
@Value("${aliyun.host}")
public void setHost(String host) {
AliyunUserConstant.host = host;
}
}
aliyun:
accessKey: 123
accessSecret: 123
iotInstanceId: 123
clientId: 123
host: 123
AliyunUserConstant .java文件
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
//以下ok
@PropertySource(value = {"classpath:application-aliyun-config.yml"},factory = YamlPropertySourceFactory.class)
public class AliyunUserConstant {
public static String regionId = "cn-shanghai";
//public static String accessKey = "LTAI5tP4G9fobpyPFUughQaZ";
public static String accessKey = "";
public static String accessSecret = "gNbmPE7AqfUvoDZdSXcduzvCBhLJc6";
public static String productKey = "hf3fsfEX3CF";
public static String deviceName = "test_device";
public static String instanceId = "iot-06z00fu20bxiv4s";
public static String clientId = "123456";
public static String host = "iot-06z00fu20bxiv4s.amqp.iothub.aliyuncs.com";
@Value("${aliyun.regionId}")
public void setRegionId(String regionId) {
AliyunUserConstant.regionId = regionId;
}
@Value("${aliyun.accessKey}")
public void setAccessKey(String accessKey) {
AliyunUserConstant.accessKey = accessKey;
}
@Value("${aliyun.accessSecret}")
public void setAccessSecret(String accessSecret) {
AliyunUserConstant.accessSecret = accessSecret;
}
@Value("${aliyun.productKey}")
public void setProductKey(String productKey) {
AliyunUserConstant.productKey = productKey;
}
@Value("${aliyun.deviceName}")
public void setDeviceName(String deviceName) {
AliyunUserConstant.deviceName = deviceName;
}
@Value("${aliyun.instanceId}")
public void setInstanceId(String instanceId) {
AliyunUserConstant.instanceId = instanceId;
}
@Value("${aliyun.clientId}")
public void setClientId(String clientId) {
AliyunUserConstant.clientId = clientId;
}
@Value("${aliyun.host}")
public void setHost(String host) {
AliyunUserConstant.host = host;
}
}
YamlPropertySourceFactory.java文件
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import java.io.IOException;
/**
* @author Erwin Feng
* @since 2020/8/13
*/
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
// 返回 yaml 属性资源
return new YamlPropertySourceLoader()
.load(resource.getResource().getFilename(), resource.getResource())
.get(0);
}
}
@ActiveProfiles(value = "aliyun-config") // 载入application-aliyun-config.yml文件