The content of this article is about how spring reads the properties file? (Attached is the code), which has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Problem:
You need to read the name of the required property on the page through properties. In order to facilitate modification in the future.
Solution:
You can use spring PropertiesFactoryBean reads the properties properties, so you don't need to write a program to read the information through the Properties class of jdk.
classpath:recommondHouse.properties
Note: You need to set fileEncoding, otherwise garbled characters will appear. You also need to set the properties encoding in eclipse. Otherwise, the page will display a bunch of characters and letters, and Chinese characters cannot be displayed. The settings in eclipse are as follows:
As shown in the picture, modify the 3 encoding to utf-8 and click update.
Then inject data through the get and set methods through the @Value annotation.
package com.fyinqing.util; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("test") public class PropertiesUtil { @Value("#{prop.name1}") private String name1; @Value("#{prop.name2}") private String name2; @Value("#{prop.name3}") private String name3; @Value("#{prop.name4}") private String name4; public String getName2() { return name2; } public void setName2(String name2) { this.name2 = name2; } public String getName3() { return name3; } public void setName3(String name3) { this.name3 = name3; } public String getName4() { return name4; } public void setName4(String name4) { this.name4 = name4; } public String getName1() { return name1; } public void setName1(String name1) { this.name1 = name1; } public ListgetNameList(){ List list = new ArrayList (); list.add(name1); list.add(name2); list.add(name3); list.add(name4); return list; } }
The test is as follows: (only the key code is written)
@Autowired PropertiesUtil propUtil; @Test public void test4() { System.out.println(propUtil.getNameList()); }
The above is the detailed content of How does spring read the properties file? (with code). For more information, please follow other related articles on the PHP Chinese website!