个人理解
在企业开发中,我们经常需要自定义一些全局变量/不可修改变量或者参数来解决大量的变量重复问题,当需要这个全局变量时,只需要从配置文件中读取即可,根据开发中常见的情况,可以分为以下两种情况,分别是:
- 配置文件为SpringBoot默认的application.properties文件中的自定义参数
- 加载自定义properties文件中的自定义参数,比如xxx.properties的自定义参数
加载SpringBoot默认的application.properties
准备工作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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=旺财
|
1
2
3
4
5
6
7
8
9
10
11
12
|
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开头的所有参数,那么推荐使用第一种方式,如果获取单个参数,那么建议使用第二种获取参数方式。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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;
}
|
测试使用代码:
1
2
3
4
5
6
7
8
9
10
11
12
|
@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
1
2
3
4
5
6
7
8
9
10
11
12
|
@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获取
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@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加载
1
2
3
4
5
6
7
8
9
10
|
//读取资源配置文件
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文件的常见方式的文章就介绍到这了,更多相关SpringBoot读取properties文件内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_39182939/article/details/113703581