类org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder源码实例Demo

下面列出了怎么用org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: tinyid   文件: DataSourceConfig.java
private DataSource buildDataSource(String dataSourceType, Map<String, Object> dsMap) {
    try {
        String className = DEFAULT_DATASOURCE_TYPE;
        if (dataSourceType != null && !"".equals(dataSourceType.trim())) {
            className = dataSourceType;
        }
        Class<? extends DataSource> type = (Class<? extends DataSource>) Class.forName(className);
        String driverClassName = dsMap.get("driver-class-name").toString();
        String url = dsMap.get("url").toString();
        String username = dsMap.get("username").toString();
        String password = dsMap.get("password").toString();

        return DataSourceBuilder.create()
                .driverClassName(driverClassName)
                .url(url)
                .username(username)
                .password(password)
                .type(type)
                .build();

    } catch (ClassNotFoundException e) {
        logger.error("buildDataSource error", e);
        throw new IllegalStateException(e);
    }
}
 
private void runScripts(DataSource dataSource, List<Resource> resources, String username, String password) {
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.setContinueOnError(this.properties.isContinueOnError());
    populator.setSeparator(this.properties.getSeparator());
    if (this.properties.getSqlScriptEncoding() != null) {
        populator.setSqlScriptEncoding(this.properties.getSqlScriptEncoding().name());
    }
    for (Resource resource : resources) {
        populator.addScript(resource);
    }
    if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
        dataSource = DataSourceBuilder.create(this.properties.getClassLoader())
                .driverClassName(this.properties.determineDriverClassName())
                .url(this.properties.determineUrl())
                .username(username)
                .password(password)
                .build();
    }
    DatabasePopulatorUtils.execute(populator, dataSource);
}
 
@Bean
public JdbcTemplate postgresJdbcTemplate(
		@Value("${postgres.jdbcUrl}") String postgresJdbcUrl,
		@Value("${postgres.jdbcDriver}") String postgresJdbcDriver,
		@Value("${postgres.jdbcUser}") String postgresJdbcUser,
		@Value("${postgres.jdbcPassword}") String postgresJdbcPassword) {

	DataSource targetDataSource = DataSourceBuilder
			.create()
			.driverClassName(postgresJdbcDriver)
			.url(postgresJdbcUrl)
			.username(postgresJdbcUser)
			.password(postgresJdbcPassword)
			.build();

	return new JdbcTemplate(targetDataSource);
}
 
源代码4 项目: klask-io   文件: DatabaseConfiguration.java
@Bean(destroyMethod = "close")
@ConditionalOnExpression("#{!environment.acceptsProfiles('" + Constants.SPRING_PROFILE_CLOUD + "') && !environment.acceptsProfiles('" + Constants.SPRING_PROFILE_HEROKU + "')}")
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public DataSource dataSource(DataSourceProperties dataSourceProperties) {
    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");
    }
    HikariDataSource hikariDataSource =  (HikariDataSource) DataSourceBuilder
            .create(dataSourceProperties.getClassLoader())
            .type(HikariDataSource.class)
            .driverClassName(dataSourceProperties.getDriverClassName())
            .url(dataSourceProperties.getUrl())
            .username(dataSourceProperties.getUsername())
            .password(dataSourceProperties.getPassword())
            .build();

    if (metricRegistry != null) {
        hikariDataSource.setMetricRegistry(metricRegistry);
    }
    return hikariDataSource;
}
 
@Bean(destroyMethod = "close")
@ConditionalOnExpression("#{!environment.acceptsProfiles('" + Constants.SPRING_PROFILE_CLOUD + "') && !environment.acceptsProfiles('" + Constants.SPRING_PROFILE_HEROKU + "')}")
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public DataSource dataSource(DataSourceProperties dataSourceProperties) {
    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");
    }
    HikariDataSource hikariDataSource =  (HikariDataSource) DataSourceBuilder
            .create(dataSourceProperties.getClassLoader())
            .type(HikariDataSource.class)
            .driverClassName(dataSourceProperties.getDriverClassName())
            .url(dataSourceProperties.getUrl())
            .username(dataSourceProperties.getUsername())
            .password(dataSourceProperties.getPassword())
            .build();

    if (metricRegistry != null) {
        hikariDataSource.setMetricRegistry(metricRegistry);
    }
    return hikariDataSource;
}
 
