Beauty of simplicity jodd — props attribute usage

Prop is a super property; It contains many things missing from JDK: UTF-8 support, macros, partitions, profiles, full configuration, etc.

Properties are stored in one or more * Props file, and it is open and supports many types of resources. More importantly, it is compatible with Java properties.

The goal of props is not to provide a maximum configuration solution, but to provide a better choice than Java properties. Therefore, if properties are used in the application, consider using props instead.

Basic rules

The following is a set of basic rules for introducing props file format. Some of these rules are as follows:

Utf8 encoding

By default, props files are utf8 encoded, or any other encoding format can be used. No matter which encoding format is used, props still uses ISO 8859-1 when loading Java properites

Remove white space characters

The white space characters at the beginning and end of the partition name and attribute name are removed, and the white space characters at the beginning and end of the attribute value are removed.

assignment

You can use "=" or ":" for assignment.

Value connection

Use "+ =" to connect the values of attributes with the same name (separated by commas).

annotation

From ';' Or "#" from the beginning to the end of the line.

Escape character

Use "\" as the escape character to escape the character (for example, "\ #" represents the character "#", "\ \" represents "\").

Multi line values

If "\" is used as the connector of the line, it indicates that the next line is still the value of the attribute.

Special characters

\\Uxxxx will be compiled into characters, and \ t \ R \ f will also be compiled into characters.

Multiline values

Use three quotation marks as a convenient form to define multiline values.

Note: the syntax of three quotation marks is a pair of consecutive single quotation marks or double quotation marks (usually used in pairs).

Three quotation marks free programmers from the quagmire of quotation marks and special strings, and keep a small string in the so-called WYSIWYG (what you see is what you get) format from beginning to end.

Basic Usage

Props is very simple to use. Generally speaking, props manages attributes.

Props can load properties in many forms: file, input stream, string, and properties. Then call the property value through props' etvalue method, which usually returns the value of a string.

partition

Partitions are very similar to those of INI files. In props, a partition represents only a set of keys with the same prefix until the end of the partition or the end of the file.

The partition name begins with [and ends with]. The attributes that belong to the partition follow the header of the partition. The partition name is added as a prefix to the partition properties. The partition ends with an empty [] or starts with a new partition or ends at the end of the file.

Examples are as follows:

The following is equivalent to the above example:

Therefore, partitioning can shorten the file, making it more readable.

