Springboot基础知识(17)- 自定义 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 开发环境,参考 “ Spring基础知识(1)- Spring简介、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         <project xmlns="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             <modelVersion>4.0.0</modelVersion>
 6 
 7             <groupId>com.mytest</groupId>
 8             <artifactId>mytest-spring-boot-starter-autoconfigure</artifactId>
 9             <version>1.0-SNAPSHOT</version>
10 
11             <properties>
12                 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13                 <maven.compiler.source>1.8</maven.compiler.source>
14                 <maven.compiler.target>1.8</maven.compiler.target>
15             </properties>
16 
17             <parent>
18                 <groupId>org.springframework.boot</groupId>
19                 <artifactId>spring-boot-starter-parent</artifactId>
20                 <version>2.6.6</version>
21                 <relativePath/> <!-- lookup parent from repository -->
22             </parent>
23 
24             <dependencies>
25                 <dependency>
26                     <groupId>org.springframework.boot</groupId>
27                     <artifactId>spring-boot-starter</artifactId>
28                 </dependency>
29             </dependencies>
30 
31         </project>
复制代码


        在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         <project xmlns="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             <modelVersion>4.0.0</modelVersion>
 6 
 7             <groupId>com.mytest</groupId>
 8             <artifactId>mytest-spring-boot-starter</artifactId>
 9             <version>1.0-SNAPSHOT</version>
10 
11             <dependencies>
12                 <dependency>
13                     <groupId>com.mytest</groupId>
14                     <artifactId>mytest-spring-boot-starter-autoconfigure</artifactId>
15                     <version>1.0-SNAPSHOT</version>
16                 </dependency>
17             </dependencies>
18 
19         </project>
复制代码


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

    本文将在 “ Springboot基础知识(08)- spring-boot-starter-web(Web启动器)” 里 SpringbootWeb 项目的基础上,对 mytest-spring-boot-starter 进行测试。

    1) 导入 mytest-spring-boot-starter 依赖包

        修改 pom.xml:

复制代码
 1             <project ... >
 2                 ...
 3 
 4                 <dependencies>
 5                     ...
 6 
 7                     <dependency>
 8                         <groupId>com.mytest</groupId>
 9                         <artifactId>mytest-spring-boot-starter</artifactId>
10                         <version>1.0-SNAPSHOT</version>
11                     </dependency>
12 
13                     ...
14                 </dependencies>
15             
16                 ...    
17             </project>
复制代码


        在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
    
     

posted @   垄山小站  阅读(267)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
点击右上角即可分享
微信分享提示