<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                案例代碼:https://gitee.com/flymini/codes01/tree/master/springboot_/com-learn-boot04 **** [TOC] # 1. 參考HttpEncodingAutoConfiguration 在自定義自動配置前,我們先參考一下 HttpEncodingAutoConfiguration 自動配置,看一下實現一個自動配置類基本需要哪些注解,它的部分源碼如下: ``` // 讀取配置文件的類 @Configuration( proxyBeanMethods = false ) // 將ServerProperties.class加載到當前的類中 @EnableConfigurationProperties({ServerProperties.class}) // 判斷當前的項目是否為Web項目,是則讓當前的配置類生效 @ConditionalOnWebApplication( type = Type.SERVLET ) // 判斷CharacterEncodingFilter.class是否存在,如果存在則加載到當前類中 @ConditionalOnClass({CharacterEncodingFilter.class}) // 讀取配置文件中以server.servlet.encoding為前綴的屬性的屬性值 // 該屬性的默認值為enabled // matchIfMissing=true 不管配置文件中是否存在prefix,一律讓 // 當前配置類生效 @ConditionalOnProperty( prefix = "server.servlet.encoding", value = {"enabled"}, matchIfMissing = true ) // 設置編碼的核心類 public class HttpEncodingAutoConfiguration { private final Encoding properties; ... // 將方法的返回值作為<bean class="返回的返回值"/> // 將方法名作為<bean id="方法名"/> @Bean // 如果CharacterEncodingFilter沒有被注入Spring的IoC容器,則注入 @ConditionalOnMissingBean public CharacterEncodingFilter characterEncodingFilter() { CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter(); filter.setEncoding(this.properties.getCharset().name()); filter.setForceRequestEncoding(this.properties.shouldForce(org.springframework.boot.web.servlet.server.Encoding.Type.REQUEST)); filter.setForceResponseEncoding(this.properties.shouldForce(org.springframework.boot.web.servlet.server.Encoding.Type.RESPONSE)); return filter; } ``` <br/> # 2. 自定義自動配置 下面自定義自動配置類 CustomAutoConfiguration,它將 AccountServiceImpl 組件自動注冊到IoC容器中。 <br/> 步驟如下: **1. 引入 spring-boot-autoconfigure 依賴** *`pom.xml`* ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.3.12.RELEASE</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> </dependency> </dependencies> ``` **2. 用于讀取配置文件的properties類** ```java @Data @Component //當IoC容器中存在多個CustomProperties時會產生沖突,@Primary表示將當前類設置為 //默認的,這樣可以防止沖突 @Primary @ConfigurationProperties(prefix = "custom.account") public class CustomProperties { private String name; private String password; } ``` **3. AccountServiceImpl組件** ```java @Data public class AccountServiceImpl { private CustomProperties properties; } ``` **4. 定義自動配置類** ```java @Primary @Configuration //將當前類作為IoC容器 @EnableConfigurationProperties(CustomProperties.class) //啟用CustomProperties @ConditionalOnClass(CustomProperties.class) //當CustomProperties存在時當前類才生效 //配置文件中存在前綴為 custom.account 的配置時當前類才生效 @ConditionalOnProperty(prefix = "custom.account", value = "enabled", matchIfMissing = true) public class CustomAutoConfiguration { @Autowired private CustomProperties properties; @Bean //注冊組件AccountServiceImpl @ConditionalOnMissingBean //當組件AccountServiceImpl還沒有被注冊時才注冊 public AccountServiceImpl accountService() { AccountServiceImpl impl = new AccountServiceImpl(); impl.setProperties(properties); return impl; } } ``` **5. 將自動配置類加入到`resources/META-INF/spring.factories`文件中** ```factories # Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.learn.boot04.config.CustomAutoConfiguration ``` **6. 在`resources/application.yml`配置文件中添加`custom.account`前綴的配置** ```yml custom: account: name: zhangsan password: 123456 ``` **7. 測試** ```java @SpringBootTest public class Boot04ApplicationTests { @Autowired private AccountServiceImpl accountService; @Test public void contextLoads() { CustomProperties properties = accountService.getProperties(); System.out.println(properties); //CustomProperties(name=zhangsan, password=123456) } } ```
                  <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>

                              哎呀哎呀视频在线观看