SpringBoot读取配置文件总结

前言

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} 
# 32位随机字符串
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 int number;
	
	@Value("${com.blog.bignumber}")
	private String bignumber;
	
	@Value("${com.blog.test1}")
	private String test1;
	
	@Value("${com.blog.test2}")
	private int 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”
 

二、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”

写法二:
resources/author.properties内容如下:时尚=美搭

	public static String getNameFrom(String codeName) throws IOException {
		InputStream in = Code2Name.class.getClassLoader().getResourceAsStream("nameFrom.properties");
		Properties props = new Properties();
		props.load(in);
		System.out.println("nameFrom:" + codeName + "->" + props.getProperty(codeName));
		in.close();
		return props.getProperty(codeName);
	}

提炼成工具类:

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesUtil {

    private static Properties properties;

    public static Properties getProperties(String configName) {
        properties = new Properties();
        InputStream in = null;
        try {
            in = PropertiesUtil.class.getClassLoader().getResourceAsStream(configName);
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != in) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return properties;
    }

    public static String getProperty(String configName, String key) {
        if (properties == null) {
            properties = getProperties(configName);
        }
        return properties.getProperty(key);
    }
}

四.分环境配置

在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优先级大

六.读取resources目录下的文件

注:这里读取的不是配置文件,而是其他类型的文件,如txt、csv等格式的文件

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class DomainConvertUtil {

	public static List<String> getChineseName(String word) {
		List<String> list = new ArrayList<String>();
		try {
			InputStream io = Thread.currentThread().getContextClassLoader().getResourceAsStream("mediaLevel.csv");
			InputStreamReader isr = new InputStreamReader(io, "utf-8");
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			while ((line = br.readLine()) != null) {
				if (line.contains(word)) {
					String[] split = line.split(",");
					list.add(split[0]);
					list.add(split[1]);
					list.add(split[2]);
					list.add(split[3]);
					return list;
				}
			}
			br.close();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}

注意:读取resources目录下文件的名称不要用中文,我在idea中有次就用中文命名了文件,用主方法main调用上面的的getChineseName是能读到文件内容的,可是当用mvn clean compile package命令打包后放到Linux中运行死活读取不到该文件,总是报java.lang.NullPointerException空指针的错误,后来把文件名改为英文就不报错了。

参考:
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

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小强签名设计

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值