Spring Boot 01 加载配置文件和获取key/value值

本文介绍了Spring Boot中配置文件的位置、默认名称,以及如何加载自定义配置文件,包括@PropertiesSource、@ConfigurationProperties等注解的使用方法。同时,详细讲解了获取配置文件中的key/value值的多种方式,如@Value、@ConfigurationProperties、Environment对象的getProperty方法等,并展示了配置文件注入List或数组的实践案例。

项目目录:

JDBCConfig.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:config/jdbc.properties")
public class JDBCConfig {
	@Value("${username}")
	private String username;
	@Value("${url}")
	private String url;
	@Value("${password}")
	private String password;
	@Value("${driverclassname}")
	private String driverClassName;
	
	@Override
	public String toString() {
		return "JDBCConfig [username=" + username + ", url=" + url
				+ ", password=" + password + ", driverClassName="
				+ driverClassName + "]";
	}
}


jdbc.porperties

username=root
password=123465
url=jdbc:mysql://127.0.0.1:3306/test_mysql
driverclassname=com.mysql.jdbs.Driver
DSConfig.java

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix="ds")
@PropertySource("classpath:ds.properties")
public class DsConfig {
	@Override
	public String toString() {
		return "DsConfig [username=" + username + ", password=" + password
				+ ", url=" + url + ", driverClassName=" + driverClassName + "]";
	}
	private String username;
	private String password;
	private String url;
	private String driverClassName;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getDriverClassName() {
		return driverClassName;
	}
	public void setDriverClassName(String driverClassName) {
		this.driverClassName = driverClassName;
	}
}

ds.properties

ds.username=root
ds.password=123456
ds.url=jdbc:mysql://127.0.0.1:3306/test_mysql
ds.driverClassName=com.mysql.jdbc.Driver


User.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
@PropertySource("file:E:/SpringBoot/application1.properties")
public class User {
	@Value("${local.port}")
	private String port;
	@Autowired
	private Environment env;
	@Override
	public String toString() {
		return "User [port=" + port + ", env=" + env.getProperty("local.ip") + "]";
	}
	
}
文件路径:



application1.properties

local.ip=192.168.1.31
local.port=8080



1、Spring Boot 默认配置文件的位置和名字

(1)、默认放置配置文件的地方是:classpath: 和classpath:config

(2)、默认名字是:application.properties

2、加载配置文件的方式

(1)、使用@PropertiesSource或@PrepertiesSources注解JDBCConfig.java。(看JDBCConfig.java)

 (2)、使用@ConfigurationProperties注解。(注:1.4版本的可以使用该注解的locations属性加载配置文件,1.5版本不支持该属性)

 (3 )、使用main方法的args参数:--spring.config.location=classpath:/propertiesName.properties

或--spring.config.name=propertiesName(注:该参数可以省略后缀名,位置在classpath:或classpath:/config目录下

 (4)、可以使用file:"路径"获取磁盘的配置文件(看User.java)

3、获取配置文件的key/value值

1、@Value("${key}")

2、@ConfigurationProperties注解(看DsConfig.java,这个注解是用set方法注入的)

3、获取Environment对象,使用getProperty方法

4、获取Enviroment对象

1.

ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
context.getEnviroment()
2、跟普通的bean一样获取。

5、配置文件注入List或数组

使用@ConfigurationPerties注解

ListConfig.java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:list.properties")
@ConfigurationProperties(prefix="esc")
public class ListConfig {
	
	private List<String> host=  new ArrayList<String>();
	private String  []ip=new String[100];
 
	public String[] getIp() {
		return ip;
	}

	@Override
	public String toString() {
		return "ListConfig [host=" + host + ", ip=" + Arrays.toString(ip) + "]";
	}

	public void setIp(String[] ip) {
		this.ip = ip;
	}

	public List<String> getHost() {
		return host;
	}

	public void setHost(List<String> host) {
		this.host = host;
	}

	

	
}
list.properties
esc.host[0]=1
esc.host[2]=2
esc.host[1]=5
esc.host[3]=3
esc.host[4]=4

esc.ip[0]=a
esc.ip[1]=b
esc.ip[2]=c
esc.ip[3]=d


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值