3、完善pom.xml添加
先安装Lombok
引入Lombok
org.projectlombok
lombok
1.18.4
provided
4、完善JdbcProperties
添加@Data后会自动生成set和get方法
package com.itzheng.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = “jdbc”)
@Data
public class JdbcProperties {
String url;
String driverClassName;
String username;
String password;
}
5、修改JdbcConfig类使用JdbcProperties(启用配置属性)
package com.itzheng.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
@Configuration //声明一个类作为配置类,代替xml文件
//@PropertySource(“classpath:application.properties”)
@EnableConfigurationProperties(JdbcProperties.class) //使用配置属性
public class JdbcConfig {
// @Value(“${jdbc.url}”)
// String url;
// @Value(“${jdbc.driverClassName}”)
// String driverClassName;
// @Value(“${jdbc.username}”)
// String username;
// @Value(“${jdbc.password}”)
// String password;
@Bean //声明在方法上,将方法返回值加入Bean容器,代替标签
public DataSource dataSource(JdbcProperties prop){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(prop.getDriverClassName());
dataSource.setUrl(prop.getUrl());
dataSource.setUsername(prop.getUsername());
dataSource.setPassword(prop.getPassword());
return dataSource;
}
}
6、运行代码
添加断点
7、可以在任意位置使用
1、修改JdbcProperties
2、修改JdbcConfig
@Bean //声明在方法上,将方法返回值加入Bean容器,代替标签
@ConfigurationProperties(prefix = “jdbc”)
public DataSource dataSource(){
return new DruidDataSource();
}
全部代码
package com.itzheng.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration //声明一个类作为配置类,代替xml文件
//@PropertySource(“classpath:application.properties”)
//@EnableConfigurationProperties(JdbcProperties.class) //使用配置属性
public class JdbcConfig {
// @Value(“${jdbc.url}”)
// String url;
// @Value(“${jdbc.driverClassName}”)
// String driverClassName;
// @Value(“${jdbc.username}”)
// String username;
// @Value(“${jdbc.password}”)
// String password;
// @Autowired
// JdbcProperties jdbcProperties;
//
// public JdbcConfig(JdbcProperties jdbcProperties){
// this.jdbcProperties = jdbcProperties;
// }
/*
@Bean //声明在方法上,将方法返回值加入Bean容器,代替标签
public DataSource dataSource(JdbcProperties prop){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(prop.getDriverClassName());
dataSource.setUrl(prop.getUrl());
dataSource.setUsername(prop.getUsername());
dataSource.setPassword(prop.getPassword());
return dataSource;
}
*/
@Bean //声明在方法上,将方法返回值加入Bean容器,代替标签
@ConfigurationProperties(prefix = “jdbc”)
public DataSource dataSource(){
return new DruidDataSource();
}
}
3、重新运行
1、创建application.yaml
使用更优雅的方式下配置文件
jdbc:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/itzheng
username: root
password: 123
user:
name: jack
age : 21
language:
-
java
-
python
-
andorid
-
sql
2、完善JdbcProperties
package com.itzheng.config;
import com.sun.org.apache.xpath.internal.operations.String;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
//@ConfigurationProperties(prefix = “jdbc”)
@Data
public class JdbcProperties {
String url;
String driverClassName;
String username;
String password;
User user = new User();
class User{
String name;
Integer age;
List language;
}