spring boot datasource
发表于:2025-02-24 作者:千家信息网编辑
千家信息网最后更新 2025年02月24日,application-database.properties#初始化数据库的时候,如果错误,是否继续启动。spring.datasource.continue-on-error=false#jdbc
千家信息网最后更新 2025年02月24日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安全错误
数据库的锁怎样保障安全
东莞南城软件开发公司
网络安全实习报告总结4000字
网络安全法考试找不到入口
杭州银行子公司软件开发
软件开发需要哪些资质证书
爬虫网易云数据库
软件开发及导入
甘特图数据库设计
我国网络安全现状图片
国内服务器搭建翻墙
zabbix本机无数据库
网络安全征文优秀题目
传统软件开发的特点
吉林省网络安全宣传周校园日
磐石网络技术有限公司和有道
普通软件开发岗位年薪
《网络安全法》文件图文
浦东新区市场软件开发包括什么
mc遗忘之地服务器
重庆品质软件开发服务有哪些
如何在gis图中加入数据库
在数据库查询某条数据
成人高考报考计算机网络技术
中国技术信息数据库
简述互联网软件开发
4个系统级数据库
我的世界服务器域名
服务器上市公司 H股
计算机网络技术与应用 课件
网络安全动态防御