项目示例已上传至GitHub,可见github项目地址。
如果你还不了解SpringBoot的自动配置原理可以参考另一篇博文SpringBoot源码解析-自动配置原理
命名规范:
官方命名空间
自定义命名空间
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.kellen5l.toolgroupId>
<artifactId>sms-send-spring-boot-starterartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>com.kellen5l.toolgroupId>
<artifactId>sms-send-spring-boot-starter-autoconfigurerartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
dependencies>
project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.20.RELEASEversion>
<relativePath/>
parent>
<groupId>com.kellen5l.toolgroupId>
<artifactId>sms-send-spring-boot-starter-autoconfigurerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>sms-send-spring-boot-starter-autoconfigurername>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>com.github.qcloudsmsgroupId>
<artifactId>qcloudsmsartifactId>
<version>1.0.6version>
dependency>
dependencies>
project>
public class SmsSendService {
SmsSendProperties smsSendeProperties;
public SmsSendProperties getSmsSendeProperties() {
return smsSendeProperties;
}
public void setSmsSendeProperties(SmsSendProperties smsSendeProperties) {
this.smsSendeProperties = smsSendeProperties;
}
public void smSingleSend(String phoneNumber, String[] params) {
try {
SmsSingleSender ssender = new SmsSingleSender(smsSendeProperties.getAppid(), smsSendeProperties.getAppkey());
SmsSingleSenderResult result = ssender.sendWithParam("86", phoneNumber,
smsSendeProperties.getTemplateId(), params, smsSendeProperties.getSmsSign(), "", "");
System.out.println(result);
} catch (HTTPException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@ConfigurationProperties("kellen5l.sms") //将配置与这个前缀绑定
public class SmsSendProperties {
private int templateId;
private int appid;
private String appkey;
private String smsSign;
...
Getter and Setter
}
@Configuration
@ConditionalOnWebApplication //仅当项目为web项目是生效
@EnableConfigurationProperties(SmsSendProperties.class) //使配置类生效
public class SmsSendServiceAutoConfiguration {
@Autowired
SmsSendProperties smsSendProperties;
@Bean
public SmsSendService smsSendService(){
SmsSendService smsSendService=new SmsSendService();
smsSendService.setSmsSendeProperties(smsSendProperties);
return smsSendService;
}
}
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.kellen5l.tool.SmsSendServiceAutoConfiguration
@RestController
public class SmsController {
@Autowired
SmsSendService smsSendService;
@PostMapping("/sms")
public void sendSms(@RequestParam("phoneNumber") String phoneNumber){
String[] params={String.valueOf((int) (Math.random() * 9000 + 1000))}; //4位数字验证码
smsSendService.smSingleSend(phoneNumber,params);
}
}
kellen5l.sms.templateId=
kellen5l.sms.appid=
kellen5l.sms.appkey=
kellen5l.sms.smsSign=