Spring boot with starter

Spring boot with starter

Mr. Neo Chen (陈景峯), netkiller, BG7NYT

节选自《Netkiller Spring Cloud 手札》

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

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

实现 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 {

} 

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

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

netkiller-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值