Spring --- PropertyPlaceholderConfigurer
A property resource configurer that resolves placeholders in bean property values of context definitions. It pulls values from a properties file into bean definitions.
The default placeholder syntax the Ant / Log4J / JSP EL style:
${ ... }
Example XML context definition:
<!-- 1.引入一个属性文件-->
<
bean
id
=
"propertyConfigurer"
class
=
"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
>
<
property
name
=
"location"
>
<
value
>
classpath:hibernate/jdbctest.properties
</
value
>
</
property
>
<!-- 指定外部文件的编码方式-->
<
property
name
=
"fileEncoding"
>
<
value
>
UTF-8
</
value
>
</
property
>
</
bean
>
<!-- 2.引入多个属性文件-->
<
bean
id
=
"propertyConfigurer"
class
=
"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
>
<
property
name
=
"locations"
>
<
set
>
<
value
>
classpath:hibernate/jdbctest.properties
</
value
>
<
value
>
classpath:others/test.properties
</
value
>
</
set
>
</
property
>
<!-- 指定外部文件的编码方式-->
<
property
name
=
"fileEncoding"
>
<
value
>
UTF-8
</
value
>
</
property
>
</
bean
>
<
bean
id
=
"dataSource"
class
=
"org.springframework.jdbc.datasource.DriverManagerDataSource"
>
<
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
>
Example property definition:
driverClassName=
oracle.jdbc.driver.OracleDriver
url=
jdbc
:
oracle:thin:@localhost:1521:orcl
username=test
password=test123
PropertyPlaceholderConfigurer 的作用是将占位符指向的配置信息放到bean对应的value中;如果在指定的配置文件中未找到对应的属性,还会在java的System类属性中查找。