SpringBoot读取配置文件总结

版权声明:本文为博主原创文章,请尊重劳动成果,觉得不错就在文章下方顶一下呗,转载请标明原地址。 https://blog.csdn.net/m0_37739193/article/details/83352591

前言

Spring-Boot的核心配置文件是application.properties,会默认读取该配置文件,当然也可以通过注解自定义配置文件的信息。开发中,经常会有一些常量,变动较少,但是我们不能在java代码中写死,这样每次修改都得去java代码中修改,引用地方较多时我们需要修改很多java文件;所以我们可以集中写在某个配置文件中,这样只用修改配置文件就好。

pom.xml:

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.3.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

注意:你在pom.xml中添加如下配置,不加的话当你使用@ConfigurationProperties注解的时候会报Warning(黄色小感叹号),但是不影响功能实现(强迫症的我看的不爽)。

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

原因:spring默认使用yml中的配置,但有时候要用传统的xml或properties配置,就需要使用spring-boot-configuration-processor了
 

一.读取核心配置文件信息application.properties的内容

核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单。
核心配置文件application.properties内容如下,这里有普通的定义属性,也有内置的函数,如随机数,随机字符串等:

# 自定义属性
com.tmall.id=123.45.67.89
# 参数间引用
com.tmall.id.description=This is id:${com.tmall.id} 
# 随机字符串
com.blog.value=${random.value}
# 随机int
com.blog.number=${random.int}
# 随机long
com.blog.bignumber=${random.long}
# 10以内的随机数
com.blog.test1=${random.int(10)}
# 10-20的随机数
com.blog.test2=${random.int[10,20]}

class.schoolName=china school
class.students[0].name=tom
class.students[1].name=jack

test.msg=Hello World SpringBoot

xiao=qiang

方式一:使用@Value方式(常用)

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class WebController {
	@Value("${com.tmall.id}")
	private String id;
	
	@Value("${com.tmall.id.description}")
	private String description;
	
	@Value("${com.blog.value}")
	private String value;
	
	@Value("${com.blog.number}")
	private String number;
	
	@Value("${com.blog.bignumber}")
	private String bignumber;
	
	@Value("${com.blog.test1}")
	private String test1;
	
	@Value("${com.blog.test2}")
	private String test2;
	
	@RequestMapping("/index1") 
	public String index1(){
		return "方式一:"+"id->"+id+";description->"+description+";value->"+value
				+";number->"+number+";bignumber->"+bignumber+";test1->"+test1+";test2->"+test2;
	}
}

注意:在@Value的${}中包含的是核心配置文件中的键名。在Controller类上加@RestController表示将此类中的所有视图都以JSON方式显示,类似于在视图方法上加@ResponseBody。
访问:http://localhost:8080/index1时得到:“方式一:id->123.45.67.89;description->This is id:123.45.67.89;value->856f604db7c4b03da21b29776dc0d3dc;number->1522979010;bignumber->-8020258677837408781;test1->6;test2->13”

方式二:把某种相关的配置,注入到某个配置类中,比如上面的application.properties中看到class.xx的配置项,想注入到ClassConfig配置类中。还有集合的注入方法

public class StudentConfig {
 
	private String name;
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
}
import java.util.ArrayList;
import java.util.List;
 
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
@Component
@ConfigurationProperties(prefix="class")
public class ClassConfig {
 
	private String schoolName;
	
	private List<StudentConfig> students = new ArrayList<StudentConfig>();
 
	public String getSchoolName() {
		return schoolName;
	}
 
	public void setSchoolName(String schoolName) {
		this.schoolName = schoolName;
	}
 
	public void setStudents(List<StudentConfig> students) {
		this.students = students;
	}
 
	public List<StudentConfig> getStudents() {
		return students;
	}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hui.config.ClassConfig;
 
@RestController
public class WebController {

	@Value("${class.schoolName}")
	private String schoolName;
	
	@Autowired
	private ClassConfig classConfig;
	
