springBoot 配置文件分类和获取配置文件的值

时间:2025-02-13 09:31:06

一、配置文件分类

        SpringBoot 是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用 或者 )进行配置。

1.1 优先级

        在同一级目录下优先级为:properties > yml > yaml

1.2 properties

        保存数据的格式如下:

=8080

1.3 yml

        保存数据的格式如下:

server:
  port: 8080

二、YAML

2.1 简介

        YAML 全称是 YAML Ain't Markup LanguageYAML 是一种直观的能够被电脑识别的的数据数据序列化格式,并且容易被人类阅读,容易和脚本语言交互的,可以被支持 YAML 库的不同的编程语言程序导入,比如: C/C++、Ruby、Python、 Java、Perl、C#、PHP 等。YML 文件是以数据为核心的,比传统的 xml 方式更加简洁。

        YAML 文件的扩展名可以使用 .yml 或者 .yaml

2.2 基本语法

        1、大小写敏感

        2、数据值前边必须有空格,作为分隔符

        3、使用缩进表示层级关系缩进时不允许使用 Tab 键,只允许使用空格(各个系统 Tab 对应的 空格数目可能不同,导致层次混乱)。

        4、缩进的空格数目不重要,只要相同层级的元素左侧对齐即可

        5、# 表示注释,从这个字符一直到行尾,都会被解析器忽略

server:
  port: 8080
  address: 127.0.0.1

2.3 数据格式

2.3.1 对象

        类似于 map键值对的集合。写法如下:

person:
  name: zhangsan

# 想要写一行的写法
person: {name: zhangsan}

2.3.2 数组

        一组按次序排列的值

address:
  - beijing
  - shanghai

# 想要写一行的写法
address: [beijing,shanghai]

2.3.3 单个值

# 单引忽略转义字符
msg1: 'hello \n world'

 # 双引识别转义字符
msg2: "hello \n world"

2.3.4 参数引用

name: lisi

person:
  name: ${name} # 引用上边定义的 name 值

三、获取默认配置文件的值

        假设此时有一个 文件,内容如下:

person:
  name: zhangsan
  age: 18
  address:
    - beijing
    - shanghai

name: 李四
age:29

# 单引忽略转义字符
msg1: 'hello \n world'
# 双引识别转义字符
msg2: "hello \n world"

3.1 @Value

@RestController
public class HelloWorld {

    @Value("${}")
    private String name;

    @Value("${age}")
    private int age;

    @RequestMapping("/hello")
    public String hello() {
        (name);
        (age);
        return "hello springboot";
    }
}

3.2 Environment

@RestController
public class HelloWorld {

    @Autowired
    private Environment env;
    
    @RequestMapping("/hello")
    public String Hello() {
        String property = ("");
        String property2 = ("address[0]");
        (property+":"+property2);
        return "Hello SpringBoot";
    }
}

3.3 @ConfigurationProperties

        使用 @ConfigurationPorperties 注解。需要先创建一个 Person 类用来接收参数,代码如下:

@Component
// 这个前缀的名称要和 properties 配置文件的前缀对应上
// 否则取的 name 的值就是 lisi 了
@ConfigurationProperties(prefix="person")
public class Person {
	
	private String name;
	private int age;
	private String [] address;

	// setter、getter、toString
	
}
@RestController
public class HelloWorld {

    @Autowired
    Person person;

    @RequestMapping("/hello")
    public String hello() {
        (());
        (());
        (().length);
        return "hello springboot";
    }
}

3.4 ApplicationContext

        可以通过 ApplicationContext 上下文获取参数值,代码如下

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
    	ConfigurableApplicationContext app = (, args);
    	(().getProperty("name"));
    	(().getProperty("age"));
    }
}

四、获取自定义配置文件的值

        在 resources 目录下新创建一个配置文件 ,内容如下,该如何取值呢?

=http://127.0.0.1:8081/dwwdw
=1221
address=tianjinshi
port=12345

4.1 @Value

        还可以使用 @Value 注解获取。

@RestController
public class HelloWorldController {

	@Value("${name}")
	private String name;
	
	@Value("${age}")
	private int age;
		
	@Value("${address}")
	private String address;
	
	@Value("${port}")
	private int port;
 
	@RequestMapping("/getSpringSingle") 
	public String getSpringSingle() { 
            return  "address:"+address+"port"+port; 
	}
}

4.2 @ConfigurationPorperties

        先创建一个 SpringBean 类,因为有多个配置文件,需要使用 @PropertySource 注解来指定加载的配置文件

@Component
@ConfigurationProperties(prefix="springBean")
@PropertySource(value="classpath:",encoding = "utf-8")
public class SpringBean {
	
	private String address;
	
	private int port;

    // getter、setter、toString
}

       接口代码如下:

@RestController
public class HelloWorldController {

	@Autowired
	SpringBean springBean;
	
	@RequestMapping("/getSpring") 
	public String getSpring() { 
		return  (); 
	}

}

4.3 ApplicationContext

        我们可以通过 ApplicationContext 上下文获取参数值,代码如下

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
    	ConfigurableApplicationContext app = (, args);
    	(().getProperty("name"));
    	(().getProperty("age"));
    	(().getProperty("address"));
    	(().getProperty("port"));
    	
    }
}