spring boot datasource
发表于:2025-01-23 作者:千家信息网编辑
千家信息网最后更新 2025年01月23日,application-database.properties#初始化数据库的时候,如果错误,是否继续启动。spring.datasource.continue-on-error=false#jdbc
千家信息网最后更新 2025年01月23日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安全错误
数据库的锁怎样保障安全
阿里云服务器软件开发
2台电脑数据库数据互通
软件开发公司属于什么性质
教学软件开发招聘
中职网络技术与应用练习题及答案
cod16连接线上服务器慢
工业网络技术专升本对口专业
服务器认证错误什么意思
4种网络安全威胁
开发一个网页数据库
怎么新建一个数据库
贵州电力数显钟服务器云主机
oracle 注册数据库
芜湖亨游网络技术有限公司
网络安全手抄报2020年
机器人软件开发培训学校
四川星天地网络安全招聘
软件开发类有哪些课程
北京车辆网络技术
福建软件开发定制包括什么
服务器状态未知
网上接软件开发单子
amd 服务器cpu好不好
创建数据库的简单实验小结
工商银行提示服务器错误
怎么增强人民的网络安全意识
连接数据库显示对象名字无效
计算类软件开发工具
芜湖市软件开发公司排名
用数据库制作准考证