@Bean(destroyMethod = "close")
@ConditionalOnExpression("#{!environment.acceptsProfiles('" + Constants.SPRING_PROFILE_CLOUD + "') && !environment.acceptsProfiles('" + Constants.SPRING_PROFILE_HEROKU + "')}")
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public DataSource dataSource(DataSourceProperties dataSourceProperties) {
    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");
    }
    HikariDataSource hikariDataSource =  (HikariDataSource) DataSourceBuilder
            .create(dataSourceProperties.getClassLoader())
            .type(HikariDataSource.class)
            .driverClassName(dataSourceProperties.getDriverClassName())
            .url(dataSourceProperties.getUrl())
            .username(dataSourceProperties.getUsername())
            .password(dataSourceProperties.getPassword())
            .build();

    if (metricRegistry != null) {
        hikariDataSource.setMetricRegistry(metricRegistry);
    }
    return hikariDataSource;
}
 
@Bean(destroyMethod = "close")
@ConditionalOnExpression("#{!environment.acceptsProfiles('" + Constants.SPRING_PROFILE_CLOUD + "') && !environment.acceptsProfiles('" + Constants.SPRING_PROFILE_HEROKU + "')}")
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public DataSource dataSource(DataSourceProperties dataSourceProperties) {
    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");
    }
    HikariDataSource hikariDataSource =  (HikariDataSource) DataSourceBuilder
            .create(dataSourceProperties.getClassLoader())
            .type(HikariDataSource.class)
            .driverClassName(dataSourceProperties.getDriverClassName())
            .url(dataSourceProperties.getUrl())
            .username(dataSourceProperties.getUsername())
            .password(dataSourceProperties.getPassword())
            .build();

    if (metricRegistry != null) {
        hikariDataSource.setMetricRegistry(metricRegistry);
    }
    return hikariDataSource;
}
 
源代码8 项目: OpenIoE   文件: DatabaseConfiguration.java
@Bean(destroyMethod = "close")
@ConditionalOnExpression("#{!environment.acceptsProfiles('" + Constants.SPRING_PROFILE_CLOUD + "') && !environment.acceptsProfiles('" + Constants.SPRING_PROFILE_HEROKU + "')}")
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public DataSource dataSource(DataSourceProperties dataSourceProperties) {
    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");
    }
    HikariDataSource hikariDataSource =  (HikariDataSource) DataSourceBuilder
            .create(dataSourceProperties.getClassLoader())
            .type(HikariDataSource.class)
            .driverClassName(dataSourceProperties.getDriverClassName())
            .url(dataSourceProperties.getUrl())
            .username(dataSourceProperties.getUsername())
            .password(dataSourceProperties.getPassword())
            .build();

    if (metricRegistry != null) {
        hikariDataSource.setMetricRegistry(metricRegistry);
    }
    return hikariDataSource;
}
 
源代码9 项目: kkbinlog   文件: BinLogClientFactory.java
private DataSource getDataSource(BinaryLogConfig binaryLogConfig) {
    return DataSourceBuilder
            .create(BinLogClientFactory.class.getClassLoader())
            .type(HikariDataSource.class)
            .driverClassName(binaryLogConfig.getDriverClassName())
            .url(binaryLogConfig.getDataSourceUrl())
            .username(binaryLogConfig.getUsername())
            .password(binaryLogConfig.getPassword()).build();
}
 
源代码10 项目: md_blockchain   文件: DataSourceConfiguration.java
@Bean(destroyMethod = "", name = "EmbeddeddataSource")
public DataSource dataSource() {
    DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
    dataSourceBuilder.driverClassName("org.sqlite.JDBC");
    dataSourceBuilder.url("jdbc:sqlite:" + dbName);
    dataSourceBuilder.type(SQLiteDataSource.class);
    return dataSourceBuilder.build();
}
 
