Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

2.44. Spring boot with starter

spring-boot-starter-xxxxx 是 Spring boot 子模組,開發中我們可以根據自己的需求開引用所需的功能,這樣不必引用所有的 Spring boot 依賴包。

我們也可以開發自己的 starter 模組和自定義註解,將我們的項目化整為零,模組化,隨時根據項目的需要引用,並且可以使用自定義註解啟用它們。

2.44.1. 實現 starter

2.44.1.1. Maven pom.xml 依賴包

			
<?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>
			
			
			

2.44.1.2. 配置檔案處理

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 + "]";
	}

}			
			
			

2.44.1.3. 自動配置檔案

			
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);
	}
}			
			
			

2.44.1.4. 啟用 starter 的自定義註解

			
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 {

}			
			
			

2.44.2. 引用 starter

2.44.2.1. Maven pom.xml 引入依賴

			
		<dependency>
			<groupId>cn.netkiller</groupId>
			<artifactId>spring-boot-starter-customize</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>			
			
			

完整的 pom.xml 檔案

			
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>cn.netkiller</groupId>
		<artifactId>parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>cn.netkiller</groupId>
	<artifactId>spring-boot-starter-customize-test</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-starter-customize-test</name>
	<url>http://maven.apache.org</url>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<dependencies>
		<dependency>
			<groupId>cn.netkiller</groupId>
			<artifactId>spring-boot-starter-customize</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
</project>			
			
			

2.44.2.2. 通過註解配置 starter

@EnableSms 啟用自動配置短信發送模組

			
package cn.netkiller.starter.customize.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import cn.netkiller.autoconfigure.EnableSms;
import cn.netkiller.sms.SmsSender;

@SpringBootApplication
@EnableSms
public class Application {
	public static void main(String[] args) {
		System.out.println("Hello World!");

		ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

		SmsSender smsSender = applicationContext.getBean(SmsSender.class);
		smsSender.send("驗證碼發送成功!");
	}
}			
			
			

2.44.2.3. 測試運行結果

			
Hello World!

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.2.RELEASE)

2020-08-02 20:51:54.564  INFO 43216 --- [           main] c.n.starter.customize.test.Application   : Starting Application on MacBook-Pro-Neo.local with PID 43216 (/Users/neo/git/springcloud/spring-boot-starter-customize-test/target/classes started by neo in /Users/neo/git/springcloud/spring-boot-starter-customize-test)
2020-08-02 20:51:54.567  INFO 43216 --- [           main] c.n.starter.customize.test.Application   : No active profile set, falling back to default profiles: default
2020-08-02 20:51:55.349  INFO 43216 --- [           main] c.n.starter.customize.test.Application   : Started Application in 1.539 seconds (JVM running for 1.942)
SmsProperties [url=https://sms.netkiller.cn/v1, username=netkiller, password=passw0rd]
驗證碼發送成功!