SpringBoot之十二:SpringBoot读取配置文件

一、读取.properties文件

本质上,是Spring的注解读取。


@Configuration
@ComponentScan(basePackages ="com.yang")
@PropertySource(value= {"classpath:db.properties"},ignoreResourceNotFound=true)
public class SpringConfig {


    @Value("${url}")
    private String jdbcUrl;

    @Value("${driver}")
    private String jdbcDriverClassName;

    @Value("${username}")
    private String jdbcUsername;


    @Bean(destroyMethod = "close")
    public DataSource dataSource() {
        BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
        // 数据库驱动
        boneCPDataSource.setDriverClass(jdbcDriverClassName);
        // 相应驱动的jdbcUrl
        boneCPDataSource.setJdbcUrl(jdbcUrl);
        // 数据库的用户名
        boneCPDataSource.setUsername(jdbcUsername);
        // 数据库的密码
        boneCPDataSource.setPassword(jdbcUsername);
        // 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0
        boneCPDataSource.setIdleConnectionTestPeriodInMinutes(60);
        // 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0
        boneCPDataSource.setIdleMaxAgeInMinutes(30);
        // 每个分区最大的连接数
        boneCPDataSource.setMaxConnectionsPerPartition(100);
        // 每个分区最小的连接数 
        boneCPDataSource.setMinConnectionsPerPartition(5);
        return boneCPDataSource;
    }


}

你可能感兴趣的:(SpringBoot,Spring)