<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      SpringBoot 自定義 Starter

      自定義 Starter

      一、基礎搭建

      1. 創建項目和模塊 ( 建議使用阿里云鏡像地址初始化: http://start.aliyun.com )

        1. 創建一個空的 SpringBoot 2.7.6 項目, 作為父工程

        2. 創建一個空的名為 my3-spring-boot-autoconfigure? (簡稱autoconfigure?) SpringBoot 2.7.6 模塊, 作為子模塊

          作用: 用于編寫核心代碼

        3. 創建一個空的名為 my3-spring-boot-starter? (簡稱starter?) 普通Maven模塊, 作為子模塊

          作用: 用于依賴管理

      2. 調整文件結構

        • 刪除父工程和starter?子模塊下的 src 目錄, 效果如下

          image
          ?

      3. 修改pom.xml

        • 父模塊

          刪除build?標簽 , 效果如下

          <?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>
              <groupId>com.example</groupId>
              <artifactId>customStarter3</artifactId>
              <version>0.0.1-SNAPSHOT</version>
          	<!-- 注意打包方式 -->
              <packaging>pom</packaging>
              <name>customStarter3</name>
              <description>customStarter3</description>
              <!-- 注意這里autoconfigure在starter前面 -->
          	<modules>
          		<module>my3-spring-boot-autoconfigure</module>
                  <module>my3-spring-boot-starter</module>
              </modules>
              <properties>
                  <java.version>1.8</java.version>
                  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
                  <spring-boot.version>2.7.6</spring-boot.version>
              </properties>
          
          	<dependencies>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter</artifactId>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-test</artifactId>
                      <scope>test</scope>
                  </dependency>
              </dependencies>
              
              <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>
          
          </project>
          
        • ?autoconfigure? 子模塊

          添加spring-boot-configuration-processor?依賴, 用于寫yml配置屬性具有提示

          注意這個依賴不要傳遞, 添加<optional>true</optional>?, 效果如下

          <?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>
              <parent>
                  <groupId>com.example</groupId>
                  <artifactId>customStarter3</artifactId>
                  <version>0.0.1-SNAPSHOT</version>
              </parent>
          
              <artifactId>my3-spring-boot-autoconfigure</artifactId>
          
              <properties>
                  <maven.compiler.source>17</maven.compiler.source>
                  <maven.compiler.target>17</maven.compiler.target>
                  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              </properties>
          
              <dependencies>
                  <!-- yml配置屬性提示 -->
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-configuration-processor</artifactId>
                      <optional>true</optional>
                  </dependency>
              </dependencies>
          
          </project>
          
        • ?starter? 子模塊

          添加依賴, 這個依賴就是autoconfigure?子模塊

          <?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>
              <parent>
                  <groupId>com.example</groupId>
                  <artifactId>customStarter3</artifactId>
                  <version>0.0.1-SNAPSHOT</version>
              </parent>
          
              <artifactId>my3-spring-boot-starter</artifactId>
          
              <properties>
                  <maven.compiler.source>17</maven.compiler.source>
                  <maven.compiler.target>17</maven.compiler.target>
                  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              </properties>
          
              <dependencies> 
                  <dependency>
                      <groupId>com.example</groupId>
                      <artifactId>my3-spring-boot-autoconfigure</artifactId>
                      <version>0.0.1-SNAPSHOT</version>
                  </dependency>
              </dependencies>
          
          </project>
          
      4. 編寫核心代碼

        一般需要在autoconfigure?子模塊寫三個核心代碼 (配置屬性類, 自動配置類, 被裝配的類), 一個核心文件(.imports文件)

        • 配置屬性類 (用于讀取 application.yml 配置屬性)

          package com.example;
          
          import org.springframework.boot.context.properties.ConfigurationProperties;
          // 讀取yml中以hello開頭的屬性
          @ConfigurationProperties(prefix = "hello")
          public class HelloProperties {
          	// hello.greeting 對應賦給 greeting, 沒有則給默認值為 "Hello"
              private String greeting = "Hello";
              private String message = "World";
          
              public String getGreeting() {
                  return greeting;
              }
          
              public void setGreeting(String greeting) {
                  this.greeting = greeting;
              }
          
              public String getMessage() {
                  return message;
              }
          
              public void setMessage(String message) {
                  this.message = message;
              }
          }
          
        • 需要被裝配的類

          package com.example;
          
          public class HelloService {
              private final String greeting;
              private final String message;
          
              public HelloService(String greeting, String message) {
                  this.greeting = greeting;
                  this.message = message;
              }
          
              public String sayHello() {
                  return greeting + ", " + message + "!";
              }
          }
          
        • 自動配置類

          package com.example;
          
          import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
          import org.springframework.boot.context.properties.EnableConfigurationProperties;
          import org.springframework.context.annotation.Bean;
          import org.springframework.context.annotation.Configuration;
          
          @Configuration
          @ConditionalOnClass(HelloService.class)
          @EnableConfigurationProperties(HelloProperties.class)
          public class HelloServiceAutoConfiguration {
          
              @Bean
              public HelloService helloService(HelloProperties helloproperties) {
                  return new HelloService(helloproperties.getGreeting(), helloproperties.getMessage());
              }
          }
          
        • resources 目錄下, META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

          com.example.HelloServiceAutoConfiguration
          
      5. 打包測試

        1. 在自定義 starter 項目中

          點擊idea右側的Maven, 再點擊父模塊中的生命周期的 install, 安裝到本地倉庫

        2. 在別的項目中

          • 引入 starter?依賴使用 (注意引入的是子模塊 starter?, version 是 starter?子模塊的版本號)

            <dependency>
                 <groupId>com.example</groupId>
                 <artifactId>my3-spring-boot-starter</artifactId>
                 <version>0.0.1-SNAPSHOT</version>
            </dependency>
            
          • application.yml 中填寫配置信息

            hello:
              greeting: greeting6666
              message: message6666
            
          • 單元測試中, 自動注入被裝配的類就可以測試了

            package com.exampleTest;
            
            import com.example.HelloService;
            import org.junit.jupiter.api.Test;
            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.boot.test.context.SpringBootTest;
            
            @SpringBootTest
            class TestCustomStarterApplicationTests {
            
                @Autowired
                private HelloService helloService;
            
                @Test
                void contextLoads() {
                    System.out.println(helloService.sayHello());
                }
            
            }
            

      ?

      posted @ 2025-07-22 23:46  hyd666  閱讀(8)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 无码精品人妻一区二区三区中| 青草青草久热精品视频在线观看| 亚洲人成网7777777国产| 真实国产老熟女无套内射| 国产午精品午夜福利757视频播放| 久久婷婷五月综合色一区二区| 国产蜜臀一区二区在线播放| 亚洲 都市 无码 校园 激情| 久久高潮少妇视频免费| 亚洲av免费成人在线| 国产伊人网视频在线观看| 夜夜添无码一区二区三区| 亚洲春色在线视频| 91精品人妻中文字幕色| 自拍视频在线观看成人| 亚洲男人的天堂一区二区| 国产精品剧情亚洲二区| 国产一区二区三区内射高清| 国产台湾黄色av一区二区| √天堂中文www官网在线| 国产乱理伦片在线观看| 波多野结av在线无码中文免费| 中文字幕日韩有码国产| 久久精品蜜芽亚洲国产av| 欧产日产国产精品精品| 日韩欧美视频一区二区三区| 久久精品国产99久久6| 成人福利国产午夜AV免费不卡在线 | 国产又黄又爽又不遮挡视频| 图片区 小说区 区 亚洲五月| 深夜福利视频在线播放| 亚洲狠狠爱一区二区三区| 国产在线拍偷自揄观看视频网站 | 99久热在线精品视频| 久久午夜无码鲁丝片直播午夜精品| 久久一亚色院精品全部免费| 免费无码黄网站在线观看| 灌云县| 亚洲国内精品一区二区| 亚洲国产成人久久一区久久| 无码精品国产VA在线观看DVD |