| 知乎专栏 |
目录
spring-boot-starter-xxxxx 是 Spring boot 子模块,开发中我们可以根据自己的需求开引用所需的功能,这样不必引用所有的 Spring boot 依赖包。
我们也可以开发自己的 starter 模块和自定义注解,将我们的项目化整为零,模块化,随时根据项目的需要引用,并且可以使用自定义注解启用它们。
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>cn.netkiller</groupId>
<artifactId>spring-boot-starter-customize</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spring Boot Starter Project</name>
<parent>
<groupId>cn.netkiller</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>cn.netkiller.starter.App</start-class>
<java.version>11</java.version>
<lombok.version>1.16.18</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties 加入短信网关的配置项
sms.gateway.url=https://sms.netkiller.cn/v1 sms.gateway.username=netkiller sms.gateway.password=passw0rd
SmsProperties 用于读取前缀为 sms.gateway 的配置项。
package cn.netkiller.autoconfigure;
import org.springframework.boot.context.properties.ConfigurationProperties;
import lombok.Data;
@ConfigurationProperties(prefix = "sms.gateway")
@Data
public class SmsProperties {
private String url;
private String username;
private String password;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "SmsProperties [url=" + url + ", username=" + username + ", password=" + password + "]";
}
}
package cn.netkiller.autoconfigure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import cn.netkiller.sms.SmsSender;
@EnableConfigurationProperties(value = SmsProperties.class)
@Configuration
public class SmsAutoConfiguration {
@Autowired
private SmsProperties smsProperties;
@Bean
public SmsSender send() {
return new SmsSender(this.smsProperties);
}
}
package cn.netkiller.autoconfigure;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
import org.springframework.context.annotation.Import;
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({ SmsAutoConfiguration.class })
public @interface EnableSms {
}