SpringBoot学习要点记录(二)----读取配置文件@Value @ConfigurationProperties @PropertySource注解使用

一、使用属性文件

在Spring Boot中使用属性文件,默认读取application.properties,读取方法有很多,这里只介绍最常用的方法。先添加Maven依赖,添加后Spring Boot将创建读取属性文件的上下文。

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-configuration-processor</artifactId>
	<optional>true</optional>
</dependency>

1.1方式一:使用@Value注解

1.新建springBoot项目,在application.properties文件中加入以下配置

database.driverName=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/test
database.username=root
database.password=l23456

相关代码:

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
public class DataBaseProperties {

	@Value("${database.driverName}")
	private String driverName = null;
	@Value("${database.url}")
	private String url = null;
	private String username = null;
	private String password = null;
	
	public void setDriverName(String driverName) {
		this.driverName = driverName;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	@Value("${database.username}")
	public void setUsername(String username) {
		this.username = username;
	}
	@Value("${database.password}")
	public void setPassword(String password) {
		this.password = password;
	}
	public String getDriverName() {return driverName;}
	public String getUrl() {return url;}
	public String getUsername() {return username;}
	public String getPassword() {return password;}
}

@Value注解既可以加载属性上也可以加在方法上。

测试

@SpringBootTest
class DemoApplicationTests {
	@Autowired DataBaseProperties dp;

	@Test
	void contextLoads() {
		System.out.println(dp.getDriverName());
		System.out.println(dp.getUrl());
		System.out.println(dp.getUsername());
		System.out.println(dp.getPassword());
	}
}

结果:
在这里插入图片描述

1.2方式二:使用@ConfigurationProperties注解

@Component
@ConfigurationProperties("database")
public class DataBaseProperties {
	private String driverName = null;
	private String url = null;
	private String username = null;
	private String password = null;
	
	public void setDriverName(String driverName) {this.driverName = driverName;}
	public void setUrl(String url) {this.url = url;}
	public void setUsername(String username) {this.username = username;}
	public void setPassword(String password) {this.password = password;}
	public String getDriverName() {return driverName;}
	public String getUrl() {return url;}
	public String getUsername() {return username;}
	public String getPassword() {return password;}
}

相当于不用@value注解,然后在类上加@ConfigurationProperties(“database”)注解,@ConfigurationProperties 中配置的字符串 database,将与 POJO 的属性名称组成属性的全限定名去配置文件里查找,所以属性名要与配置文件中一致。

测试代码不变,结果与1.1一样。

1.3方式三:单独的配置文件,使用@PropertySource

其实不算一种读取方式,只是引入外部文件,便于结合前两种方式一起使用
有些配置需要在单独的配置文件(都放在一个配置文件中会有些杂乱,不好管理),不在application.properties中,这时需要用到注解@PropertySource,作用是引入外部属性配置。

@Component
@PropertySource(value={"classpath:jdbc.properties"})
public class DataBaseProperties {
	@Value("${database.driverName}")
	private String driverName = null;
	@Value("${database.url}")
	private String url = null;
	@Value("${database.username}")
	private String username = null;
	@Value("${database.password}")
	private String password = null;
	
	public void setDriverName(String driverName) {this.driverName = driverName;}
	public void setUrl(String url) {this.url = url;}
	public void setUsername(String username) {this.username = username;}
	public void setPassword(String password) {this.password = password;}
	public String getDriverName() {return driverName;}
	public String getUrl() {return url;}
	public String getUsername() {return username;}
	public String getPassword() {return password;}
}

测试与测试结果与之前一样。

测试时发现@PropertySource加在测试类上获取不到配置文件中的值,加在启动类上可以。可以一次加载多个配置文件。
加载多配制文件示例:
@PropertySource(value={"classpath:jdbc.properties","classpath:system.properties"})
总结:@PropertySource可放在配置类或者启动类上,配合@Value或@ConfigurationProperties注解一起使用,测试中是结合@Value注解,可自行测试其他情况。

方式四:使用Environment

Environment可以获取项目属性配置文件中的值,如果配置没有在application.yml配置文件中要结合@PropertySource(“classpath:xxx.properties”)注解一起使用,作用为引入配置文件,放在启动类上即可。使用起来简单粗暴

配置文件
application.yml

#测试值
app:
  val: application.yml中的属性

jdbc.properties

database.driverName=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/test
database.username=root
database.password=l23456

jdbc.val=jdbc.properties中的值

测试

	@Autowired
	Environment evn;
	@Test
	void test2() {
		System.out.println(evn.getProperty("app.val"));
		System.out.println(evn.getProperty("jdbc.val"));
	}

测试结果
在这里插入图片描述
也可以自定义配置类实现EnvironmentAware接口,达到类似效果,需要重写setEnvironment方法。工程启动时,获取到系统环境变量和application配置文件中的变量。

以上为4种常用读取配置文件方法,建议复制代码测试一下就知道怎么使用了。

发布了25 篇原创文章 · 获赞 2 · 访问量 848

猜你喜欢

转载自blog.csdn.net/Laputa219/article/details/103475883