Properties substitution of hutool -- Setting

Source: Internet
Author: User
Preface

For the JDK's built-in PropertiesPropertiesFile, there are many restrictions for us, the first isISO8859-1Encoding makes it impossible to add Chinese values and comments (the Japanese plug-in can be read and written in eclipse, and it is hard to read them on the server). In addition, variable grouping and other functions are not supported, therefore, the setting class is available.

Origin

Variable usage in the configuration file has been in use for a long time.PropertyPlaceholderConfigurerClass is used inApplicationContext.xmlUsed inPropertiesReplace the variables in the file.
The concept of grouping I first learned about it in the/etc/rsyncd. conf configuration file of Linux rsync. I found that it is especially useful for anyone who can use Baidu.

These two features were discovered later in the props of jodd. The extension class of this configuration file is very powerful and even supports multiple lines and so on. I wanted to use them directly, avoiding repeated wheel creation, but I found that many features were completely unavailable and did not have the convenient features I needed, so I createdSettingThis wheel.

Configuration File Format: example. Setting
# Assets # ----- setting file with utf8 ----- # ----- Database Configuration File ----- # ----------------------------------------------------------- # indicates a group, and all its attributes belong to this group. The group name is demo, you can also leave the [demo] # Custom Data Source setting file for the group. This file takes effect for the current group and is used to configure separate database connection pool parameters for the current group, if not, use global DS configuration. setting. path = config/Other. setting # Database driver name. If this parameter is not specified, the driver = com. mySQL. JDBC. driver # jdbc url, required url = JDBC: mysql: // fedora. VMware: 3306/extractor # user name, which must be user = root $ {driver} # password. required. If the password is empty, enter pass = 123456.

The configuration file can be stored in any location. The specific setting class provides multiple reading methods in the constructor method, which will be described later. Now let's talk about the specific format of the configuration file.
The setting configuration file is similar to the properties file. The rules are as follows:

  1. Annotation#Only single line comments are supported. Empty lines and unrecognized key-value pairs are also ignored and can be used as comments. However, it is recommended to explicitly specify comments.
  2. Key-value pairs are represented by key = value. When the key and value are read, trim will drop spaces, so do not worry about spaces.
  3. Content enclosed in brackets (for example[demo]), The rows below the brackets are all the contents of this group, no group is equivalent to a null character group, that is[]. IfkeyYesname, Group isgroupThe key after the group is added is equivalent to group. Name.
  4. Supports variables. The default variable name is $ {variable name}. The variable can only recognize the variables that are read into the row. For example, the variables in line 6th cannot be read from the third row, for example, $ {driver} in the configuration file will be replaced with COM. mySQL. JDBC. driver. For the sake of performance, when setting is created, the constructor will specify whether to enable variable replacement, which is disabled by default.
Code

For code details, seecom.xiaoleilu.hutool.demo.SettingDemo

Package COM. xiaoleilu. hutool. demo; import Java. io. ioexception; import COM. xiaoleilu. hutool. charsetutil; import COM. xiaoleilu. hutool. fileutil; import COM. xiaoleilu. hutool. setting;/*** setting demo class * @ author looly **/public class settingdemo {public static void main (string [] ARGs) throws ioexception {// ------------------------------------------------- initialize // read XXX under classpath. setting. The setting variable is not used. = New setting ("XXX. setting "); // read XXX in the config directory under classpath. setting, without the variable setting = new setting ("config/xxx. setting "); // read the absolute path file/home/looly/xxx. setting (create if none. For more information about touch, see fileutil) // The second parameter is custom encoding, make sure the encoding is consistent with that of the setting file. // The third parameter indicates whether to use the variable. If it is true, then, each key in the configuration file can be referenced in the value form of $ {key} setting = new setting (fileutil. touch ("/home/looly/xxx. setting "), charsetutil. utf_8, true); // read and settingdemo. the class file is in the same package as the xxx. s Etting setting = new setting ("XXX. setting ", settingdemo. class, charsetutil. utf_8, true); // ----------------------------------------------- use // to obtain the value setting with the key as name. getstring ("name"); // you can call this operation to obtain the setting value whose key is name in a group. getstring ("name", "group1"); // when the obtained value is null (null or blank characters, including multiple spaces), the default value setting is returned. getstringwithdefault ("name", "Default Value"); // setting is a complete method for obtaining keys, groups, and default values. getstringwithdefault ("name", "group1", "Default Value" "); // If you want to obtain other types of values, you can call the corresponding getxxx method. The parameters are similar. // sometimes, when the value corresponding to the key does not exist (if this setting is not enabled) this method is used to print a debug log setting. getwithlog ("name"); setting. getwithlog ("name", "group1"); // you can enable a timer to call this method to regularly update the setting configuration. reload (); // when a new key-value pair is added through code, calling store will save it to the file, but it will overwrite the original file and lose the comment setting. setsetting ("name1", "value"); setting. store ("/home/looly/xxx. setting "); // get all group names setting. getgroups (); // map key-value to object. The principle is to call Like the corresponding setxx method // setting. toobject (); // set the regular expression of the variable name. // The variable replacement of setting is replaced by regular expression search. If the variable name in setting conflicts with other variables, you can change the definition of the variable. // The variable name of the entire regular expression match, group 1 matches the key name setting. setvarregex \\{(. *?) \\}");}}
Simple encapsulation of Properties Props(Available in version 2.0.0)

I can't do anything about the wide use of properties. Sometimes it is not easy to read and write properties files conveniently, So I encapsulated properties, provides convenient constructor methods (consistent with setting) and getxxx methods consistent with setting to extend the properties class,PropsThe class inherits from properties, so it is compatible with the properties class. If you are interested, please refer to com. xiaoleilu. hutool. Props.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.