	@RequestMapping("/index2") 
	public String index1(){
		return "方式二:"+"schoolName="+classConfig.getSchoolName()+"或者->"+schoolName+";name1="+classConfig.getStudents().get(0).getName()
				+";name2="+classConfig.getStudents().get(1).getName();
	}
}

访问:http://localhost:8080/index2时得到:“方式二:schoolName=china school或者->china school;name1=tom;name2=jack”

方式三:使用Environment方式

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class WebController {
	@Autowired
	private Environment env;
	
	@RequestMapping("/index3") 
	public String index3(){
		return "方式三:"+env.getProperty("test.msg");
	}
}

注意:这种方式是依赖注入Evnironment来完成,在创建的成员变量private Environment env上加上@Autowired注解即可完成依赖注入,然后使用env.getProperty(“键名”)即可读取出对应的值。
访问:http://localhost:8080/index3时得到:“方式三:Hello World SpringBoot”
 

扫描二维码关注公众号,回复: 4152156 查看本文章

二、xiao属性注入到配置类中不同的两种写法

写法一:

import org.springframework.stereotype.Component;

@Component
public class Configa {
	
	@Value("${name}")
	private String name;
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
}

写法二:

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

@Component
@ConfigurationProperties //和@ConfigurationProperties(prefix="")等效,不可省略否则会找不到为null
//@PropertySource(value = { "" }) //可省略不写,默认读取application.properties
public class Configa {
	
	private String name;
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
}

三.读取自定义配置文件信息

为了不破坏核心文件的原生态,但又需要有自定义的配置信息存在,一般情况下会选择自定义配置文件来放这些自定义信息,这里在resources目录下创建配置文件author.properties。resources/author.properties内容如下:
author.name=Solin
author.age=22

创建管理配置的实体类:

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

@Component
//@ConfigurationProperties(prefix = "author",locations = "classpath:author.properties")
@ConfigurationProperties(prefix = "author")
@PropertySource("classpath:/author.properties")
//多配置文件引用,若取两个配置文件中有相同属性名的值,则取值为最后一个配置文件中的值
//@PropertySource({"classpath:/my.properties","classpath:/author.properties"})
public class MyWebConfig{
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

注意:
springboot 1.5版本以前@ConfigurationProperties注释中有两个属性:
locations:指定配置文件的所在位置
prefix:指定配置文件中键名称的前缀(我这里配置文件中所有键名都是以author.开头)
springboot 1.5版本以后@ConfigurationProperties没有了location属性,使用@PropertySource来指定配置文件位置

创建测试Controller:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
@Controller  
public class ConfigController {
	@Autowired
	private MyWebConfig conf;
	
	@RequestMapping("/test") 
	public @ResponseBody String test() {
		return "Name:"+conf.getName()+"---"+"Age:"+conf.getAge(); 
	}
}

注意:由于在Conf类上加了注释@Component,所以可以直接在这里使用@Autowired来创建其实例对象。
访问:http://localhost:8080/test时得到:“Name:Solin---Age:22”
 

四.分环境配置

在src/main/resources下添加类似这样的文件application-xxx.properties,然后在application.propertie中添加spring.profiles.active=xxx配置则说明首先读取的是application-xxx.properties而不是application.properties,当使用@Value时则先去找application-xxx.properties,如果application-xxx.properties没有则再找application.properties,如果两个文件都没有则会报错Could not resolve placeholder ‘com.blog.test1’ in value “${com.blog.test1}”
 

五.误区

1.认为分环境配置可以配置多个
我实践后发现只能配一个,如在applicatio.properties中这样配置:
spring.profiles.active=pro
spring.profiles.active=dev
你可能会认为先找application-pro.properties文件,再找application-dev.properties,最后找application.properties。但实际上是applicatio.properties中最下面的生效,即先找application-dev.properties文件,再找application.properties文件,没有则会报错

2.两个配置文件即内容如下:
application.properties name=hehe
hui.properties name=haha

配置类写法:

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

@Component
@ConfigurationProperties
@PropertySource("classpath:/hui.properties")
public class Configa {
	
	private String name;
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
}

Controller写法:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hui.config.Configa;
 
@RestController
public class WebController {
	
	@Autowired
	private Configa configa;
	
	@RequestMapping("/index") 
	public String index1(){
		return configa.getName();
	}
}

问:最后返回的值是什么,是hehe还是haha?
答:最后返回的是hehe,当application.properties和其他配置文件有相同属性名则application.properties优先级大

参考:
https://blog.csdn.net/qq_32786873/article/details/52840745
https://blog.csdn.net/weixin_39800144/article/details/78837603
https://blog.csdn.net/dream_broken/article/details/72385295
https://blog.csdn.net/flygoa/article/details/58075398

猜你喜欢

转载自blog.csdn.net/m0_37739193/article/details/83352591