<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                案例代碼:https://gitee.com/flymini/codes01/tree/master/springboot_/starter_ **** **1. 為什么要自定義場景啟動器** * 可能官方提供的場景啟動器不滿足你的要求。 * 可能是你想開發一個被 SpringBoot 兼容的框架。比如 Mybatis 想要被 SpringBoot 兼容,則 Mybatis 團隊自己開發了一個 mybatis 的場景啟動器。 ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> ``` * 可能你想要在項目啟動時自動加載一下依賴組件。 **2. 自定義場景啟動器需考慮如下因素** * 場景啟動器需要引入哪些依賴。 * 自動配置類需要用到哪些注解。 [TOC] # 1. WebMvcAutoConfiguration源碼 1. 通過看 WebMvcAutoConfiguration 來了解一下編寫一個場景啟動器可能需要用到哪些注解。 ```java package org.springframework.boot.autoconfigure.web.servlet; // 指定類為配置類 @Configuration( proxyBeanMethods = false ) // 當應用是Web應用時讓該配置類生效 @ConditionalOnWebApplication( type = Type.SERVLET ) // 當存在Servlet、DispatcherServlet,WebMvcConfigurer時才讓配置類生效 @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class}) // 當不存在WebMvcConfigurationSupport才讓這個配置類生效 @ConditionalOnMissingBean({WebMvcConfigurationSupport.class}) // 指定該配置類生效的順序,當大于0時,數字越小越先被執行 @AutoConfigureOrder(-2147483638) // 指定在這三個類被執行后才能執行該配置類 @AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class}) public class WebMvcAutoConfiguration { @Bean // 將方法的返回值注入到IoC容器中,<bean class="方法返回值"/>,方法名作為<bean id="方法名"/> @ConditionalOnMissingBean({HiddenHttpMethodFilter.class}) @ConditionalOnProperty( prefix = "spring.mvc.hiddenmethod.filter", name = {"enabled"}, matchIfMissing = false ) public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() { return new OrderedHiddenHttpMethodFilter(); } @Configuration( proxyBeanMethods = false ) @Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class}) // 結合對應的XXXProperties來綁定相關的配置,這些配置就是在application.properties中可以配置的屬性 // EnableConfigurationProperties讓XXXProperties這些配置生效 @EnableConfigurationProperties({WebMvcProperties.class, ResourceProperties.class}) @Order(0) public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer { ... } ``` 2. 需要將該類配置在 META-INF/spring.factories 文件中才能使它生效。 ![](https://img.kancloud.cn/87/75/8775fdd46a422f4f7c65fa332d3622d1_1305x298.jpg) ```xml # Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\ ``` <br/> # 2. 自定義場景啟動器 ## 2.1 創建自動配置類模塊 **1. 創建模塊:learn-custom-autoconfig** ```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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.12.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.learn.example</groupId> <artifactId>learn-custom-autoconfig</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> </dependency> </dependencies> </project> ``` **2. 當前模塊的properties類** ```java @Data @ConfigurationProperties(prefix = "custom.config.account") public class AccountProperties { private String username; private String password; } ``` **3. 在當前模塊提供一個獲取properties類的API** ```java @Data public class AccountService { private AccountProperties properties; } ``` **4. 將AccountService注冊到IoC容器中** ```java @Configuration //標注該類為配置類 @ConditionalOnWebApplication //當前項目是Web應用時這個配置類生效 @EnableConfigurationProperties(AccountProperties.class) //將屬性類加載到當前配置類中 public class CustomAutoConfiguration { @Autowired private AccountProperties properties; @Bean public AccountService getAccountService() { AccountService impl = new AccountService(); impl.setProperties(properties); return impl; } } ``` **5. 將配置類CustomAutoConfiguration添加到`resources/META-INF/spring.factories`** ```factories org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ learn.custom.autoconfig.config.CustomAutoConfiguration ``` >[info]提醒:不要給當前模塊添加啟動類。 <br/> ## 2.2 創建場景啟動器模塊 **1. 創建啟動器模塊:custom-spring-boot-starter** ![](https://img.kancloud.cn/a7/4f/a74fe2a5102fd1cb5ffe0869749b0134_1522x924.jpg) ![](https://img.kancloud.cn/6d/cb/6dcb120d6529abef1e385249f6cd88f3_1521x919.jpg) ``` /*********************啟動器命名規范***************************/ ------------ 官方命名空間 ------------ 前綴:spring-boot-starter 模式:spring-boot-starter-模塊名 舉例:spring-boot-starter-web、spring-boot-starter-jdbc ------------ 自定義命名空間 ------------ 后綴:spring-boot-starter 模式:模塊名-spring-boot-starter 舉例:mybatis-spring-boot-starter ``` **2. 在當前模塊引入自動配置類模塊** ```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.learn.example</groupId> <artifactId>custom-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>com.learn.example</groupId> <artifactId>learn-custom-autoconfig</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project> ``` <br/> ## 2.3 創建項目來做測試 **1. 創建一個SpringBoot項目來做測試:com-custom-test** ```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.learn.example</groupId> <artifactId>com-custom-test</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!-- 當前模塊引入自定義場景啟動器 --> <dependency> <groupId>com.learn.example</groupId> <artifactId>custom-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project> ``` **2. 在當前模塊的配置文件中給自動配置模塊的properties賦值** *`application.yml`* ```yml custom: config: account: username: zhangsan password: 123456 ``` **3. controller層調用自動配置類中的API** ```java @RestController public class IndexController { @Autowired private AccountService accountService; @RequestMapping("/v1/auto/properties") public AccountProperties getProperties() { AccountProperties properties = accountService.getProperties(); System.out.println(properties); //AccountProperties(username=zhangsan, password=123456) return properties; } } ``` <br/> 通過上面的演示可以看出,com-custom-test 模塊引入了場景啟動模塊,而場景啟動器模塊又引入了自動配置模塊,所以可以在測試模塊中可以調用到自動配置類的 API。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看