这篇文章将为大家详细讲解有关如何在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");
}
关于如何在SpringBoot中读取resource目录下的properties文件就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。