This is the 27th day of my participation in the August Genwen Challenge.More challenges in August
One practical tip every day, Properties configuration file
The Yaml configuration file is now recommended, but the properties configuration file is also used frequently
There is a directly related class called Properties in the JDK, so let’s look at its use
1. Configuration file
The format of the properties file is relatively simple
key = value
: The value on the left of the equals sign is the configuration key, and the value on the right is the configuration value (the value value will remove the space before and after the value).#
To:#
To distinguish between comments
A basic configuration file is as follows
# testKey = value user.name = a gray blog user.age = 18 user.skill = Java, Python,js,shellCopy the code
2. Load the configuration file
For the Properties configuration file, we can use the Properties class very simply to load the configuration
public class PropertiesUtil {
/** * Reads configuration ** from a file@param propertyFile
* @return
* @throws IOException
*/
public static Properties loadProperties(String propertyFile) throws IOException {
Properties config = new Properties();
config.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(propertyFile));
returnconfig; }}Copy the code
Using Properties#config directly, you can read the contents of the configuration file and assign values to Java objects
Focus on:
Focusing on the inheritance of the Properties class, its parent is Hashtable, which is essentially a Map object
public
class Properties extends Hashtable<Object.Object> {}Copy the code
3. Properties object usage
Since Properties is derived from Hashtable, which is a thread-safe Map container, Properties is thread-safe, and also suffers from poor performance when multiple threads concurrently fetch configurations. Why?
First, take a look at configuration fetch
// Get configuration properties
public String getProperty(String key) {
Object oval = super.get(key);
String sval = (oval instanceof String) ? (String)oval : null;
return ((sval == null) && (defaults ! =null))? defaults.getProperty(key) : sval; }// Gets the configuration attribute, or returns the default value if none exists
public String getProperty(String key, String defaultValue) {
String val = getProperty(key);
return (val == null)? defaultValue : val; }Copy the code
The above two methods are frequently used, and it is easy to know the gesture from the signature; The next thing we need to look at is why concurrency is inefficient
The key is in the first method super.get(), which corresponds to the source code
public synchronized V get(Object key) {
// ...
}
Copy the code
Synchronized is on the method signature, so it’s easy to see why performance in concurrent environments is not particularly good
In addition to getting the configuration, another common use is to update the configuration
public synchronized Object setProperty(String key, String value) {
return put(key, value);
}
Copy the code
4. Summary
This article mainly introduces the knowledge of the properties configuration file processing, using the same Java class to operate; It’s important to note that the Properties class is a subclass of Hashtable, which is also a container
In SpringBoot configuration auto-loading, the configuration content can be automatically loaded into the configuration class. Simply speaking, it supports configuration to Java bean mapping. Now let’s implement this, how can we do?
Series of blog posts:
- Practical tip 1: String placeholder replacement -JDK version
- Practice Tip 2: Array and list are rotated
- Practice Tip 3: String and container transfer
- Practical tip 4: Elegant string concatenation implementation
- Practice tip 5: Hump and underline turn each other
- Practice Tip 6: Special use of enumeration
- Practice Tip 7: Sort carefully
- Practice tip 8: Specify the initial size of the container
- List. SubList is used incorrectly StackOverflowError
- Practice Tip # 10: Immutable containers
- Practice Tip 11: Array copy
- Practice Tip 12: Number formatting
II. The other
1. A gray Blog:liuyueyi.github.io/hexblog
A gray personal blog, recording all the study and work in the blog, welcome everyone to go to stroll
2. Statement
As far as the letter is not as good, the above content is purely one’s opinion, due to the limited personal ability, it is inevitable that there are omissions and mistakes, if you find bugs or have better suggestions, welcome criticism and correction, don’t hesitate to appreciate
- Micro Blog address: Small Gray Blog
- QQ: a gray /3302797840
- Wechat official account: One Grey Blog