Spring基础知识(36)- Spring Boot (十七)
自定义 starter
starter 是 SpringBoot 中一种非常重要的机制,它可以繁杂的配置统一集成到 starter 中,我们只需要通过 maven 将 starter 依赖引入到项目中,SpringBoot 就能自动扫描并加载相应的默认配置。
starter 的出现让开发人员从繁琐的框架配置中解放出来,将更多的精力专注于业务逻辑的开发,极大的提高了开发效率。在一些情况下,我们也可以将一些通用功能封装成自定义的 starter 进行使用。
命名规范:
SpringBoot 提供的 starter 以 spring-boot-starter-xxx 的形式命名。为了与 SpringBoot 生态提供的 starter 进行区分,官方建议第三方开发者或技术(例如 Druid、Mybatis 等等)厂商自定义的 starter 使用 xxx-spring-boot-starter 的形式命名,例如 mybatis-spring-boot-starter、druid-spring-boot-starter 等等。
模块规范:
Spring Boot 官方建议我们在自定义 starter 时,创建两个 Module :autoConfigure Module 和 starter Module,其中 starter Module 依赖于 autoConfigure Module。
这只是 Spring Boot 官方的建议,并不是硬性规定,若不需要自动配置代码和依赖项目分离,我们也可以将它们组合到同一个 Module 里。
本文将在 Windows 下使用 IntelliJ IDEA 和 Apache Maven 创建一个简单的 starter 示例。在开始之前,确保已经正确搭建了 Spring 开发环境,参考 “ ”。
Windows版本 : Windows 10 Home (20H2)
IntelliJ IDEA:Community Edition for Windows 2020.1.4
Apache Maven:3.8.1
1. 创建一个空项目(Empty Project)
在 IDEA 创建一个空项目(Empty Project)-> Next -> 输入项目名称 StarterProject -> Finish
项目路径:~/workshop/idea-projects/StarterProject
2. 创建一个 Springboot 模块
1) 创建 Maven 模块
IDEA 菜单 -> File -> Project Structure -> 进入 Project Structure 界面 Modules 页
-> 点击 “+” 按钮 -> 选择 “New Module” -> 进入 New Module 页面
-> 选择 “Maven” 类型 -> 不选择 “Create from archtype”,直接点击 Next -> 进入 New Module (2) 页面
Parent: None
Name: mytest-spring-boot-starter-autoconfiguration
Location: ~/workshop/idea-projects/StarterProject/mytest-spring-boot-starter-autoconfiguration
Artifact Coordinates
GroupId: com.mytest
ArtifactId: mytest-spring-boot-starter-autoconfiguration
Version: 1.0-SNAPSHOT
-> Finish -> 返回 Project Structure 界面 -> OK
2) 修改 pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2"http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 4.0.0 6 7com.mytest 8mytest-spring-boot-starter-autoconfigure 91.0-SNAPSHOT 10 1112 16 17UTF-8 131.8 141.8 1518 23 24org.springframework.boot 19spring-boot-starter-parent 202.6.6 2122 25 30 3126 29org.springframework.boot 27spring-boot-starter 28
在IDE中项目列表 -> mytest-spring-boot-starter-autoconfigure -> 点击鼠标右键 -> Maven -> Reload Project
3) 创建 src/main/java/com/mytest/TestProperties.java 文件
1 package com.mytest; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 5 @ConfigurationProperties("com.mytest") 6 public class TestProperties { 7 8 private String prefix; 9 private String suffix; 10 11 public String getPrefix() { 12 return prefix; 13 } 14 15 public void setPrefix(String prefix) { 16 this.prefix = prefix; 17 } 18 19 public String getSuffix() { 20 return suffix; 21 } 22 23 public void setSuffix(String suffix) { 24 this.suffix = suffix; 25 } 26 }
4) 创建 src/main/java/com/mytest/TestService.java 文件
1 package com.mytest; 2 3 public class TestService { 4 5 TestProperties testProperties; 6 7 public TestProperties getTestProperties() { 8 return testProperties; 9 } 10 11 public void setTestProperties(TestProperties testProperties) { 12 this.testProperties = testProperties; 13 } 14 15 public String showTest(String name){ 16 17 return testProperties.getPrefix() + name + testProperties.getSuffix(); 18 19 } 20 }
5) 创建 src/main/java/com/mytest/TestServiceAutoConfiguration.java 文件
1 package com.mytest; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; 5 import org.springframework.boot.context.properties.EnableConfigurationProperties; 6 import org.springframework.context.annotation.Bean; 7 import org.springframework.context.annotation.Configuration; 8 9 @Configuration 10 @EnableConfigurationProperties(TestProperties.class) 11 @ConditionalOnWebApplication // 当项目是 web 应用的时候才生效 12 public class TestServiceAutoConfiguration { 13 14 @Autowired 15 TestProperties testProperties; 16 17 @Bean 18 public TestService testService(){ 19 TestService testService = new TestService(); 20 testService.setTestProperties(testProperties); 21 return testService; 22 } 23 24 }
6) 创建 src/main/resources/META-INF/spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.mytest.TestServiceAutoConfiguration
3. 创建一个普通的 Maven 模块
1) 创建 Maven 模块
IDEA 菜单 -> File -> Project Structure -> 进入 Project Structure 界面 Modules 页
-> 点击 “+” 按钮 -> 选择 “New Module” -> 进入 New Module 页面
-> 选择 “Maven” 类型 -> 不选择 “Create from archtype”,直接点击 Next -> 进入 New Module (2) 页面
Parent: None
Name: mytest-spring-boot-starter
Location: ~/workshop/idea-projects/StarterProject/mytest-spring-boot-starter
Artifact Coordinates
GroupId: com.mytest
ArtifactId: mytest-spring-boot-starter
Version: 1.0-SNAPSHOT
-> Finish -> 返回 Project Structure 界面 -> OK
2) 修改 pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2"http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 4.0.0 6 7com.mytest 8mytest-spring-boot-starter 91.0-SNAPSHOT 10 1112 18 1913 17com.mytest 14mytest-spring-boot-starter-autoconfigure 151.0-SNAPSHOT 16
4. 打包安装
1) 打包安装 mytest-spring-boot-starter-autoconfigure 到本地 Maven 仓库
菜单 View -> Tool Windows -> Maven -> mytest-spring-boot-starter-autoconfigure -> Lifecycle -> Install
2) 打包安装 mytest-spring-boot-starter 到本地 Maven 仓库
菜单 View -> Tool Windows -> Maven -> mytest-spring-boot-starter -> Lifecycle -> Install
5. 测试 mytest-spring-boot-starter
本文将在 “” 里 SpringbootWeb 项目的基础上,对 mytest-spring-boot-starter 进行测试。
1) 导入 mytest-spring-boot-starter 依赖包
修改 pom.xml:
12 ... 3 4 5 ... 6 7 15 16 ... 178 12 13 ... 14com.mytest 9mytest-spring-boot-starter 101.0-SNAPSHOT 11
在IDE中项目列表 -> SpringbootWeb -> 点击鼠标右键 -> Maven -> Reload Project
2) 修改 src/main/java/com/example/controller/IndexController.java 文件
1 package com.example.controller; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.ResponseBody; 7 8 import com.mytest.TestService; 9 10 @Controller 11 public class IndexController { 12 @Autowired 13 TestService testService; 14 15 @ResponseBody 16 @RequestMapping("/mytest") 17 public String mytest() { 18 return testService.showTest("< 自定义starter >"); 19 } 20 }
3) 修改 src/main/resources/application.properties 文件,添加如下代码
# mytest-spring-boot-starter
com.mytest.prefix=mytest-prefix
com.mytest.suffix=mytest-suffix
访问:http://localhost:9090/mytest
mytest-prefix< 自定义starter >mytest-suffix