Springboot读取yaml配置⽂件的三种⽅式(以及static注⼊失效的解决⽅式和原
理)描写秋天的一段话
⽂章⽬录
前⾔
初衷:
2.总结⼀下对项⽬配置⽂件整体或者拆分处理
注意:@PropertySource⽅式只对properties⽂件可以进⾏加载,但对于yml或者yaml不能⽀持。
⾸先看⼀下yaml部分配置:
spring:
# 数据库配置
datasource:
druid:
driver-class-name: oracle.jdbc.OracleDriver
王珂王新url: jdbc:oracle:thin:@:1521/orcl
username: scott
password: 12345
正⽂
1.@value+@Configuration对配置的全局处理
迷离夜 莫绮雯1.1 正确写法
1.新建全局配置类Confignature,⽤于配置整个yaml的配置信息。(正确写法)
由下⾯可以看到,有2种写法
(1)直接在属性上⾯@value注解(代码简洁,但是每次使⽤需要先new⼀个Confignature对象,或者@Autowired先注⼊):driver-class-name
(2)属性上⾯加static关键字修饰,@value写在对应set⽅法上⾯(不需要new,可以其他类直接调⽤):url,username,password
@Value(value = "${spring.datasource.druid.driver-class-name}")
public String DATASOURCE_DRUID_DRIVER_CLASS_NAME; //oracle驱动
public static String DATASOURCE_DRUID_URL; //oracle url
public static String DATASOURCE_DRUID_USERNAME; // oracle ⽤户名
public static String DATASOURCE_DRUID_PASSWORD; // oracle 密码
@Value("${spring.datasource.druid.url}")
public void setDatasourceDruidUrl(String datasourceDruidUrl) {
DATASOURCE_DRUID_URL = datasourceDruidUrl;
}
@Value("${spring.datasource.druid.username}")
public void setDatasourceDruidUsername(String datasourceDruidUsername) {
DATASOURCE_DRUID_USERNAME = datasourceDruidUsername;
}
@Value("${spring.datasource.druid.password}")
public void setDatasourceDruidPassword(String datasourceDruidPassword) {
DATASOURCE_DRUID_PASSWORD = datasourceDruidPassword;
}
}
2.写⼀个controller测试⼀下
@Autowired
private Confignature confignature;
@GetMapping("/testSpringBoot")
@ResponseBody
public void testSpringBoot() {
// driver属性需要先注⼊confignature配置类
System.out.println(confignature.DATASOURCE_DRUID_DRIVER_CLASS_NAME); // url,usernam,password属性可以直接调⽤
System.out.println(Confignature.DATASOURCE_DRUID_URL);
System.out.println(Confignature.DATASOURCE_DRUID_USERNAME);
System.out.println(Confignature.DATASOURCE_DRUID_PASSWORD);
}
如图所⽰:成功获取值
1.2 错误写法
错误写法:
(1)@value注解和static同时在属性上⾯
(2)static修饰属性,但是set⽅法⽤了static修饰
// 第⼀种错误写法
@Value(value = "${spring.datasource.druid.driver-class-name}")
public static String DATASOURCE_DRUID_DRIVER_CLASS_NAME; //oracle驱动
/
/ 第⼆种错误写法
public static String DATASOURCE_DRUID_URL; //oracle url
// 正确写法
public static String DATASOURCE_DRUID_USERNAME; // oracle ⽤户名
// 正确写法
public static String DATASOURCE_DRUID_PASSWORD; // oracle 密码
@Value("${spring.datasource.druid.url}")
public static void setDatasourceDruidUrl(String datasourceDruidUrl) {
DATASOURCE_DRUID_URL = datasourceDruidUrl;
}
@Value("${spring.datasource.druid.username}")
读取配置文件失败
public void setDatasourceDruidUsername(String datasourceDruidUsername) {
DATASOURCE_DRUID_USERNAME = datasourceDruidUsername;
}
@Value("${spring.datasource.druid.password}")
public void setDatasourceDruidPassword(String datasourceDruidPassword) {
DATASOURCE_DRUID_PASSWORD = datasourceDruidPassword;
}
}
测试⼀下:
2.ConfigurationProperties注解对配置进⾏拆分处理
1.新建⼀个配置类DatasourceConfig,注意属性名称要跟yaml配置⽂件⼀致。这⾥罗列正确和错误写法(不能⽤static修饰属性)正确:driverClassName,url,username
错误:password
这种写法优势:当yaml配置⽂件配置信息过多,可以多写⼏个DatasourceConfig针对不同的配置,这样结构更为清晰
@Setter
@Component
@ConfigurationProperties(prefix = "spring.datasource.druid")
public class DatasourceConfig {
private String driverClassName;
private String url ;
private String username;
// 错误写法
private static String password;
}
2.新写⼀个controller,这种⽅式就必须要@Autowired注⼊DatasourceConfig 配置类
@Autowired
private DatasourceConfig datasourceConfig;
@GetMapping("/testSpringBoot1")
michael jackson图片@ResponseBody
public void testSpringBoot1() {
System.out.DriverClassName());
System.out.Url());
System.out.Username());
}
测试⼀下:
由图可知,取得了3个值,password未能获取,原因在于属性⽤了static修饰
3.Environment读取yaml配置⽂件
1.新写⼀个controller,注⼊Environment 类
@Autowired
private Environment env;
描写心理活动的成语@GetMapping("/testSpringBoot2")
@ResponseBody
public void testSpringBoot2() {
System.out.Property("spring.datasource.druid.driver-class-name"));
System.out.Property("spring.datasource.druid.url"));
System.out.Property("spring.datasource.druid.username"));
System.out.Property("spring.datasource.druid.password"));
}
pom相关依赖
<!--配置类相关-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional>
</dependency>
<!--druid相关-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!--oracle相关-->
<dependency>
<groupId&acle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<!--lomback-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
<scope>provided</scope>
</dependency>
源码解读为什么@value不能注⼊static
发布评论