Setting

Introduction

In addition to supporting the Properties file format, Setting provides some unique features, including:

  • Support for various encoding methods
  • Support for variables
  • Support for grouping

First, let’s talk about encoding support. In Properties, only ISO8859-1 is supported, which makes it impossible to use Chinese characters in comments and values in the Properties file. (Using the Japanese plugin in Eclipse can read and write, but it’s difficult to read on the server.) Therefore, custom encoding is introduced in Setting, which can well support configuration files of various encodings.

Next is variable support. In Setting, variables in the form of ${key} are supported, which can use the value corresponding to the previously defined key as part of the current value. This feature can reduce a lot of redundant configuration files.

Finally, there is group support. The concept of grouping was first introduced to me in the /etc/rsyncd.conf configuration file of Linux’s rsync, and I found it very practical. You can search for it on Baidu. Of course, the concept of grouping also exists in Windows’ ini files. Setting introduces this concept to greatly increase the readability of configuration files.

Example of configuration file format: example.setting

# Square brackets indicate a group, and all properties under it belong to this group. In this case, the group name is demo, which can also be left empty.
[demo]
# Custom data source setting file, which takes effect for the current group and is used to configure separate database connection pool parameters for the current group. If not specified, the global configuration is used.
ds.setting.path = config/other.setting
# Database driver name. If not specified, it will be automatically determined based on the url.
driver = com.mysql.jdbc.Driver
# JDBC url, required.
url = jdbc:mysql://fedora.vmware:3306/extractor
# User name, required.
user = root${demo.driver}
# Password, required. If the password is empty, please fill in pass = 
pass = 123456

The configuration file can be placed anywhere. The SpecificSetting class provides various reading methods in the constructor method to find it. Now let’s talk about the specific format of the configuration file. The Setting configuration file is similar to the Properties file, with the following rules:

  1. Comments are indicated by a # at the beginning of the line, and only single-line comments are supported. Blank lines and key-value pairs that cannot be recognized will also be ignored and can be used as comments. However, it is recommended to explicitly specify comments. At the same time, comments are not allowed after the value and will be treated as part of the value.
  2. Key-value pairs are represented as key = value. The key and value will be trimmed of spaces when read, so there is no need to worry about spaces.
  3. The group is the content enclosed in square brackets (for example, [demo] in the configuration file), and the lines below it are the content of this group. If there is no group, it is equivalent to an empty character group, that is, []. If a key is name and the group is group, the key after adding the group is equivalent to group.name.
  4. Variables are supported, with the default variable name being ${variable name}. Variables can only recognize variables in the read-in line. For example, a variable in the sixth line cannot be read in the third line. For example, ${driver} in the configuration file will be replaced with com.mysql.jdbc.Driver. To improve performance, the Setting constructor method specifies whether to enable variable replacement by default, which is disabled by default.

Code

Please refer to cn.hutool.setting.test.SettingTest for specific code.

  1. Initialization of Setting
// Read XXX.setting under the classpath without using variables
Setting setting = new Setting("XXX.setting");

// Read XXX.setting under the config directory in the classpath without using variables
setting = new Setting("config/XXX.setting");

// Read the absolute path file /home/looly/XXX.setting (create it if it doesn't exist, please refer to FileUtil for touc)
// The second parameter is the custom encoding, please keep it consistent with the encoding of the Setting file
// The third parameter indicates whether to use variables. If true, each key in the configuration file can be referenced by subsequent entries in the form of ${key}
setting = new Setting(FileUtil.touc("/home/looly/XXX.setting"), CharsetUtil.CHARSET_UTF_8, true);

// Read XXX.setting in the same package as SettingDemo.class
setting = new Setting("XXX.setting", SettingDemo.class, CharsetUtil.CHARSET_UTF_8, true);
  1. Reading Configuration Parameters with Setting
// Get the value of the key "name"
setting.getStr("name");

// Get the value of the key "name" under the group "group1"
setting.getByGroup("name", "group1");

// Return the default value when the obtained value is empty (null or blank characters, including multiple spaces)
setting.getStr("name", "default value");

// Complete method to get the value with key, group, and default value
setting.getStr("name", "group1", "default value");

// To get values of other types, you can call the corresponding getXXX method with similar parameters

// Sometimes it's necessary to inform the user when the value corresponding to the key does not exist (when there is no such setting), hence this method logs a debug message
setting.getWithLog("name");
setting.getByGroupWithLog("name", "group1");

// Get all configuration key-value pairs under a group and form a new Setting
setting.getSetting("group1")
  1. Reloading and Saving Configurations
// Re-read the configuration file
setting.reload();

// Automatically load when the configuration file changes
setting.autoLoad(true);

// When new key-value pairs are added through code, calling store will save them to the file, but it will overwrite the original file and lose comments
setting.set("name1", "value");
setting.store("/home/looly/XXX.setting");

// Get all group names
setting.getGroups();

// Map key-value pairs to an object by calling the corresponding setXX method of the object
UserVO userVo = new UserVo();
setting.toBean(userVo);

// Set the regular expression for variable names. 
// Variable replacement in Setting is achieved through regular expression search and replacement. If there is a conflict with variable names in Setting, you can change the way variables are defined. 
// The entire regular expression matches the variable name, and group 1 matches the name of the key. 
setting.setVarRegex("\\$\\{(.*?)\\}");