您好,登錄后才能下訂單哦!
application-database.properties
#初始化數(shù)據(jù)庫的時(shí)候,如果錯(cuò)誤,是否繼續(xù)啟動。
spring.datasource.continue-on-error=false
#jdbc driver.默認(rèn)通過uri來自動檢測。
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#使用的db名稱
spring.datasource.name=test
#是否隨機(jī)生成db名稱
spring.datasource.generate-unique-name=false
#jdbc url.連接數(shù)據(jù)庫的uri
spring.datasource.url=
#數(shù)據(jù)庫連接用戶名
spring.datasource.username=
#數(shù)據(jù)連接密碼
spring.datasource.password=
#DML的用戶名(如果數(shù)據(jù)庫專門設(shè)置了對應(yīng)的用戶名和密碼)
spring.datasource.data-username=
#DML的密碼(如果數(shù)據(jù)庫專門設(shè)置了對應(yīng)的用戶名和密碼)
spring.datasource.data-password=
#DDL的用戶名(如果數(shù)據(jù)庫專門設(shè)置了對應(yīng)的用戶名和密碼)
spring.datasource.schema-username=
#DDL的密碼(如果數(shù)據(jù)庫專門設(shè)置了對應(yīng)的用戶名和密碼)
spring.datasource.schema-password=
#全限定名,連接池。默認(rèn)自動檢測classpath
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
#sql腳本字符
spring.datasource.sql-script-encoding=UTF-8
#sql腳本分割符,默認(rèn)為分號。
spring.datasource.separator=;
#dbcp2的連接參數(shù)
spring.datasource.dbcp2.*
#hikari的連接參數(shù)
spring.datasource.hikari.*
#tomcat的連接參數(shù)
spring.datasource.tomcat.*
源碼-DataSourceProperties
加載對應(yīng)的spring.datasource配置參數(shù)。
@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。根據(jù)DataSourceProperties參數(shù)。
@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默認(rèn)采用HikariDataSource數(shù)據(jù)庫連接池。
druid數(shù)據(jù)庫連接池:https://blog.51cto.com/881206524/2121687
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。