一、前言
Spring Boot Starter 是一组预定义的依赖项集合,它们可以帮助开发者快速集成各种功能模块。这些 Starter 可以减少开发者的配置工作,使得开发者可以更加专注于业务逻辑的实现。
举例说明:例如引入MyBatis-Plus的stater,只需要在配置文件中配置即可使用
二、实现步骤
2.1 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>com.qiangesoft.demo</groupId>
<artifactId>qiangesoft-demo-starter</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>自定义starter</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>2.7.17</spring-boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2 属性类
java
package com.qiangesoft.demo.starter.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 属性类
*
* @author qiangesoft
* @date 2024-04-29
*/
@Data
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
/**
* 是否启用
*/
private boolean enabled = true;
/**
* 地址
*/
private String url;
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;
}
2.3 配置类
java
package com.qiangesoft.demo.starter.config;
import com.qiangesoft.demo.starter.client.DemoTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
/**
* 配置类
*
* @author qiangesoft
* @date 2024-04-29
*/
@AutoConfiguration
@EnableConfigurationProperties(DemoProperties.class)
@ConditionalOnClass(DemoTemplate.class)
@ConditionalOnProperty(prefix = "demo", name = "enabled", havingValue = "true")
public class DemoConfiguration {
@Autowired
private DemoProperties demoProperties;
@Bean
@ConditionalOnMissingBean
public DemoTemplate demoTemplate() {
return new DemoTemplate(demoProperties.getUrl(),
demoProperties.getUsername(),
demoProperties.getPassword());
}
}
2.4 客户端调用类
java
package com.qiangesoft.demo.starter.client;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
/**
* api服务
*
* @author qiangesoft
* @date 2024-04-29
*/
@Slf4j
public class DemoTemplate implements InitializingBean {
/**
* 地址
*/
private final String url;
/**
* 用户名
*/
private final String username;
/**
* 密码
*/
private final String password;
public DemoTemplate(String url, String username, String password) {
this.url = url;
this.username = username;
this.password = password;
log.info("DemoTemplate init with url[{}], username[{}] !", this.url, this.username);
}
@Override
public void afterPropertiesSet() throws Exception {
log.info("DemoTemplate init finished !");
}
public void execute() {
log.info("DemoTemplate.execute() executed !");
}
}
2.5 自动装配
创建spring.factories文件
java
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.qiangesoft.demo.starter.config.DemoConfiguration
三、测试
3.1 注意事项
项目搭建完成后,编译打包安装
重要: 如果打包后没有自动生成spring-configuration-metadata.json文件,是没有自动提示的。需要配置idea的Annotation Processors开启,然后再打包!
3.2 测试使用
引入依赖
xml
<dependency>
<groupId>com.qiangesoft.demo</groupId>
<artifactId>qiangesoft-demo-starter</artifactId>
<version>1.0.0</version>
</dependency>
配置
yaml
demo:
enabled: true
url: 111
username: 111
password: 111
api使用
java
@Autowired
private DemoTemplate demoTemplate;
@GetMapping
public void test(){
demoTemplate.execute();
}
测试