Use of PropertyPlaceholderConfigurer in Spring

Source: Internet
Author: User

You can use PropertyPlaceholderConfigurer in Spring to read configuration information:


1. Properties

[Html]
Jdbc. driverClassName = com. mysql. jdbc. Driver
Jdbc. url = jdbc: mysql: // localhost: 3306/mysql
Jdbc. username = root
Jdbc. password = root
[Html]
<Bean class = "org. springframework. beans. factory. config. PropertyPlaceholderConfigurer">
<Property name = "locations" value = "classpath: jdbc. properties"/>
</Bean>
 
<Bean id = "dataSource" destroy-method = "close"
Class = "org. apache. commons. dbcp. BasicDataSource">
<Property name = "driverClassName" value = "$ {jdbc. driverClassName}"/>
<Property name = "url" value = "$ {jdbc. url}"/>
<Property name = "username" value = "$ {jdbc. username}"/>
<Property name = "password" value = "$ {jdbc. password}"/>
</Bean>

Spring 2.5 introduces the Context namespace, which can be configured through the context: property-placeholder element:

[Html]
<Context: property-placeholder location = "classpath: jdbc. properties"/>

PropertyPlaceholderConfigurer can not only search for Properties Based on the specified Properties file, but also use the java System attribute mode.

Original spring document:
[Html]
The propertyplaceholderpolicer not only looks for properties in the Properties file you specify.
By default it also checks against the Java System properties if it cannot find a property in the specified properties files.
You can customize this behavior by setting the systemPropertiesMode property of the caller with one of the following three supported integer values:
Never (0): Never check system properties

Fallback (1): Check system properties if not resolvable in the specified properties files. This is the default.

Override (2): Check system properties first, before trying the specified properties files. This allows system properties to override any other property source.

Nerver (0): do not check system configuration attributes.
Fallback (1): Check the System Configuration attributes. If the System Configuration attributes are not found in the specified attribute file, use the System Configuration attributes. The default value is

Override (2): First, check the system configuration attributes. the system configuration attributes take precedence.


[Html]
<Bean class = "org. springframework. beans. factory. config. PropertyPlaceholderConfigurer">
<Property name = "locations">
<Value> classpath: com/foo/strategy. properties </value>
</Property>
<Property name = "properties">
<Value> custom. strategy. class = com. foo. DefaultStrategy </value>
</Property>
</Bean>
 
<Bean id = "serviceStrategy" class = "$ {custom. strategy. class}"/>

If the class cannot be resolved at runtime to a valid class, resolution of the bean fails when it is about to be created, which is during thepreInstantiateSingletons () phase of anApplicationContext for a non-lazy-init bean.


The above section indicates that if the system configures attributes in the configuration information, classes in the attributes should be non-delayed loaded beans during loading.


Spring 3.1 starts to use org. springframework. context. support. PropertySourcesPlaceholderConfigurer.

 


[Html]
<! -- Method 1 -->
<Bean class = "org. springframework. context. support. PropertySourcesPlaceholderConfigurer">
<Property name = "locations">
<Value> classpath: jdbc. properties </value>
</Property>
</Bean>
 
<! -- Method 2 -->
<Bean class = "org. springframework. context. support. PropertySourcesPlaceholderConfigurer">
<Property name = "location">
<Value> classpath: jdbc. properties </value>
</Property>
</Bean>
 
<! -- Method 3 -->
<Context: property-placeholder location = "classpath: jdbc. properties"/>

 


 

Related Article

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.