读取配置文件
SpringBoot整合JDBC需要添加的启动器和依赖
自定义配置文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:://localhost:3306/ego?useSSL=false
jdbc.username=root
dbc.password=root
1 通过自定义配置文件方式,获取数据源
通过@PropertySource 注解读取配置文件,此注解为spring的注解,可以读取任何配置文件
/**
* @author 守鹤
* @date 2020/5/12 23:47
*/
@Configuration
@PropertySource("classpath:/jdbc.properties")
public class DataSourceConfig{
@Value("${jdbc.driverClassName}")
private String driverClassName;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource getDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(this.driverClassName);
dataSource.setUrl(this.url);
dataSource.setUsername(this.username);
dataSource.setPassword(this.password);
return dataSource;
}
}
2 通过application.properties文件读取配置信息
通过 @ConfigurationProperties 注解读取配置文件,此注解是
applicationMT4https://www.gendan5.com/opera...
SpringBoot 的注解不能读取其他配置文件,只能读取 SpringBoot 的 application 配置文件.
application.properties配置文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc//localhost:3306/ego?useSSL=false
jdbc.username=root
jdbc.password=root
2.1 自动生成配置信息属性类的对象
/**
* @author 守鹤
* @date 2020/5/13 0:06
*/
@ConfigurationProperties(prefix = "jdbc")
//prefix 前缀需要和配置文件中的前缀一致
//pojo类的属性和前缀后面的名称一致
//需要提供set方法,否则属性值无法注入
public class JdbcProperties {
private String driverClassName;
private String url;
private String username;
private String password;
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
配置信息属性类对象的使用,(注解加载类级别上)
/**
* @author 守鹤
* @date 2020/5/12 23:47
*/
@Configuration
//这个注解必须要写,指定加载哪个配置信息属性类
@EnableConfigurationProperties(JdbcProperties.class)
public class DataSourceConfig {
@Autowired
private JdbcProperties jdbcProperties;
@Bean
public DataSource getDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(jdbcProperties.getDriverClassName());
dataSource.setUrl(jdbcProperties.getUrl());
dataSource.setUsername(jdbcProperties.getUsername());
dataSource.setPassword(jdbcProperties.getPassword());
return dataSource;
}
}
2.2 自动生成配置信息属性类的对象
配置信息属性类对象的使用,(注解加载方法级别上)
@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class DataSourceConfig {
@Bean
public DataSource getDataSource(JdbcProperties jdbcProperties) {DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(jdbcProperties.getDriverClassName());
dataSource.setUrl(jdbcProperties.getUrl());
dataSource.setUsername(jdbcProperties.getUsername());
dataSource.setPassword(jdbcProperties.getPassword());
return dataSource;
2.3 自动生成配置信息属性类的对象
配置信息属性类对象的使用,(注解加载构造方法级别上)
/**
* @author 守鹤
* @date 2020/5/12 23:47
*/
@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class DataSourceConfig {
private JdbcProperties jdbcProperties;
public DataSourceConfig(JdbcProperties jdbcProperties){
this.jdbcProperties=jdbcProperties;
@Bean
public DataSource getDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(jdbcProperties.getDriverClassName());
dataSource.setUrl(jdbcProperties.getUrl());
dataSource.setUsername(jdbcProperties.getUsername());
dataSource.setPassword(jdbcProperties.getPassword());
return dataSource;
2.4 自动生成配置信息属性类的对象(优雅方式)
application.properties 中 prefix 后面的属性名称需要和DruidDataSource 中属性一致才可以注入.
因为@ConfigurationProperties注解会根据配置文件的内容,调用对象的set方法.
/**
* @author 守鹤
* @date 2020/5/12 23:47
*/
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "jdbc")
public DataSource getDataSource() {
DruidDataSource dataSource = new DruidDataSource();
return dataSource;