Spring Profile <context:property-placeholder> Tag usage ramble

Source: Internet
Author: User

<context:property-placeholder>The label provides an elegant way to configure an externalized parameter, but only one copy of the tag can exist in the spring configuration file!!!

It is well known that the spring container is a discovery mechanism using reflection scanning, which instantiates an instance through the namespace of a tag, and when spring detects that one org.springframework.beans.factory.config.PropertyPlaceholderCVonfigurer of the beans in the container stops the remaining PropertyPlaceholderConfigurer scan, there can only be one instance!

< Context:property-placeholder             Location =""           file-encoding=""          ignore-resource-not-found= ""           ignore-unresolvable= ""          properties-ref= ""          local-override  = ""          system-properties-mode=""          order= ""  />

Location : represents a property file position, separated by multiple, such as commas/semicolons;

file-encoding: file encoding;
Ignore-resource-not-found: If the property file is not found, ignore, default false, that is not ignored, will throw an exception
ignore-unresolvable: whether to ignore unresolved properties, if not ignored, throws an exception is not found
properties-ref: local Java.util.Properties configuration
Local-override: Whether the local overwrite mode, that is, if true, then the Properties-ref property overrides the location-loaded property
System-properties-mode: system attribute mode, Environment (default), Never,override
Environment: The Propertysourcesplaceholderconfigurer provided with Spring 3.1 will be used in other cases using spring Propertyplaceholderconfigurer before 3.1
OVERRIDE: Propertyplaceholderconfigurer is used because the version prior to Spring 3.1 is not enviroment, so override is prior to the Spring 3.1 version of Environment
never: only find properties-ref, location;
Order : <context:property-placeholder/> The lookup sequence when multiple configurations are configured

Use Note:
The order of loading files in 1.location
If there are multiple files in the location:
classpath:db.properties,classpath:default.properties,classpath:default3.properties,classpath:default2.properties
will be loaded sequentially, it is worth noting that if the latter file has the same property name as in the previous file, the final value is the post-loaded value
For example:
The Default.properties file has a property name, UserID, whose corresponding value is-1
The Default2.properties file also has a property name UserID with a value of-2
The Default3.properties file has a property name, UserID, whose value is-3

Default.properties file is loaded first, at this time the value of UserID is-1, when the default3.properties file load will update the original value, at this time the value of UserID is-3, similarly, the last load default2.properties file, so Useri D Final value is-2
So you need to avoid duplicate attribute names in different property files.

2.ignore-resource-not-found and ignore-unresolvable Two properties are similar in function, recommended for pairing use
If the file in the location points to a nonexistent file, it is also very likely that there is a property that cannot be parsed (although there are duplicate names in other properties files, this should be avoided)
So when Ignore-resource-not-found is set to True, Ignore-unresolvable must also be set to true, when Ignore-unresolvable is set to True, Ignore-resource-not-found value TRUE or FALSE, does not affect the throw of the exception
If set to Ture, after the property value cannot be resolved successfully, the value will be assigned to the ${property name}
It is not recommended to set the value of Ignore-resource-not-found and ignore-unresolvable to ture, which defaults to false, which effectively avoids program run exceptions

3.properties-ref Property
Introduce other ways to introduce property files

<BeanID= "Refproperties"class= "Org.springframework.beans.factory.config.PropertiesFactoryBean">    < Propertyname= "Locations">        <List>            <value>Classpath:default2.properties</value>        </List>    </ Property></Bean>

This property requires Local-override with the property value in the Properties-ref property file to override the Location property file property value only if the Local-override property value is true (property with the same name)

4.local-override Property
When the Local-override property value is True, the property value in the Properties-ref property file overrides the Location property file property value (property with the same name)

5.sytem-properties-mode Property
Different Sytem-properties-mode properties define a different lookup order
Environment environment: including JDK environment, system environment variables, sevlet environment, spring environment, etc., is spring after 3.1, an abstract representation of the environment configuration

When the Local-override property value is False,sytem-properties-mode property value is environment or ovrride, the lookup order is location, Then environment or System.getproperty (), System.getenv () (before Spring 3.1)
The property file that the location points to is now loaded, and in addition to the environment that environment points to, the value of the property will be modified, depending on the value in the environment environment, when there is a property in the environment environment that has the same name as the property file in the location point.
If the Sytem-properties-mode property value is never, only the property file that the location points to is queried

When the Local-override property value is True, the file that the Properties-ref points to will be loaded, and if it encounters the same name, the property value will depend on the value in the file that Properties-ref points to

Therefore, the value obtained in the final program will be a combined value, generally recommended Sytem-properties-mode property value never avoid uncontrolled environment environment

Transferred from: http://blog.csdn.net/Rickesy/article/details/50791534

Spring Profile <context:property-placeholder> Tag usage ramble

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.