SpringBoot -- 配置(yaml / properties)语法 获取配置文件信息Spring Boot Configuration Annotation...
Posted CodeJiao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot -- 配置(yaml / properties)语法 获取配置文件信息Spring Boot Configuration Annotation...相关的知识,希望对你有一定的参考价值。
01:SpringBoot – 配置(yaml / properties)语法
02:SpringBoot – 配置(yaml / properties)语法 获取配置文件信息、Spring Boot Configuration Annotation…
03:SpringBoot – 配置:多环境配置Profile
1. 读取配置文件内容
读取配置文件有三种方式:
- @Value
- Environment
- @ConfigurationProperties
1.1 @Value注解获取单个值
myName: tianjiao
gender: male
person:
name: zhangsan
gender: ${gender} # 引用上边定义的gender值
msg1: 'hello \\n world' # 单引忽略转义字符
msg2: "hello \\n world" # 双引识别转义字符
获取属性的值:
package com.tian.demo.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Value("${myName}")
private String username;
@Value("${person.name}")
private String name;
@Value("${person.gender}")
private String gender;
@Value("${msg1}")
private String str1;
@Value("${msg2}")
private String str2;
@RequestMapping("/hello")
public String hello() {
System.out.println("myName: " + username);
System.out.println("name: " + name);
System.out.println("gender: " + gender);
System.out.println("str1: " + str1);
System.out.println("str2: " + str2);
return "HELLO SPRINGBOOT!!!";
}
}
1.2 Environment
myName: tianjiao
gender: male
person:
name: zhangsan
gender: ${gender} # 引用上边定义的gender值
msg1: 'hello \\n world' # 单引忽略转义字符
msg2: "hello \\n world" # 双引识别转义字符
获取属性的值:
package com.tian.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private Environment environment;
@RequestMapping("/hello")
public String hello() {
System.out.println("myName: " + environment.getProperty("myName"));
System.out.println("name: " + environment.getProperty("person.name"));
System.out.println("gender: " + environment.getProperty("person.gender"));
System.out.println("str1: " + environment.getProperty("msg1"));
System.out.println("str2: " + environment.getProperty("msg2"));
return "HELLO SPRINGBOOT!!!";
}
}
运行结果:
1.3 @ConfigurationProperties
配置信息:
person:
name: zhangsan
gender: male
address:
- beijing
- shanghai
实体类:
package com.tian.demo.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Arrays;
/**
* ClassName: Person
* Description:
*
* @author Tianjiao
* @date 2021/11/15 15:15
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private String gender;
private String[] address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String[] getAddress() {
return address;
}
public void setAddress(String[] address) {
this.address = address;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\\'' +
", gender='" + gender + '\\'' +
", address=" + Arrays.toString(address) +
'}';
}
}
获取属性值:
1.4 补充:@value 和 Environment获取数组值
说明:不可以直接获取address这个数组。
@Value:
Environment:
2. Spring Boot Configuration Annotation Processor not configured
2.1 问题探究
Spring Boot Configuration Annotation Processor not configured
:Spring Boot 配置注解处理器未配置
配置注解处理器的好处是什么?
配置注解处理器配置完成后,当执行类中已经定义了对象和该对象的字段后,在配置文件中对该类赋值时,便会非常方便的弹出提示信息。
2.2 解决方案
导入相关依赖
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
现在写配置信息的时候就有提示了
以上是关于SpringBoot -- 配置(yaml / properties)语法 获取配置文件信息Spring Boot Configuration Annotation...的主要内容,如果未能解决你的问题,请参考以下文章