最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot 中 AutoConfiguration的使用方法

 更新時(shí)間:2019年04月12日 11:00:21   作者:small_925_ant  
這篇文章主要介紹了SpringBoot 中 AutoConfiguration的使用方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

在SpringBoot中我們經(jīng)??梢砸胍恍﹕tarter包來(lái)集成一些工具的使用,比如spring-boot-starter-data-redis。

使用起來(lái)很方便,那么是如何實(shí)現(xiàn)的呢?

代碼分析

我們先看注解@SpringBootApplication,它里面包含一個(gè)@EnableAutoConfiguration

繼續(xù)看@EnableAutoConfiguration注解

@Import({AutoConfigurationImportSelector.class})

在這個(gè)類(AutoConfigurationImportSelector)里面實(shí)現(xiàn)了自動(dòng)配置的加載

主要代碼片段:

String[] selectImports(AnnotationMetadata annotationMetadata)方法中

AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(autoConfigurationMetadata, annotationMetadata);

getAutoConfigurationEntry方法中: 

List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes); 

protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
    List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
    Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
    return configurations;
}

最后會(huì)通過(guò)SpringFactoriesLoader.loadSpringFactories去加載META-INF/spring.factories

Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");
        LinkedMultiValueMap result = new LinkedMultiValueMap();
    while(urls.hasMoreElements()) {
          URL url = (URL)urls.nextElement();
          UrlResource resource = new UrlResource(url);
          Properties properties = PropertiesLoaderUtils.loadProperties(resource);
          Iterator var6 = properties.entrySet().iterator();

          while(var6.hasNext()) {
            Entry<?, ?> entry = (Entry)var6.next();
            String factoryClassName = ((String)entry.getKey()).trim();
            String[] var9 = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());
            int var10 = var9.length;

            for(int var11 = 0; var11 < var10; ++var11) {
              String factoryName = var9[var11];
              result.add(factoryClassName, factoryName.trim());
            }
          }
        }

ZookeeperAutoConfiguration

我們來(lái)實(shí)現(xiàn)一個(gè)ZK的AutoConfiguration    

首先定義一個(gè)ZookeeperAutoConfiguration類 

然后在META-INF/spring.factories中加入

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.fayayo.fim.zookeeper.ZookeeperAutoConfiguration

接下來(lái)我們看看具體的實(shí)現(xiàn):

@ConfigurationProperties(prefix = "fim.register")
@Configuration
public class URLRegistry {
  private String address;
  private int timeout;
  private int sessionTimeout;
  public String getAddress() {
    if (address == null) {
      address = URLParam.ADDRESS;
    }
    return address;
  }
  public void setAddress(String address) {
    this.address = address;
  }
  public int getTimeout() {
    if (timeout == 0) {
      timeout = URLParam.CONNECTTIMEOUT;
    }
    return timeout;
  }
  public void setTimeout(int timeout) {
    this.timeout = timeout;
  }
  public int getSessionTimeout() {
    if (sessionTimeout == 0) {
      sessionTimeout = URLParam.REGISTRYSESSIONTIMEOUT;
    }
    return sessionTimeout;
  }
  public void setSessionTimeout(int sessionTimeout) {
    this.sessionTimeout = sessionTimeout;
  }
}
@Configuration
@EnableConfigurationProperties(URLRegistry.class)
@Slf4j
public class ZookeeperAutoConfiguration {
  @Autowired
  private URLRegistry url;
  @Bean(value = "registry")
  public Registry createRegistry() {
    try {
      String address = url.getAddress();
      int timeout = url.getTimeout();
      int sessionTimeout = url.getSessionTimeout();
      log.info("init ZookeeperRegistry,address[{}],sessionTimeout[{}],timeout[{}]", address, timeout, sessionTimeout);
      ZkClient zkClient = new ZkClient(address, sessionTimeout, timeout);
      return new ZookeeperRegistry(zkClient);
    } catch (ZkException e) {
      log.error("[ZookeeperRegistry] fail to connect zookeeper, cause: " + e.getMessage());
      throw e;
    }
  }
}

 ZookeeperRegistry部分實(shí)現(xiàn):

public ZookeeperRegistry(ZkClient zkClient) {
    this.zkClient = zkClient;

    log.info("zk register success!");

    String parentPath = URLParam.ZOOKEEPER_REGISTRY_NAMESPACE;
    try {
      if (!zkClient.exists(parentPath)) {
        log.info("init zookeeper registry namespace");
        zkClient.createPersistent(parentPath, true);
      }
      //監(jiān)聽
      zkClient.subscribeChildChanges(parentPath, new IZkChildListener() {
        //對(duì)父節(jié)點(diǎn)添加監(jiān)聽子節(jié)點(diǎn)變化。
        @Override
        public void handleChildChange(String parentPath, List<String> currentChilds) {
          log.info(String.format("[ZookeeperRegistry] service list change: path=%s, currentChilds=%s", parentPath, currentChilds.toString()));
          if(watchNotify!=null){
            watchNotify.notify(nodeChildsToUrls(currentChilds));
          }
        }
      });

      ShutDownHook.registerShutdownHook(this);

    } catch (Exception e) {
      e.printStackTrace();
      log.error("Failed to subscribe zookeeper");
    }
  }

具體使用

