SpringBoot的配置类
//这种配置适合出现在集体开发中
@Configuration
@EnableConfigurationProperties(jdbcProperties.class)
public class jdbc {
@Bean
public DruidDataSource getDataSource(jdbcProperties properties){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUrl(properties.getUrl());
druidDataSource.setDriverClassName(properties.getUsername());
druidDataSource.setUsername(properties.getUsername());
druidDataSource.setPassword(properties.getPassword());
return druidDataSource;
}
}
@Configuration 表明这是个配置类
@EnableConfigurationProperties(**.class) 导入properties类
@ConfigurationProperties(prefix = “jdbc”)
@Data
public class jdbcProperties {
String url;
String driverClassName;
String username;
String password;
}
@Data 是一个插件来简化代码;帮我们自动生成get set等方法
@ConfigurationProperties 和application.properties文件关联
//这种方式适合出现个人配置
@Bean
@ConfigurationProperties(prefix=“jdbc”)
public DruidDataSource getDataSource(){
return new DruidDataSource();
}