下面列出了org.springframework.boot.autoconfigure.jdbc.DataSourceProperties#getUsername ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Bean(destroyMethod = "close")
public HikariDataSource dataSource(DataSourceProperties dataSourceProperties, ApplicationProperties applicationProperties) {
log.debug("Configuring Datasource");
HikariConfig config = new HikariConfig();
config.setDataSourceClassName(dataSourceProperties.getDriverClassName());
config.addDataSourceProperty("url", dataSourceProperties.getUrl());
if (dataSourceProperties.getUsername() != null) {
config.addDataSourceProperty("user", dataSourceProperties.getUsername());
} else {
config.addDataSourceProperty("user", ""); // HikariCP doesn't allow null user
}
if (dataSourceProperties.getPassword() != null) {
config.addDataSourceProperty("password", dataSourceProperties.getPassword());
} else {
config.addDataSourceProperty("password", ""); // HikariCP doesn't allow null password
}
return new HikariDataSource(config);
}
@Bean
public DataSource configurationDataSource(@Autowired DataSourceProperties configDataSourceProperties) {
String url = configDataSourceProperties.getUrl();
// A simple inspection is done on the JDBC URL to deduce whether to create an in-memory
// in-process database, start a file-based externally visible database or connect to
// an external database.
if (url.contains("hsql")) {
String username = configDataSourceProperties.getUsername();
String password = configDataSourceProperties.getPassword();
return HsqlDatabaseBuilder.builder()
.url(url)
.username(username)
.password(password)
.script(new ClassPathResource("sql/config-schema-hsqldb.sql"))
.build().toDataSource();
} else {
return configDataSourceProperties.initializeDataSourceBuilder().build();
}
}
@Bean
@ConfigurationProperties("c2mon.server.history.jdbc")
public DataSource historyDataSource(@Autowired DataSourceProperties historyDataSourceProperties) {
String url = historyDataSourceProperties.getUrl();
if (url.contains("hsql")) {
String username = historyDataSourceProperties.getUsername();
String password = historyDataSourceProperties.getPassword();
return HsqlDatabaseBuilder.builder()
.url(url)
.username(username)
.password(password)
.script(new ClassPathResource("sql/history-schema-hsqldb.sql")).build()
.toDataSource();
} else {
return historyDataSourceProperties.initializeDataSourceBuilder().build();
}
}
@Bean
public DataSource cacheDataSource(@Autowired DataSourceProperties cacheDataSourceProperties) {
String url = cacheDataSourceProperties.getUrl();
// A simple inspection is done on the JDBC URL to deduce whether to create an in-memory
// in-process database, start a file-based externally visible database or connect to
// an external database.
if (url.contains("hsql")) {
String username = cacheDataSourceProperties.getUsername();
String password = cacheDataSourceProperties.getPassword();
return HsqlDatabaseBuilder.builder().url(url).username(username).password(password)
.script(new ClassPathResource("sql/cache-schema-hsqldb.sql")).build().toDataSource();
} else {
return cacheDataSourceProperties.initializeDataSourceBuilder().build();
}
}
@Bean(destroyMethod = "close")
@ConditionalOnExpression("#{!environment.acceptsProfiles('cloud') && !environment.acceptsProfiles('heroku')}")
public DataSource dataSource(DataSourceProperties dataSourceProperties, JHipsterProperties jHipsterProperties) {
log.debug("Configuring Datasource");
if (dataSourceProperties.getUrl() == null) {
log.error("Your database connection pool configuration is incorrect! The application" +
" cannot start. Please check your Spring profile, current profiles are: {}",
Arrays.toString(env.getActiveProfiles()));
throw new ApplicationContextException("Database connection pool is not configured correctly");
}
HikariConfig config = new HikariConfig();
config.setDataSourceClassName(dataSourceProperties.getDriverClassName());
config.addDataSourceProperty("url", dataSourceProperties.getUrl());
if (dataSourceProperties.getUsername() != null) {
config.addDataSourceProperty("user", dataSourceProperties.getUsername());
} else {
config.addDataSourceProperty("user", ""); // HikariCP doesn't allow null user
}
if (dataSourceProperties.getPassword() != null) {
config.addDataSourceProperty("password", dataSourceProperties.getPassword());
} else {
config.addDataSourceProperty("password", ""); // HikariCP doesn't allow null password
}
if (metricRegistry != null) {
config.setMetricRegistry(metricRegistry);
}
return new HikariDataSource(config);
}