Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

1.28. Spring boot with starter

spring-boot-starter-xxxxx 是 Spring boot 子模块,开发中我们可以根据自己的需求开引用所需的功能,这样不必引用所有的 Spring boot 依赖包。

我们也可以开发自己的 starter 模块和自定义注解,将我们的项目化整为零,模块化,随时根据项目的需要引用,并且可以使用自定义注解启用它们。

1.28.1. 实现 starter

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>
			
			
			

配置文件处理

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

启用 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 {

}			
			
			

1.28.2. 引用 starter

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>			
			
			

通过注解配置 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("验证码发送成功!");
	}
}			
			
			

测试运行结果

			
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]
验证码发送成功!