详解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=旺财
importlombok.AllArgsConstructor;
importlombok.Data;
importlombok.NoArgsConstructor;
importjava.io.Serializable;
@NoArgsConstructor
@AllArgsConstructor
@Data
publicclassPetimplementsSerializable{
privateStringtype;
privateStringname;
}
方式一:@ConfigurationProperties
开发中如果获取整个以xxx开头的所有参数,那么推荐使用第一种方式,如果获取单个参数,那么建议使用第二种获取参数方式。
importcom.szh.test.entity.Pet;
importlombok.Data;
importorg.springframework.boot.context.properties.ConfigurationProperties;
importorg.springframework.stereotype.Component;
importjava.util.List;
importjava.util.Map;
@Component
@ConfigurationProperties(prefix="person")
@Data
publicclassPersonConfig{
privateintid;
privateStringname;
privateListhobby;
privateString[]family;
privateMapmap;
privatePetpet;
}
测试使用代码:
@Autowired
privatePersonConfigpersonConfig;
@RequestMapping("/hello1")
publicvoidhello1(){
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}")
privateintid;
@Value("${person.name}")
privateStringname;
@Value("${person.hobby}")
privateListhobby;
@Value("${person.family}")
privateString[]family;
@Value("${person.map}")
privateMapmap;
@Value("${person.pet}")
privatePetpet;
方式三:使用Environment获取
@Autowired
privateEnvironmentenv;
@RequestMapping("/hello1")
publicvoidhello1()throwsUnsupportedEncodingException{
Stringid=env.getProperty("person.id");
//中文
Stringname=newString(env.getProperty("person.name").getBytes("ISO-8859-1"),"UTF-8");
Listhobby=newArrayList();
hobby.add(env.getProperty("person.hobby[0]"));
hobby.add(env.getProperty("person.hobby[1]"));
String[]family;
Mapmap=newHashMap();
map.put("key1",env.getProperty("person.map.key1"));
map.put("key2",env.getProperty("person.map.key2"));
Petpet=newPet(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
publicclassPersonConfig{
privateintid;
privateStringname;
privateListhobby;
privateString[]family;
privateMapmap;
privatePetpet;
}
方式二:@PropertySource+@Value
@Component
@PropertySource(value="classpath:szh.properties")
@Data
publicclassPersonConfig{
@Value("${person.id}")
privateintid;
@Value("${person.name}")
privateStringname;
@Value("${person.hobby}")
privateListhobby;
@Value("${person.family}")
privateString[]family;
@Value("${person.map}")
privateMapmap;
@Value("${person.pet}")
privatePetpet;
}
方式三:Properties加载
//读取资源配置文件
InputStreamis=Bean.class.getClassLoader().getResourceAsStream("szh.properties");
prop=newProperties();
StringclassName="person.name";//可以作为一个函数的变量
try{
prop.load(is);
StringpathName=prop.getProperty(className);
}catch(Exceptione){
thrownewRuntimeException("xxxx");
}
到此这篇关于SpringBoot读取resource目录下properties文件的常见方式的文章就介绍到这了,更多相关SpringBoot读取properties文件内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!