application-database.properties

#初始化数据库的时候,如果错误,是否继续启动。
spring.datasource.continue-on-error=false
#jdbc driver.默认通过uri来自动检测。
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#使用的db名称
spring.datasource.name=test
#是否随机生成db名称
spring.datasource.generate-unique-name=false
#jdbc url.连接数据库的uri
spring.datasource.url=
#数据库连接用户名
spring.datasource.username=
#数据连接密码
spring.datasource.password=
#DML的用户名(如果数据库专门设置了对应的用户名和密码)
spring.datasource.data-username=
#DML的密码(如果数据库专门设置了对应的用户名和密码)
spring.datasource.data-password=
#DDL的用户名(如果数据库专门设置了对应的用户名和密码)
spring.datasource.schema-username=
#DDL的密码(如果数据库专门设置了对应的用户名和密码)
spring.datasource.schema-password=
#全限定名,连接池。默认自动检测classpath
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
#sql脚本字符
spring.datasource.sql-script-encoding=UTF-8
#sql脚本分割符,默认为分号。
spring.datasource.separator=;

#dbcp2的连接参数
spring.datasource.dbcp2.*
#hikari的连接参数
spring.datasource.hikari.*
#tomcat的连接参数
spring.datasource.tomcat.*

源码-DataSourceProperties 加载对应的spring.datasource配置参数。

@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties implements BeanClassLoaderAware, InitializingBean {

	private ClassLoader classLoader;
	/**
	 * Name of the datasource. Default to "testdb" when using an embedded database.
	 */
	private String name;

	/**
	 * Whether to generate a random datasource name.
	 */
	private boolean generateUniqueName;

	/**
	 * Fully qualified name of the connection pool implementation to use. By default, it
	 * is auto-detected from the classpath.
	 */
	private Class<? extends DataSource> type;

	/**
	 * Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.
	 */
	private String driverClassName;

	/**
	 * JDBC URL of the database.
	 */
	private String url;

	/**
	 * Login username of the database.
	 */
	private String username;

	/**
	 * Login password of the database.
	 */
	private String password;

	/**
	 * JNDI location of the datasource. Class, url, username & password are ignored when
	 * set.
	 */
	private String jndiName;

	/**
	 * Initialize the datasource with available DDL and DML scripts.
	 */
	private DataSourceInitializationMode initializationMode = DataSourceInitializationMode.EMBEDDED;

	/**
	 * Platform to use in the DDL or DML scripts (such as schema-${platform}.sql or
	 * data-${platform}.sql).
	 */
	private String platform = "all";

	/**
	 * Schema (DDL) script resource references.
	 */
	private List<String> schema;

	/**
	 * Username of the database to execute DDL scripts (if different).
	 */
	private String schemaUsername;

	/**
	 * Password of the database to execute DDL scripts (if different).
	 */
	private String schemaPassword;

	/**
	 * Data (DML) script resource references.
	 */
	private List<String> data;
	/**
	 * Username of the database to execute DML scripts (if different).
	 */
	private String dataUsername;

	/**
	 * Password of the database to execute DML scripts (if different).
	 */
	private String dataPassword;

	/**
	 * Whether to stop if an error occurs while initializing the database.
	 */
	private boolean continueOnError = false;

	/**
	 * Statement separator in SQL initialization scripts.
	 */
	private String separator = ";";

	/**
	 * SQL scripts encoding.
	 */
	private Charset sqlScriptEncoding;

	private EmbeddedDatabaseConnection embeddedDatabaseConnection = EmbeddedDatabaseConnection.NONE;

	private Xa xa = new Xa();

	private String uniqueName;

源码-DataSourceAutoConfiguration 自动装配dataSource。根据DataSourceProperties参数。

@Configuration
@ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class })
@EnableConfigurationProperties(DataSourceProperties.class)
@Import({ DataSourcePoolMetadataProvidersConfiguration.class,
		DataSourceInitializationConfiguration.class })
public class DataSourceAutoConfiguration {

