SpringBoot读取properties文件 详解SpringBoot读取resource目录下properties文件的常见方式

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringBoot读取properties文件 详解SpringBoot读取resource目录下properties文件的常见方式

SZH勿忘初心   2021-02-07 我要评论
想了解详解SpringBoot读取resource目录下properties文件的常见方式的相关内容吗,SZH勿忘初心在本文为您仔细讲解SpringBoot读取properties文件的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:SpringBoot读取properties文件,SpringBoot读取resource目录properties文件,下面大家一起来学习吧。

个人理解

在企业开发中,我们经常需要自定义一些全局变量/不可修改变量或者参数来解决大量的变量重复问题,当需要这个全局变量时,只需要从配置文件中读取即可,根据开发中常见的情况,可以分为以下两种情况,分别是:

  • 配置文件为SpringBoot默认的application.properties文件中的自定义参数
  • 加载自定义properties文件中的自定义参数,比如xxx.properties的自定义参数

加载SpringBoot默认的application.properties

准备工作

server.port=8081

# 自定义参数->都是person.变量名的形式

person.id=1
person.name=szh

# list/set/数组->两种写法
person.hobby=play,read,write
person.family[0]=father
person.family[1]=mother

# map->两种写法
person.map.key1=value1
person.map[key2]=value2

# Entity对象->Pet实体类
person.pet.type=dog
person.pet.name=旺财
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class Pet implements Serializable {
 private String type;
 private String name;
}

方式一 : @ConfigurationProperties

开发中如果获取整个以xxx开头的所有参数,那么推荐使用第一种方式,如果获取单个参数,那么建议使用第二种获取参数方式。

import com.szh.test.entity.Pet;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "person")
@Data
public class PersonConfig {
 private int id;
 private String name;
 private List hobby;
 private String[] family;
 private Map map;
 private Pet pet;

}

测试使用代码:

@Autowired
 private PersonConfig personConfig;

 @RequestMapping("/hello1")
 public void hello1() {
 System.out.println(personConfig.getFamily());
 System.out.println(personConfig.getHobby());
 System.out.println(personConfig.getMap());
 System.out.println(personConfig.getId());
 System.out.println(personConfig.getName());
 System.out.println(personConfig.getPet().getName());
 }

方式二:@Value

@Value("${person.id}")
 private int id;
 @Value("${person.name}")
 private String name;
 @Value("${person.hobby}")
 private List hobby;
 @Value("${person.family}")
 private String[] family;
 @Value("${person.map}")
 private Map map;
 @Value("${person.pet}")
 private Pet pet;

方式三:使用Environment获取

@Autowired
 private Environment env;

 @RequestMapping("/hello1")
 public void hello1() throws UnsupportedEncodingException {

 String id = env.getProperty("person.id");
 // 中文
 String name = new String(env.getProperty("person.name").getBytes("ISO-8859-1"), "UTF-8");
 List hobby = new ArrayList();
 hobby.add(env.getProperty("person.hobby[0]"));
 hobby.add(env.getProperty("person.hobby[1]"));
 String[] family;
 Map<String,String> map = new HashMap<String,String>();
 map.put("key1", env.getProperty("person.map.key1"));
 map.put("key2", env.getProperty("person.map.key2"));

 Pet pet = new Pet(env.getProperty("person.pet.type"),env.getProperty("person.pet.name"));
 }

加载自定义properties文件

准备工作:在resource/目录下新建一个自定义配置文件szh.properties

person.id=1
person.name=szh

# list/set/数组->两种写法
person.hobby=play,read,write
person.family[0]=father
person.family[1]=mother

# map->两种写法
person.map.key1=value1
person.map[key2]=value2

# Entity对象
person.pet.type=dog
person.pet.name=旺财

方式一: @PropertySource+@ConfigurationProperties

@Component
@PropertySource(value = "classpath:szh.properties")
@ConfigurationProperties(prefix = "person")
@Data
public class PersonConfig {
 private int id;
 private String name;
 private List hobby;
 private String[] family;
 private Map map;
 private Pet pet;

}

方式二:@PropertySource+@Value

@Component
@PropertySource(value = "classpath:szh.properties")
@Data
public class PersonConfig {
 @Value("${person.id}")
 private int id;
 @Value("${person.name}")
 private String name;
 @Value("${person.hobby}")
 private List hobby;
 @Value("${person.family}")
 private String[] family;
 @Value("${person.map}")
 private Map map;
 @Value("${person.pet}")
 private Pet pet;

}

方式三:Properties加载

//读取资源配置文件
 InputStream is = Bean.class.getClassLoader().getResourceAsStream("szh.properties");
 prop = new Properties();
 String className = "person.name";//可以作为一个函数的变量
 try {
  prop.load(is);
  String pathName = prop.getProperty(className);
 } catch (Exception e) {
  throw new RuntimeException("xxxx");
 }

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们