String host = "https://dfsns.market.alicloudapi.com";
String path = "/data/send_sms";
String method = "POST";
String appcode = "你自己的AppCode";
1.将需要被抽取的属性添加到配置文件并自定义前缀
aliyun:
sms:
host: "https://dfsns.market.alicloudapi.com"
path: "/data/send_sms"
method: "POST"
appcode: "你自己的AppCode"
2.将需要被抽取的属性创建配置类
//@Configuration 此注解标注的配置类,项目启动时一定会初始化对象添加到容器中
@ConfigurationPropertes(prefix="aliyun.sms")
@Data
public class SmsProperties implements InitializingBean{
private String host ;
private String path ;
private String method ;
private String appcode ;
public static String HOST;
public static String PATH;
public static String METHOD;
public static String APPCODE;
//对象初始化后调用
@Override
public void afterPropertiesSet() throws Exception {
SmsProperties.HOST=this.host;
SmsProperties.PATH=this.path;
SmsProperties.METHOD=this.method;
SmsProperties.APPCODE=this.appcode;
}
}
3.在需要使用他们的业务类上开启启用配置
@Service
@EnableConfigurationProperties(SmsProperties.class)
public class SmsServiceImpl implements SmsService{
String host = SmsProperties.HOST;
String path = SmsProperties.PATH;
String method = SmsProperties.METHOD;
String appcode = SmsProperties.APPCODE;
}