Java Code Examples for org.springframework.boot.autoconfigure.jdbc.DataSourceProperties#getUsername()

The following examples show how to use org.springframework.boot.autoconfigure.jdbc.DataSourceProperties#getUsername() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DatabaseConfig.java    From spring-boot-react-blog with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 2
Source File: ConfigDataSourceConfig.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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();
   }
 }
 
Example 3
Source File: HistoryDataSourceConfig.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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();
  }
}
 
Example 4
Source File: CacheDataSourceConfig.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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();
  }
}
 
Example 5
Source File: DatabaseConfiguration.java    From expper with GNU General Public License v3.0 5 votes vote down vote up
@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);
}