如何抽取到配置类?

文章讲述了如何配置和使用阿里云短信服务。首先,将服务属性如host、path、method和appcode写入配置文件。然后,创建一个配置类`SmsProperties`来绑定这些属性,并在项目启动时初始化。最后,在业务类`SmsServiceImpl`中启用配置,以便直接访问这些属性进行短信发送操作。
摘要由CSDN通过智能技术生成
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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值