Explain in detail the common ways for SpringBoot to read properties files under resource directory
- 2021-08-17 00:01:30
- OfStack
Personal understanding
In enterprise development, we often need to customize 1 global variable/unmodifiable variable or parameter to solve a large number of variable duplication problems. When this global variable is needed, we only need to read it from the configuration file. According to the common situation in development, it can be divided into the following two situations, namely:
The configuration file is a custom parameter in the default application. properties file for SpringBoot Load custom parameters in the custom properties file, such as the custom parameters of xxx. propertiesLoad the default application. properties for SpringBoot
Preparatory work
server.port=8081
# Custom parameters -> It's all person. Form of variable name
person.id=1
person.name=szh
# list/set/ Array -> Two ways of writing
person.hobby=play,read,write
person.family[0]=father
person.family[1]=mother
# map-> Two ways of writing
person.map.key1=value1
person.map[key2]=value2
# Entity Object ->Pet Entity class
person.pet.type=dog
person.pet.name= Prosperous wealth
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;
}
Mode 1: @ ConfigurationProperties
In development, if you get all the parameters beginning with xxx, you recommend using the first method, and if you get a single parameter, you recommend using the second method to get parameters.
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;
}
Test using code:
@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());
}
Mode 2: @ 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;
Mode 3: Use Environment to get
@Autowired
private Environment env;
@RequestMapping("/hello1")
public void hello1() throws UnsupportedEncodingException {
String id = env.getProperty("person.id");
// Chinese
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"));
}
Load a custom properties file
Preparation: Create a new custom configuration file szh. properties in the resource/directory
person.id=1
person.name=szh
# list/set/ Array -> Two ways of writing
person.hobby=play,read,write
person.family[0]=father
person.family[1]=mother
# map-> Two ways of writing
person.map.key1=value1
person.map[key2]=value2
# Entity Object
person.pet.type=dog
person.pet.name= Prosperous wealth
Mode 1: @ 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;
}
Mode 2: @ 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;
}
Mode 3: Properties Load
// Read the resource configuration file
InputStream is = Bean.class.getClassLoader().getResourceAsStream("szh.properties");
prop = new Properties();
String className = "person.name";// Can be used as 1 Variables of functions
try {
prop.load(is);
String pathName = prop.getProperty(className);
} catch (Exception e) {
throw new RuntimeException("xxxx");
}