下面列出了org.springframework.boot.autoconfigure.jdbc.DataSourceProperties#setUrl ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Bean
@Primary
public DataSourceProperties dataSourceProperties() {
DataSourceProperties properties = new DataSourceProperties();
properties.setInitialize(false);
// dbName.serviceName example: user-db.mysql (we must )
final String serviceId = environment.getProperty(MYSQL_SERVICE_ID);
if (discoveryClient != null && serviceId != null) {
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
if (!instances.isEmpty()) {
properties.setUrl(getJdbcUrl(instances, fetchDBName(serviceId)));
} else {
LOGGER.warn("there is no services with id {} in service discovery", serviceId);
}
}
return properties;
}
@Test
public void testDatabasePropUpdate() {
TaskDefinition taskDefinition = new TaskDefinition("testTask", "testApp");
DataSourceProperties dataSourceProperties = new DataSourceProperties();
dataSourceProperties.setUsername("myUser");
dataSourceProperties.setDriverClassName("myDriver");
dataSourceProperties.setPassword("myPassword");
dataSourceProperties.setUrl("myUrl");
TaskDefinition definition = TaskServiceUtils.updateTaskProperties(
taskDefinition,
dataSourceProperties);
assertThat(definition.getProperties().size()).isEqualTo(5);
assertThat(definition.getProperties().get("spring.datasource.url")).isEqualTo("myUrl");
assertThat(definition.getProperties().get("spring.datasource.driverClassName")).isEqualTo("myDriver");
assertThat(definition.getProperties().get("spring.datasource.username")).isEqualTo("myUser");
assertThat(definition.getProperties().get("spring.datasource.password")).isEqualTo("myPassword");
}
@Bean
@Primary
@ConfigurationProperties(prefix = "datasource")
public DataSourceProperties dataSourceProperties() {
DataSourceProperties props = new DataSourceProperties();
props.setUrl(
"jdbc:h2:mem:taskana;IGNORECASE=TRUE;LOCK_MODE=0;INIT=CREATE SCHEMA IF NOT EXISTS "
+ schemaName);
return props;
}
@Bean
@Primary
@ConfigurationProperties(prefix = "datasource")
public DataSourceProperties dataSourceProperties() {
DataSourceProperties props = new DataSourceProperties();
props.setUrl(
"jdbc:h2:mem:taskana;IGNORECASE=TRUE;LOCK_MODE=0;INIT=CREATE SCHEMA IF NOT EXISTS "
+ schemaName);
return props;
}
@Bean
@Primary
@ConfigurationProperties(prefix = "datasource")
public DataSourceProperties dataSourceProperties() {
DataSourceProperties props = new DataSourceProperties();
props.setUrl(
"jdbc:h2:mem:taskana;IGNORECASE=TRUE;LOCK_MODE=0;INIT=CREATE SCHEMA IF NOT EXISTS "
+ schemaName);
return props;
}
@Bean
@Primary
@ConditionalOnBean(CloudSqlJdbcInfoProvider.class)
public DataSourceProperties cloudSqlDataSourceProperties(
GcpCloudSqlProperties gcpCloudSqlProperties,
DataSourceProperties properties,
GcpProperties gcpProperties,
CloudSqlJdbcInfoProvider cloudSqlJdbcInfoProvider) {
if (StringUtils.isEmpty(properties.getUsername())) {
properties.setUsername("root");
LOGGER.warn("spring.datasource.username is not specified. "
+ "Setting default username.");
}
if (StringUtils.isEmpty(properties.getDriverClassName())) {
properties.setDriverClassName(cloudSqlJdbcInfoProvider.getJdbcDriverClass());
}
else {
LOGGER.warn("spring.datasource.driver-class-name is specified. " +
"Not using generated Cloud SQL configuration");
}
if (StringUtils.hasText(properties.getUrl())) {
LOGGER.warn("Ignoring provided spring.datasource.url. Overwriting it based on the " +
"spring.cloud.gcp.sql.instance-connection-name.");
}
properties.setUrl(cloudSqlJdbcInfoProvider.getJdbcUrl());
if (gcpCloudSqlProperties.getCredentials().getEncodedKey() != null) {
setCredentialsEncodedKeyProperty(gcpCloudSqlProperties);
}
else {
setCredentialsFileProperty(gcpCloudSqlProperties, gcpProperties);
}
CoreSocketFactory.setApplicationName("spring-cloud-gcp-sql/"
+ this.getClass().getPackage().getImplementationVersion());
return properties;
}