	@Configuration
	@Conditional(EmbeddedDatabaseCondition.class)
	@ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
	@Import(EmbeddedDataSourceConfiguration.class)
	protected static class EmbeddedDatabaseConfiguration {

	}
	
	@Configuration
	@Conditional(PooledDataSourceCondition.class)
	@ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
	@Import({ DataSourceConfiguration.Hikari.class, DataSourceConfiguration.Tomcat.class,
			DataSourceConfiguration.Dbcp2.class, DataSourceConfiguration.Generic.class,
			DataSourceJmxConfiguration.class })
	protected static class PooledDataSourceConfiguration {

	}
	
	
	/**
	 * {@link AnyNestedCondition} that checks that either {@code spring.datasource.type}
	 * is set or {@link PooledDataSourceAvailableCondition} applies.
	 */
	static class PooledDataSourceCondition extends AnyNestedCondition {

		PooledDataSourceCondition() {
			super(ConfigurationPhase.PARSE_CONFIGURATION);
		}

		@ConditionalOnProperty(prefix = "spring.datasource", name = "type")
		static class ExplicitType {

		}

		@Conditional(PooledDataSourceAvailableCondition.class)
		static class PooledDataSourceAvailable {

		}

	}

	/**
	 * {@link Condition} to test if a supported connection pool is available.
	 */
	static class PooledDataSourceAvailableCondition extends SpringBootCondition {

		@Override
		public ConditionOutcome getMatchOutcome(ConditionContext context,
				AnnotatedTypeMetadata metadata) {
			ConditionMessage.Builder message = ConditionMessage
					.forCondition("PooledDataSource");
			if (getDataSourceClassLoader(context) != null) {
				return ConditionOutcome
						.match(message.foundExactly("supported DataSource"));
			}
			return ConditionOutcome
					.noMatch(message.didNotFind("supported DataSource").atAll());
		}
		
		/**
		 * Returns the class loader for the {@link DataSource} class. Used to ensure that
		 * the driver class can actually be loaded by the data source.
		 * @param context the condition context
		 * @return the class loader
		 */
		private ClassLoader getDataSourceClassLoader(ConditionContext context) {
			Class<?> dataSourceClass = DataSourceBuilder
					.findType(context.getClassLoader());
			return (dataSourceClass == null ? null : dataSourceClass.getClassLoader());
		}

	}
	
	/**
	 * {@link Condition} to detect when an embedded {@link DataSource} type can be used.
	 * If a pooled {@link DataSource} is available, it will always be preferred to an
	 * {@code EmbeddedDatabase}.
	 */
	static class EmbeddedDatabaseCondition extends SpringBootCondition {

		private final SpringBootCondition pooledCondition = new PooledDataSourceCondition();
		
		@Override
		public ConditionOutcome getMatchOutcome(ConditionContext context,
				AnnotatedTypeMetadata metadata) {
			ConditionMessage.Builder message = ConditionMessage
					.forCondition("EmbeddedDataSource");
			if (anyMatches(context, metadata, this.pooledCondition)) {
				return ConditionOutcome
						.noMatch(message.foundExactly("supported pooled data source"));
			}
			EmbeddedDatabaseType type = EmbeddedDatabaseConnection
					.get(context.getClassLoader()).getType();
			if (type == null) {
				return ConditionOutcome
						.noMatch(message.didNotFind("embedded database").atAll());
			}
			return ConditionOutcome.match(message.found("embedded database").items(type));
		}

	}

}

测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class AppContextTest {
    @Qualifier("dataSource")
    @Autowired
    private DataSource dataSource;
	
	  @Test
    public void dataSourceTest() throws SQLException {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            int transactionIsolation = connection.getTransactionIsolation();
            String schema = connection.getSchema();
            System.out.println("transactionIsolation="+transactionIsolation+";schema="+schema);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (null != connection)
                connection.close();
        }

    }

spring boot默认采用HikariDataSource数据库连接池。

druid数据库连接池:http://blog.51cto.com/881206524/2121687