SpringBoot 中 AutoConfiguration的使用方法

所属分类: 软件编程 / java 阅读数: 25
收藏 0 赞 0 分享

在SpringBoot中我们经常可以引入一些starter包来集成一些工具的使用,比如spring-boot-starter-data-redis

使用起来很方便,那么是如何实现的呢?

代码分析

我们先看注解@SpringBootApplication,它里面包含一个@EnableAutoConfiguration

继续看@EnableAutoConfiguration注解

@Import({AutoConfigurationImportSelector.class})

在这个类(AutoConfigurationImportSelector)里面实现了自动配置的加载

主要代码片段:

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

最后会通过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

我们来实现一个ZK的AutoConfiguration    

首先定义一个ZookeeperAutoConfiguration类 

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

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

接下来我们看看具体的实现:

@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部分实现:

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);
      }
      //监听
      zkClient.subscribeChildChanges(parentPath, new IZkChildListener() {
        //对父节点添加监听子节点变化。
        @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");
    }
  }

具体使用

那么我们怎么使用自己写的ZookeeperAutoConfiguration呢

 首先要在需要使用的项目中引入依赖

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

    然后配置参数

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

   如果不配置会有默认的参数

    具体使用的时候只需要在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

以上所述是小编给大家介绍的SpringBoot 中 AutoConfiguration的使用方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

更多精彩内容其他人还在看

SpringBoot环境搭建及第一个程序运行(小白教程)

这篇文章主要介绍了SpringBoot环境搭建及第一个程序运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

过滤器 和 拦截器的 6个区别(别再傻傻分不清了)

这篇文章主要介绍了过滤器 和 拦截器的 6个区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

SpringBoot整合SpringTask实现定时任务的流程

这篇文章主要介绍了SpringBoot整合SpringTask实现定时任务的流程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

vscode快速引入第三方jar包发QQ邮件

这篇文章主要介绍了vscode快速引入第三方jar包发QQ邮件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Java Enum和String及int的相互转化示例

这篇文章主要介绍了Java Enum和String及int的相互转化示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring boot如何快速的配置多个Redis数据源

这篇文章主要介绍了Spring boot如何快速的配置多个Redis数据源,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

JAVA 对接腾讯云直播的实现

这篇文章主要介绍了JAVA 对接腾讯云直播的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

JavaSE static final及abstract修饰符实例解析

这篇文章主要介绍了JavaSE static final及abstract修饰符实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringBoot定时任务参数运行代码实例解析

这篇文章主要介绍了SpringBoot定时任务运行代码实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring Boot调用 Shell 脚本实现看门狗功能

这篇文章主要介绍了Spring Boot调用 Shell 脚本实现看门狗功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多