Got it

Use of the PropertyPlaceholder Configurer class in Spring

516 0 8 0 0

Hello, everyone!

This post will share with you the use of the PropertyPlaceholder configured class in spring.

This class is used to parse Java Properties property file values and provide alternative use of property values during spring configuration. Here are three common ways to use it.

1. Basic methods of use:

Spring-config.xml

<bean id="propertyConfigurer" class="org.spring framework.beans.factory.config.PropertyPlaceholder Configurer">
<property name="location">
<value>classpath:/spring/include/dbQuery.properties</value>
</property>
</bean>


Classpath refers to file writing in the SRC directory.

2. When multiple Properties files exist, configuration requires locations:

Spring-config.xml

<bean id="propertyConfigurer" class="org.spring framework.beans.factory.config.PropertyPlaceholder Configurer">
<property name="locations">
<list>
<value>classpath:/spring/include/jdbc-parms.properties</value>
<value>classpath:/spring/include/base-config.properties</value>
</list>
</property>
</bean>

3. Next, we need to use multiple Property Placeholder Configurers to decentralize the configuration so as to integrate multiple decentralized Proerties files under multiple projects. The configuration is as follows

Xml code

<bean id="propertyConfigurerForProject1" class="org.spring framework.beans.factory.config.Property Placeholder Configurer">
<property name="order" value="1"/>
<property name="ignoreUnresolvable Placeholders" value="true"/>
<property name="location">
<value>classpath:/spring/include/dbQuery.properties</value>
</property>
</bean>


Xml code

<bean id="propertyConfigurerForProject2" class="org.spring framework.beans.factory.config.Property Placeholder Configurer">
<property name="order" value="2"/>
<property name="ignoreUnresolvable Placeholders" value="true"/>
<property name="locations">
<list>
<value>classpath:/spring/include/jdbc-parms.properties</value>
<value>classpath:/spring/include/base-config.properties</value>
</list>
</property>
</bean>

Where the order attribute represents its loading order, ignoreUnresolvable Placeholders need to be set to true for ignoreUnresolvable Placeholders to ignore unresolved Placeholders, if multiple Property Placeholder Configurers are configured.

Usage in Manager code:

In the configuration of controller, some configuration files configure database user password and so on. In spring loading stage, the configuration file will be read and connected to the database. The configuration of database connection is as follows:

In persistence-context.xml, the configuration is as follows:

<! - Persistence context for Hadoop OM Model Setup - >
<bean id="omDataSource" class="com.huawei.hadoop.om.controller.persistence.MyDataSource" destroy-method="close">
<property name="driverClassName" value="${database.driverClassName}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<property name="validation Query" value="select 1"/>
<property name="maxActive" value="60"/>
</bean>


Among them, ${database.url}, ${database.username} and ${database.password} are all values obtained by parsing the value of the Java Properties property file. The configuration of properties is as follows:

In the controller-context.xml file, the following properties are configured:

Global Property Placement Holders

<bean
Class= "org. spring framework. beans. factory. config. Property Placeholder Configurer">
<property name="ignoreUnresolvable Placeholders" value="true"/>
<property name="locations">
<list>
< value > controller-private. properties </value> <! - - for internal purpose only - > <
<value>controller.properties</value>
<value>license.properties</value>
</list>
</property>
</bean>

Several of the Yellow Icon files, the property files loaded during spring loading, in which key-value pairs can be applied in other configurations, such as ${database.url}, ${database.username} and ${database.password} are configuration items in controller.properties file.


#\\\\\\\\\\\\\\\\\\\\
Database.url = jdbc: postgresql://127.0.0.1:<pg-port>/<db-name>
#\\\\\\\\\\\\\\\\\\\\
# Schema authentication.
#\\\\\\\\\\\\\\\\\\\\
Database. username = < DATABASE_USERNAME >
Database.password=<DATABASE_PASSWORD>


This is the second of the three uses described above.


Comment

You need to log in to comment to the post Login | Register
Comment

Notice: To protect the legitimate rights and interests of you, the community, and third parties, do not release content that may bring legal risks to all parties, including but are not limited to the following:
  • Politically sensitive content
  • Content concerning pornography, gambling, and drug abuse
  • Content that may disclose or infringe upon others ' commercial secrets, intellectual properties, including trade marks, copyrights, and patents, and personal privacy
Do not share your account and password with others. All operations performed using your account will be regarded as your own actions and all consequences arising therefrom will be borne by you. For details, see " User Agreement."

My Followers

Login and enjoy all the member benefits

Login

Block
Are you sure to block this user?
Users on your blacklist cannot comment on your post,cannot mention you, cannot send you private messages.
Reminder
Please bind your phone number to obtain invitation bonus.