Spring Boot 在配置上相比 Spring 要简单许多,其核心就在于 Spring Boot Starter。Spring Boot Starter 可以将模块所需的依赖根据环境自动配置。使用者只需要依赖相应功能的 Starter ,无需过多的配置和依赖,Spring Boot 就能自动扫描并加载相应的模块。
实现一个短信发送的模块(本文目的为学习 Starter ,因此只是模拟实现,调用之后打印一行日志)。
<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>2.6.7version>
parent>
<groupId>com.aichen.sms.springbootgroupId>
<artifactId>aichen-sms-spring-boot-starterartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>11maven.compiler.source>
<maven.compiler.target>11maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
dependency>
dependencies>
project>
package com.aichen.sms.springboot.service;
/**
* 爱辰短信发送
* @author aichen
* @date 2022/4/26 8:35
*/
public interface AichenSmsSend {
/**
* 发送短信
* @param phoneNum 手机号
* @param content 内容
*/
boolean send(String phoneNum, String content);
}
package com.aichen.sms.springboot.service.impl;
import com.aichen.sms.springboot.service.AichenSmsSend;
/**
* @author aichen
* @date 2022/4/26 8:38
*/
public class AichenSmsSendImpl implements AichenSmsSend {
@Override
public boolean send(String phoneNum, String content) {
System.out.println("发送成功,手机号:"+phoneNum+",内容:"+content);
return true;
}
}
package com.aichen.sms.springboot.config;
import com.aichen.sms.springboot.service.AichenSmsSend;
import com.aichen.sms.springboot.service.impl.AichenSmsSendImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 自动配置爱辰短信发送Bean
* @author aichen
* @date 2022/4/26 8:34
*/
@Configuration
@ConditionalOnClass({AichenSmsSend.class, AichenSmsSendImpl.class})
public class SmsAutoConfigure {
@Value("${aichen.sms.securitykey}")
private String securitykey;
@Bean
@ConditionalOnMissingBean
public AichenSmsSend aichenSmsSend(){
if (!"123456".equals(securitykey)){
throw new RuntimeException("爱辰短信服务注册失败,密钥不正确!");
}
return new AichenSmsSendImpl();
}
}
此配置文件的作用类似于Bean扫描,让Spring动态加载这些类。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.aichen.sms.springboot.config.SmsAutoConfigure
mvn clean install
<dependency>
<groupId>com.aichen.sms.springbootgroupId>
<artifactId>aichen-sms-spring-boot-starterartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
application.yml
aichen:
sms:
securitykey: 123456
package com.aichen.test.thymeleaf.controller;
import com.aichen.sms.springboot.service.AichenSmsSend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author aichen
* @date 2022/4/26 8:50
*/
@RestController
@RequestMapping("/aichen/sms")
public class AichenSmsTestController {
@Autowired
private AichenSmsSend aichenSmsSend;
@GetMapping("/send")
public String send(){
boolean send = aichenSmsSend.send("18888888888", "发财啦!");
return send ? "发送成功" : "发送失败";
}
}
以上 SmsAutoConfigure 中使用的 @Value(“${aichen.sms.securitykey}”) 引入配置的方式,不能指定默认值,依赖此 Starter 时必须在 配置文件里增加 aichen.sms.securitykey 的值,否则会报错。
换成以下方式可以给 aichen.sms.securitykey 增加默认值,当引用此 Starter 时不配置 aichen.sms.securitykey 会使用默认值。
使用实体接收配置文件的参数值:
package com.aichen.sms.springboot.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author aichen
* @date 2022/4/26 9:51
*/
@ConfigurationProperties(prefix = "aichen.sms")
public class AichenSmsProperties {
private String securitykey = "123456";
public String getSecuritykey() {
return securitykey;
}
public void setSecuritykey(String securitykey) {
this.securitykey = securitykey;
}
}
改造 SmsAutoConfigure 自动装配类:
增加 @EnableConfigurationProperties(AichenSmsProperties.class)
securitykey 的值从 实体配置 AichenSmsProperties 中获取。
package com.aichen.sms.springboot.config;
import com.aichen.sms.springboot.service.AichenSmsSend;
import com.aichen.sms.springboot.service.impl.AichenSmsSendImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 自动配置爱辰短信发送Bean
* @author aichen
* @date 2022/4/26 8:34
*/
@Configuration
@ConditionalOnClass({AichenSmsSend.class, AichenSmsSendImpl.class})
@EnableConfigurationProperties(AichenSmsProperties.class)
public class SmsAutoConfigure {
@Autowired
private AichenSmsProperties aichenSmsProperties;
@Bean
@ConditionalOnMissingBean
public AichenSmsSend aichenSmsSend(){
if (!"123456".equals(aichenSmsProperties.getSecuritykey())){
throw new RuntimeException("爱辰短信服务注册失败,密钥不正确!");
}
return new AichenSmsSendImpl();
}
}