美文网首页
DataSourceBuilder 类分析

DataSourceBuilder 类分析

作者: 有bear来啊 | 来源:发表于2018-03-12 10:34 被阅读0次

从分析DataSourceBuilder类来了解spring boot 的数据源配置.
org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder.java
源码贴这 有空再看

/**
 * Convenience class for building a {@link DataSource} with common implementations and
 * properties. If Tomcat, HikariCP or Commons DBCP are on the classpath one of them will
 * be selected (in that order with Tomcat first). In the interest of a uniform interface,
 * and so that there can be a fallback to an embedded database if one can be detected on
 * the classpath, only a small set of common configuration properties are supported. To
 * inject additional properties into the result you can downcast it, or use
 * {@code @ConfigurationProperties}.
 *
 * @author Dave Syer
 * @since 1.1.0
 */
public class DataSourceBuilder {

    private static final String[] DATA_SOURCE_TYPE_NAMES = new String[] {
            "org.apache.tomcat.jdbc.pool.DataSource",
            "com.zaxxer.hikari.HikariDataSource",
            "org.apache.commons.dbcp.BasicDataSource", // deprecated
            "org.apache.commons.dbcp2.BasicDataSource" };

    private Class<? extends DataSource> type;

    private ClassLoader classLoader;

    private Map<String, String> properties = new HashMap<String, String>();

    public static DataSourceBuilder create() {
        return new DataSourceBuilder(null);
    }

    public static DataSourceBuilder create(ClassLoader classLoader) {
        return new DataSourceBuilder(classLoader);
    }

    public DataSourceBuilder(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }
    //构建数据源
    public DataSource build() {
       //返回数据源的类 类型
        Class<? extends DataSource> type = getType();
       //实例化这个数据源
        DataSource result = BeanUtils.instantiate(type);
       //向读取配置文件中的 driverClassName  
        maybeGetDriverClassName();
       // tm的我猜是将配置属性绑定到数据源上
        bind(result);
        return result;
    }

    private void maybeGetDriverClassName() {
        //如果this.properties不存在driverClassName 并且存在url
        if (!this.properties.containsKey("driverClassName")
                && this.properties.containsKey("url")) {
            String url = this.properties.get("url");

         //根据url 获取 DateBaseDriver 进一步获取到 驱动名 这就是为什么不配置驱动名也可以加载驱动的原因吧,通过url来判断驱动类型
            String driverClass = DatabaseDriver.fromJdbcUrl(url).getDriverClassName();
            this.properties.put("driverClassName", driverClass);
        }
    }
  //?
    private void bind(DataSource result) {
        MutablePropertyValues properties = new MutablePropertyValues(this.properties);
        new RelaxedDataBinder(result).withAlias("url", "jdbcUrl")
                .withAlias("username", "user").bind(properties);
    }

    public DataSourceBuilder type(Class<? extends DataSource> type) {
        this.type = type;
        return this;
    }

    public DataSourceBuilder url(String url) {
        this.properties.put("url", url);
        return this;
    }

    public DataSourceBuilder driverClassName(String driverClassName) {
        this.properties.put("driverClassName", driverClassName);
        return this;
    }

    public DataSourceBuilder username(String username) {
        this.properties.put("username", username);
        return this;
    }

    public DataSourceBuilder password(String password) {
        this.properties.put("password", password);
        return this;
    }

    @SuppressWarnings("unchecked")
    public Class<? extends DataSource> findType() {
        if (this.type != null) {
            return this.type;
        }
        for (String name : DATA_SOURCE_TYPE_NAMES) {
            try {
                return (Class<? extends DataSource>) ClassUtils.forName(name,
                        this.classLoader);
            }
            catch (Exception ex) {
                // Swallow and continue
            }
        }
        return null;
    }
    
    private Class<? extends DataSource> getType() {
        Class<? extends DataSource> type = findType();
        if (type != null) {
            return type;
        }
        throw new IllegalStateException("No supported DataSource type found");
    }

}

//@ConfigurationProperties

相关文章

  • DataSourceBuilder 类分析

    从分析DataSourceBuilder类来了解spring boot 的数据源配置.org.springfram...

  • ios 底层原理 : 类与类结构分析

    本文主要分析类与类结构 类的分析 类的分析主要是分析 isa 的走向与继承关系 准备 创建两个类 1.继承自 NS...

  • OC底层原理 07:类 & 类结构分析

    本文的主要目的是分析 类 & 类的结构,整篇都是围绕一个类展开的一些探索 类 的分析 类的分析 主要是分析 isa...

  • iOS底层原理07:类 & 类结构分析

    本文的主要目的是分析 类 & 类的结构,整篇都是围绕一个类展开的一些探索 类的分析 类的分析主要是分析 isa的走...

  • iOS底层类结构分析

    类 的分析 类的分析 主要是分析 isa的走向 以及 继承关系 定义两个类 继承自NSObject的类LGPers...

  • iOS 类的结构分析

    类 的分析 类的分析 主要是分析 isa的走向 以及 继承关系 准备工作 定义两个类 继承自NSObject的类C...

  • iOS 类原理探索:类的结构分析

    OC 类原理探索 系列文章 OC 类原理探索:类的结构分析 OC 类原理探索:类结构分析补充[https://ju...

  • 类,类结构分析

    忙不是不学习的借口 在isa和类的关联[https://www.jianshu.com/p/079a6ad90f1...

  • 8、面向对象

    分析面向对象 先分析类,分析静态的属性和动态的方法 创建对象:类名 对象名=new 类名(); 为对象的属性赋值:...

  • Jun_25.md

    今日任务 以主类为目标尝试新的分析方式 Que 0x01 整理分析的主类 当前分析主类: com.samsung....

网友评论

      本文标题:DataSourceBuilder 类分析

      本文链接:https://www.haomeiwen.com/subject/tafgfftx.html