步骤:
目录结构:

Step 1、启动器模块(haha-spring-boot-starter)
启动器模块只需依赖于自动配置模块,无需做任何配置
新建Module,maven

其中pom文件
<?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.haha.starter</groupId>
<artifactId>haha-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<!--启动器-->
<dependencyManagement>
<dependencies>
<!--引入自动配置模块-->
<dependency>
<groupId>com.haha.starter</groupId>
<artifactId>haha-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Step 2、自动配置模块(haha-spring-boot-autoconfigurer)
(1)新建Module,Spring Initializr(引入web模块)

pom文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.haha.starter</groupId>
<artifactId>haha-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>haha-spring-boot-starter-autoconfigurer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- 所有starter的基本配置:spring-boot-starter-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
</project>
(2)创建于自动配置映射的properties类(HelloProperties)
package com.haha.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "haha.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
(3)声明组件(HelloService)
该类从HelloProperties类读取字符串的头、尾,一起和name组成新的字符串并返回
package com.haha.starter;
public class HelloService {
HelloProperties helloProperties;
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHelloToHaha(String name){
return helloProperties.getPrefix()+"-"+ name+"-"+helloProperties.getSuffix();
}
}
(4)创建自动配置类(HelloServiceAutoConfiguration)
package com.haha.starter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnWebApplication//只有web应用才生效
@EnableConfigurationProperties(HelloProperties.class)//使HelloProperties配置类生效
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean//将HelloService注入容器中
public HelloService HelloService(){
HelloService service = new HelloService();
service.setHelloProperties(helloProperties);
return service;
}
}
Step 3、在resources目录下,新建 META-INF文件夹,新建spring.factories 文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.haha.starter.HelloServiceAutoConfiguration
com.haha.starter.HelloServiceAutoConfiguration是HelloServiceAutoConfiguration自动配置类的位置,写入spring.factories 中,系统启动后才可以加载自动配置类
Step 4、将上面两个模块下载到maven仓库中,供以后使用,先install自动配置模块haha-spring-boot-autoconfigurer(因为启动器中依赖自动配置模块),后installhaha-spring-boot-starter启动器模块

Step 5、测试
新建Spring Initializr(引入web应用),pom引入启动器模块
<dependency>
<groupId>com.haha.starter</groupId>
<artifactId>haha-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
从maven中下载自定义starter


如果pom文件显示引入成功,但在External Libraries中没有找到,则file-Project Structure手动引入即可。
新建controller
package com.starter.test.controller;
import com.haha.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String sayHello(){
String selina = helloService.sayHelloToHaha("Selina");
return selina;
}
}
自定义配置application.properties
haha.hello.prefix=12345
haha.hello.suffix=67890
访问界面


本文介绍了如何创建自定义的SpringBoot Starter。首先创建启动器模块,仅依赖自动配置模块;接着创建自动配置模块,包含properties类、服务组件及自动配置类;然后在spring.factories中注册自动配置类;最后通过maven安装到本地仓库,并在测试项目中验证其功能。
291

被折叠的 条评论
为什么被折叠?



