spring boot datasource
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,application-database.properties#初始化数据库的时候,如果错误,是否继续启动。spring.datasource.continue-on-error=false#jdbc
千家信息网最后更新 2026年10月31日spring boot datasource
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.连接数据库的urispring.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=#全限定名,连接池。默认自动检测classpathspring.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 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 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)@SpringBootTestpublic 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数据库连接池:https://blog.51cto.com/881206524/2121687
数据
数据库
密码
用户
用户名
参数
名称
源码
脚本
检测
分号
字符
时候
错误
测试
生成
装配
配置
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
手冲咖啡和咖啡机区别
互联网科技名片
互联网科技排名
浩特互联网科技
深圳互联网科技怎么样
海南科技产业互联网平台
互联网科技公司招聘
深圳国际航运科技和互联网
互联网之后的科技是什么
来此购互联网科技有限公司
岳阳华臻互联网科技有限公司
互联网科技革命什么开始
晓花互联网科技有限公司网址
互联网科技商业
明优互联网科技有限公司
雨阳互联网卡科技有限公司
易企秀互联网科技是做什么的
西安有哪些互联网高科技企业
中国科技和美国互联网
长沙易易互联网科技有限公司
喔噻互联网科技有限公司
互联网佳科技有限公司
佩信高科技互联网
银川新华互联网科技学校地址
北京大学互联网科技怎么样
互联网企业和科技型企业
互联网科技项目计划书材料类
佛山市翊博互联网络科技有限公司
四川慕妮娅互联网科技有限公司
上海旺乐尼互联网科技有限公司