企业开发中,往往将常见工具类封装抽取,以简洁便利的方式供其他工程模块使用。而SpringBoot的自动装配机制可以方便的实现组件抽取。SpringBoot执行流程如下
- 扫描依赖模块中META-INF/spring.factories
- 执行装配类中方法
- 对象存入容器中
- 核心工程注入对象,调用方法使用
application.yml
1.需要在resources 写一个包是META-INF的spring.factories文件
2.配置类
@Data
@ConfigurationProperties(prefix = “tanhua.sms”)
public class SmsProperties {
private String signName;
private String templateCode;
private String accessKey;
private String secret;
}
然后是发短信的代码
在自动装配类
添加@EnableConfigurationProperties({SmsProperties.class})注解,和添加bean(SmsTemplate类加载的数据) 到spring容器