Usually, an application has different environments, so different groups of attributes are required. For example, the testing environment and publishing environment of web applications. One way to organize these attributes is to define different profiles in which the same key has different values. Props supports profiles of attributes. Profile is defined in the key name: the profile name is wrapped with < >. A can have one or more profile definitions. Similarly, the profile can be defined anywhere in the key name, even in the middle of the word; However, it's best to put it at the end of the key name. Properties without a profile are based on properties. If retrieving the properties of a specific profile fails, the basic properties will be checked. Profile can look at a "different view" or "snapshot" of the same attribute group. Example: dB port=3086db. url = localhost db. username = root db. url =192.168. 1.101 db. Username = app2499 in the above example, three keys are defined. The two keys have two different profiles (development / deploy) and have no basic values. As mentioned above, the partition is a prefix definition of a key, and the profile can be defined anywhere in the key name, so the partition name can also contain the profile definition. The above example can be modified as follows: dB port=3086 [ db ]< span style="color: #000000;"> url= localhost username= root [ db ]< span style="color: #000000;"> url=192.168. 1.101 Username = app2499 when retrieving the above values, you can specify an active profile: String url = props getValue("db.url","develop") String user = props. GetValue ("dB. Username", "develop") Note: a profile can be defined multiple times at the same time. The order of profiles is very important! When a key is defined to multiple active profiles, the value of the first (the value of the first matching profile) is returned. You can also retrieve only the basic properties (ignoring the profile) -- using the getbasevalue () method. The base property does not belong to any profile. The default active profile. Generally, only one set of profiles is active in the life cycle of an application. There is no need to pass this active profile every time getvalues () is called. Props supports the external definition of this so-called active profile, which is defined in the props file that loads properties. When using getValue (string) to retrieve properties, the active profile is the default profile. The active profile is defined by the special attribute key of @ profiles. For example: key1 = = @ profiles = one, the following java code will return the value "Hi!", Because the active profile is "one" The active profile can also be set through Java code. The method is: setactiveprofiles() Internal profiles have such a scenario: two or more profiles share most of the configuration, and only a few properties are different. To avoid repeatedly defining all attributes in each profile, you can use the internal profile to define which different attributes. Props first retrieves the key of the internal profile, and then retrieves the basic attributes. Examples are as follows: key1 = = = key1 < one two>=Hola! The above example defines two profiles. The first one is named "one" and contains 100 key value pairs. The second profile is a profile named one Internal profile of two. It contains a property (key1) -- but all properties of the upper profile are available! When using java code to call the above property: props What happens when getValue ("key1", "one. Two")? Props will be internally named one If the property is not found, props checks the profile of the previous layer: one. If the profile cannot be retrieved, and there is no higher-level profile, props retrieves the basic properties. Internal profiles are available in many layers. The biggest advantage of macro props is that it supports macros. Macros are references to key values that can be used by other keys. Macros are wrapped in {}. An example is as follows: key1 = = nice. The value of key1 is "something nice". Macros can reference any property key that exists, regardless of where they are defined. Nested macros are also supported. Examples are as follows: key1 = = 2 = foo. The value of key1 is "* * foo * *" Macros and profile macros are usually parsed using the currently active or provided profile. If the current profile changes, the value of the macro usually changes. This behavior is controlled by the flag bit useactiveprofileswenresolvingmacros. Examples are as follows: root = = = ${root} / data What is the value of path when the profile is set to active? Because foo is active, the value of root becomes / foo, so data The value of path is / foo / data If we close all profiles and use only basic properties, data The value of path is / APP / data You can also display the profile of the setting macro: root = = ${root} / data. In the above example, the macro root will always use foo profile regardless of the currently selected profile, so data The value of path will always be "/ foo / data" Multiline values multiline values can be defined with three quotation marks. The values in the middle are regarded as email body=,welcome! ''' Note: the blank of multi line values will not be cut short! Therefore, the value in the above example will contain 5 lines. Traversal and key order the keys in props are sequential! Therefore, you can traverse all keys according to the order of keys in the properties file. Do not use the following code: foo 1 =. 2 = you can traverse props as follows: props props props = it = p.iterator() the traversal order is consistent with that defined by props. Further, you can filter retrieval or / and partition traversal by adding a profile. You can write this: iterator it = "one. Two" "prof1", "prof2" the above code only traverses the properties of a given partition and a given profile. Due to the introduction of profile, a key can be defined in multiple places in the property file. For example, you can define a specific value for two different profiles. In this case, the correct order of keys will not be determined: is it the first retrieved key or the value obtained from its location? You can control by using skipduplicatesbyvalue and skipduplicatesbyposition(). The copy operator assumes that you have a certain number of attributes, which is the same as the number of different types by default. An example is as follows: com jodd. Action1 === props enables you to use the copy operator (< =) to minimize duplicate attributes. The above attributes can be written as follows: = = < span style = "color: #800000; font weight: bold;" > [] org. jodd <= actions [ com]< span style="color: #000000;"> jodd <= actions [ net. jodd]< span style="color: #000000;"> <= Actions the above example shows three different ways to use replication operations, no partition, partial partition and full partition. You can use either of these three methods. You can choose any one. Note: the copied value is set as macro, so all the copied properties above are also equivalent to: org jodd. Action1 = = configuration several configuration settings can be used to fine tune props: new line escape value when the end of line (EOL) needs to be escaped, specify a new string. The default value is an empty string, so multi line values are connected with single line values. If the escape value of a new row is set to, for example, "\ n", the values of multiple rows will be saved in the form of multiple rows. Cut the left value. If you need to cut from the left, set this value. Cut right value set this value if you need to cut from the right. The new line ignores the previous blank value. If the value is divided into multiple lines (to escape EOL), you need to ignore the main blank value, define this value. The default value is true, so the following multi line attribute: key1 = will be read as line1line2line3 (connected) to skip the empty attribute and skip the flag bit of the empty attribute. Connect duplicate attributes. Once set, duplicate attribute keys will not overwrite the old keys, but will be connected and separated by commas. Multiline values are enabled by default, making it easier to write multiline values in three quotation marks (like Python). All characters within three quotation marks are recognized as a value, so new lines do not need to be escaped. The source code is as follows: String escape newlinevalue = < / span > < span style = "color: #008000;"& gt;/** valueTrimLeft = valueTrimRight = ignorePrefixWhitespacesOnNewLine = multilineValues = skipEmptyProps = ; References: http://jodd.org/doc/props.html

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>