個人理解
在企業開發中,我們經常需要自定義一些全局變量/不可修改變量或者參數來解決大量的變量重復問題,當需要這個全局變量時,只需要從配置文件中讀取即可,根據開發中常見的情況,可以分為以下兩種情況,分別是:
- 配置文件為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