源代码11 项目: TAC   文件: AppConfiguration.java
@Bean
@ConfigurationProperties(prefix = "spring.datasource.tac")
public DataSource dataSource() {
    // 使用内嵌的hsqldb作为DEMO的测试
    // return new EmbeddedDatabaseBuilder().addScript("schema.sql").build();
    // 日常使用spring-data数据源或者其他开源的数据库连接池
     return DataSourceBuilder.create().build();
}
 
public DataSource tccDataSource() {
    return DataSourceBuilder.create()
            .type(properties.getType())
            .driverClassName(properties.getDriverClassName())
            .url(properties.getUrl())
            .username(properties.getUsername())
            .password(properties.getPassword())
            .build();
}
 
public DataSource tccDataSource() {
    return DataSourceBuilder.create()
            .type(properties.getType())
            .driverClassName(properties.getDriverClassName())
            .url(properties.getUrl())
            .username(properties.getUsername())
            .password(properties.getPassword())
            .build();
}
 
public DataSource tccDataSource() {
    return DataSourceBuilder.create()
            .type(properties.getType())
            .driverClassName(properties.getDriverClassName())
            .url(properties.getUrl())
            .username(properties.getUsername())
            .password(properties.getPassword())
            .build();
}
 
源代码15 项目: SpringBoot-Base-System   文件: DataSourceConfig.java
@Bean(name = "master")
@Qualifier("master")
/**
 * 「多数据源配置的时候注意,必须要有一个主数据源,用 Primary 标志该 Bean」
 * 
 * @time 2018年4月10日 下午4:59:04
 * @version V1.0
 * @return DataSource
 */
@Primary /* @Primary 标志这个 Bean 如果在多个同类 Bean 候选时,该 Bean 优先被考虑 */
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource primaryDataSource() {
	return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
}
 
源代码16 项目: learning-code   文件: DataSourceConfig.java
/**
 * 注册 data source
 *
 * @return
 */
@ConfigurationProperties(prefix = "spring.datasource")
@Bean("firstDataSource")
@Primary // 有相同实例优先选择
public DataSource firstDataSource() {
    return DataSourceBuilder.create().build();
}
 
@Bean
@ConfigurationProperties("calcite.datasource")
public DataSource calciteDataSource(@Autowired String inlineModel) {
	DataSource dataSource = DataSourceBuilder
			.create()
			.driverClassName("org.apache.calcite.jdbc.Driver")
			.url("jdbc:calcite:lex=JAVA;model=inline:" + inlineModel)
			.build();

	log.info("Generated Calcite mode: " + inlineModel);
	return dataSource;
}
 
源代码18 项目: MI-S   文件: DataBaseConfiguration.java
/**
 * 主库配置(负责写)
 * @return
 */
@Bean(name="masterDataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource writeDataSource() {
    log.info("-------------------- Master DataSource init ---------------------");
    return DataSourceBuilder.create().type(dataSourceType).build();
}
 
源代码19 项目: MI-S   文件: DataBaseConfiguration.java
/**
 * 主库配置(负责写)
 * @return
 */
@Bean(name="masterDataSource", destroyMethod = "close", initMethod="init")
@Primary
@ConfigurationProperties(prefix = "spring.datasource",locations = "classpath:application.properties")
public DataSource writeDataSource() {
    log.info("-------------------- Master DataSource init ---------------------");
    return DataSourceBuilder.create().type(dataSourceType).build();
}
 
源代码20 项目: MI-S   文件: DataBaseConfiguration.java
/**
 * 从库配置(负责读)
 * @return
 */
@Bean(name = "slaveDataSourceOne")
@ConfigurationProperties(prefix = "spring.slave",locations = "classpath:application.properties")
public DataSource readDataSourceOne(){
    log.info("-------------------- Slave DataSource One init ---------------------");
    return DataSourceBuilder.create().type(dataSourceType).build();
}
 
@Bean(name = "dataSource")
public DataSource getDataSource() {
    DataSource dataSource = DataSourceBuilder
            .create()
            .username(username)
            .password(password)
            .url(url)
            .driverClassName(driverClassName)
            .build();
    return dataSource;
}
 
