org.springframework.boot.jdbc

Class DataSourceBuilder<T extends javax.sql.DataSource>

  • java.lang.Object
    • org.springframework.boot.jdbc.DataSourceBuilder<T>
  • Type Parameters:
    T - the DataSource type being built


    public final class DataSourceBuilder<T extends javax.sql.DataSource>
    extends java.lang.Object
    Convenience class for building a DataSource. Provides a limited subset of the properties supported by a typical DataSource as well as detection logic to pick the most suitable pooling DataSource implementation.

    The following pooling DataSource implementations are supported by this builder. When no type has been explicitly set, the first available pool implementation will be picked:

    • Hikari (com.zaxxer.hikari.HikariDataSource)
    • Tomcat JDBC Pool (org.apache.tomcat.jdbc.pool.DataSource)
    • Apache DBCP2 (org.apache.commons.dbcp2.BasicDataSource)
    • Oracle UCP (oracle.ucp.jdbc.PoolDataSourceImpl)

    The following non-pooling DataSource implementations can be used when explicitly set as a type:

    • Spring's SimpleDriverDataSource (org.springframework.jdbc.datasource.SimpleDriverDataSource)
    • Oracle (oracle.jdbc.datasource.OracleDataSource)
    • H2 (org.h2.jdbcx.JdbcDataSource)
    • Postgres (org.postgresql.ds.PGSimpleDataSource)
    • Any DataSource implementation with appropriately named methods

    This class is commonly used in an @Bean method and often combined with @ConfigurationProperties.

    Since:
    2.0.0
    See Also:
    create(), create(ClassLoader), derivedFrom(DataSource)
    • Method Detail

      • type

        public <D extends javax.sql.DataSource> DataSourceBuilder<D> type(java.lang.Class<D> type)
        Set the DataSource type that should be built.
        Type Parameters:
        D - the datasource type
        Parameters:
        type - the datasource type
        Returns:
        this builder
      • url

        public DataSourceBuilder<T> url(java.lang.String url)
        Set the URL that should be used when building the datasource.
        Parameters:
        url - the JDBC url
        Returns:
        this builder
      • driverClassName

        public DataSourceBuilder<T> driverClassName(java.lang.String driverClassName)
        Set the driver class name that should be used when building the datasource.
        Parameters:
        driverClassName - the driver class name
        Returns:
        this builder
      • username

        public DataSourceBuilder<T> username(java.lang.String username)
        Set the username that should be used when building the datasource.
        Parameters:
        username - the user name
        Returns:
        this builder
      • password

        public DataSourceBuilder<T> password(java.lang.String password)
        Set the password that should be used when building the datasource.
        Parameters:
        password - the password
        Returns:
        this builder
      • build

        public T build()
        Return a newly built DataSource instance.
        Returns:
        the built datasource
      • create

        public static DataSourceBuilder<?> create(java.lang.ClassLoader classLoader)
        Create a new DataSourceBuilder instance.
        Parameters:
        classLoader - the classloader used to discover preferred settings
        Returns:
        a new DataSource builder instance
      • derivedFrom

        public static DataSourceBuilder<?> derivedFrom(javax.sql.DataSource dataSource)
        Create a new DataSourceBuilder instance derived from the specified data source. The returned builder can be used to build the same type of DataSource with username, password, url and driverClassName properties copied from the original when not specifically set.
        Parameters:
        dataSource - the source DataSource
        Returns:
        a new DataSource builder
        Since:
        2.5.0
      • findType

        public static java.lang.Class<? extends javax.sql.DataSource> findType(java.lang.ClassLoader classLoader)
        Find the DataSource type preferred for the given classloader.
        Parameters:
        classLoader - the classloader used to discover preferred settings
        Returns:
        the preferred DataSource type