那么我們?cè)趺词褂米约簩懙腪ookeeperAutoConfiguration呢

 首先要在需要使用的項(xiàng)目中引入依賴

   <dependency>
      <groupId>com.fayayo</groupId>
      <artifactId>fim-registry-zookeeper</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>

    然后配置參數(shù)

 fim:
   register:
    address: 192.168.88.129:2181
    timeout: 2000

   如果不配置會(huì)有默認(rèn)的參數(shù)

    具體使用的時(shí)候只需要在Bean中注入就可以了,比如

@Autowired
  private Registry registry;
  public List<URL> getAll(){
    List<URL>list=cache.get(KEY);
    if(CollectionUtils.isEmpty(list)){
      list=registry.discover();
      cache.put(KEY,list);
    }
    return list;
  }

完整代碼

https://github.com/lizu18xz/fim.git

總結(jié)

以上所述是小編給大家介紹的SpringBoot 中 AutoConfiguration的使用方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

  • Java快速排序與歸并排序及基數(shù)排序圖解示例

    Java快速排序與歸并排序及基數(shù)排序圖解示例

    快速排序是基于二分的思想,對(duì)冒泡排序的一種改進(jìn)。主要思想是確立一個(gè)基數(shù),將小于基數(shù)的數(shù)放到基數(shù)左邊,大于基數(shù)的數(shù)字放到基數(shù)的右邊,然后在對(duì)這兩部分進(jìn)一步排序,從而實(shí)現(xiàn)對(duì)數(shù)組的排序
    2022-09-09
  • Java內(nèi)部類持有外部類導(dǎo)致內(nèi)存泄露的原因與解決方案詳解

    Java內(nèi)部類持有外部類導(dǎo)致內(nèi)存泄露的原因與解決方案詳解

    這篇文章主要為大家詳細(xì)介紹了Java因?yàn)閮?nèi)部類持有外部類導(dǎo)致內(nèi)存泄露的原因以及其解決方案,文中的示例代碼講解詳細(xì),希望對(duì)大家有所幫助
    2022-11-11
  • 記一次springboot配置redis項(xiàng)目啟動(dòng)時(shí)的一個(gè)奇怪的錯(cuò)誤

    記一次springboot配置redis項(xiàng)目啟動(dòng)時(shí)的一個(gè)奇怪的錯(cuò)誤

    這篇文章主要介紹了spring?boot配置redis項(xiàng)目啟動(dòng)時(shí)的一個(gè)奇怪的錯(cuò)誤,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 淺談使用java解析和生成JSON

    淺談使用java解析和生成JSON

    在www.json.org上公布了很多JAVA下的json構(gòu)造和解析工具,其中g(shù)oogle-gson和org.json比較簡(jiǎn)單,兩者使用上差不多但還是有些區(qū)別。下面我們就來(lái)分別介紹下用他們構(gòu)造和解析Json數(shù)據(jù)的方法示例。
    2015-08-08
  • Maven 項(xiàng)目生成jar運(yùn)行時(shí)提示“沒(méi)有主清單屬性”

    Maven 項(xiàng)目生成jar運(yùn)行時(shí)提示“沒(méi)有主清單屬性”

    這篇文章主要介紹了Maven 項(xiàng)目生成jar運(yùn)行時(shí)提示“沒(méi)有主清單屬性”,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • 在Java中使用Moshi?JSON庫(kù)的方法詳解

    在Java中使用Moshi?JSON庫(kù)的方法詳解

    Moshi?是一個(gè)可用于?Java?與?Kotlin?的?JSON?序列化與反序列化庫(kù),其主要使用?Kotlin?編寫,本文以樣例代碼的方式來(lái)演示該庫(kù)在?Java?中的使用,需要的朋友可以參考下
    2024-04-04
  • 通過(guò)xml配置SpringMVC注解DispatcherServlet初始化過(guò)程解析

    通過(guò)xml配置SpringMVC注解DispatcherServlet初始化過(guò)程解析

    這篇文章主要為大家介紹了通過(guò)xml配置SpringMVC注解DispatcherServlet初始化過(guò)程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • SpringBoot?+?MyBatis-Plus構(gòu)建樹形結(jié)構(gòu)的幾種方式

    SpringBoot?+?MyBatis-Plus構(gòu)建樹形結(jié)構(gòu)的幾種方式

    在實(shí)際開發(fā)中,很多數(shù)據(jù)都是樹形結(jié)構(gòu),本文主要介紹了SpringBoot?+?MyBatis-Plus構(gòu)建樹形結(jié)構(gòu)的幾種方式,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • JavaWeb亂碼問(wèn)題的終極解決方案(推薦)

    JavaWeb亂碼問(wèn)題的終極解決方案(推薦)

    這篇文章主要給大家介紹了關(guān)于JavaWeb亂碼問(wèn)題的終極解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用JavaWeb具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Java Integer.valueOf()和Integer.parseInt()的區(qū)別說(shuō)明

    Java Integer.valueOf()和Integer.parseInt()的區(qū)別說(shuō)明

    這篇文章主要介紹了Java Integer.valueOf()和Integer.parseInt()的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-08-08

最新評(píng)論

信丰县| 五峰| 平舆县| 綦江县| 五台县| 如东县| 泽普县| 修文县| 遵化市| 乳源| 鄂州市| 孟津县| 黄浦区| 芜湖县| 合山市| 肇源县| 永清县| 武邑县| 梁山县| 会同县| 大邑县| 呈贡县| 遵义县| 和平区| 津南区| 桃源县| 哈密市| 怀来县| 松潘县| 安阳县| 纳雍县| 郓城县| 四会市| 商洛市| 南丰县| 罗城| 衢州市| 乡宁县| 枞阳县| 肥东县| 江西省|