@Bean(name = "dataSource")
public DataSource getDataSource() {
    DataSource dataSource = DataSourceBuilder
            .create()
            .username(username)
            .password(password)
            .url(url)
            .driverClassName(driverClassName)
            .build();
    return dataSource;
}
 
源代码23 项目: MonitorClient   文件: DBConfig.java
@Bean
public DataSource dataSource() {
    DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
    dataSourceBuilder.driverClassName("org.sqlite.JDBC");
    dataSourceBuilder.url("jdbc:sqlite:config/config.db");
    return dataSourceBuilder.build();
}
 
源代码24 项目: MonitorClient   文件: DBConfig.java
/**
 * 索引数据库配置
 * @data 2017年3月11日
 * @param indexPath 数据库存储位置
 * @return
 */
public DataSource indexDataSource(String indexPath) {
    DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
    dataSourceBuilder.driverClassName("org.sqlite.JDBC");
    dataSourceBuilder.url("jdbc:sqlite:"+indexPath);
    return dataSourceBuilder.build();
}
 
源代码25 项目: monolith   文件: TeiidTestDataSources.java
@Bean
public DataSource legacyDS() {

    DataSource dataSource = DataSourceBuilder.create().driverClassName("org.h2.Driver").username("sa").password("").url("jdbc:h2:mem:legacydb;DB_CLOSE_ON_EXIT=FALSE;DATABASE_TO_UPPER=FALSE;MODE=MYSQL").build();

    Resource initSchema = new ClassPathResource("h2/scripts/legacydb-schema.sql");
    Resource initData = new ClassPathResource("h2/scripts/legacydb-data.sql");
    DatabasePopulator databasePopulator = new ResourceDatabasePopulator(initSchema, initData);
    DatabasePopulatorUtils.execute(databasePopulator, dataSource);
    return dataSource;
}
 
源代码26 项目: monolith   文件: TeiidTestDataSources.java
@Bean
public DataSource ordersDS() {
    DataSource dataSource = DataSourceBuilder.create().driverClassName("org.h2.Driver").username("sa").password("").url("jdbc:h2:mem:ordersdb;DB_CLOSE_ON_EXIT=FALSE;DATABASE_TO_UPPER=FALSE;MODE=MYSQL").build();

    Resource initSchema = new ClassPathResource("h2/scripts/ordersdb-schema.sql");
    Resource initData = new ClassPathResource("h2/scripts/ordersdb-data.sql");
    DatabasePopulator databasePopulator = new ResourceDatabasePopulator(initSchema, initData);
    DatabasePopulatorUtils.execute(databasePopulator, dataSource);
    return dataSource;
}
 
@Bean(name="jdbchdfsDataSource")
public DataSource jdbcHdfsDataSource() {
	DataSource dataSource;
	if(props.getUrl() != null && props.getUsername() != null) {
		dataSource = DataSourceBuilder.create().driverClassName(props.getDriverClassName())
			.url(props.getUrl())
			.username(props.getUsername())
			.password(props.getPassword()).build();
	} else {
		dataSource = getDefaultDataSource();
	}
	return dataSource;
}
 
源代码28 项目: WebIDE-Backend   文件: JpaConfig.java
@Bean
public DataSource dataSource() {
    DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
    dataSourceBuilder.driverClassName(driverClassName);
    dataSourceBuilder.url(url);
    dataSourceBuilder.type(HikariDataSource.class);
    return dataSourceBuilder.build();
}
 
源代码29 项目: javabase   文件: DataSourceConfiguration.java
@Bean(name = "writeDataSource")
@Primary
@ConfigurationProperties(prefix = "datasource.write")
public DataSource writeDataSource() {
	log.info("-------------------- writeDataSource init ---------------------");
	return DataSourceBuilder.create().type(dataSourceType).build();
}
 
源代码30 项目: javabase   文件: DataSourceConfiguration.java
/**
 * 有多少个从库就要配置多少个
 * @return
 */
@Bean(name = "readDataSource1")
@ConfigurationProperties(prefix = "datasource.read1")
public DataSource readDataSourceOne() {
	log.info("-------------------- readDataSourceOne init ---------------------");
	return DataSourceBuilder.create().type(dataSourceType).build();
}
 
 同包方法