Spring 专题
专题目录
您的位置:java > Spring专题 > Spring PropertyPlaceholderConfigurer实例
Spring PropertyPlaceholderConfigurer实例
作者:--    发布时间:2019-11-20
很多时候,大多数spring开发人员只是把整个部署的详细信息(数据库的详细信息,日志文件的路径)写在xml bean配置文件如下:
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
	xsi:schemalocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="customerdao" class="com.h3.customer.dao.impl.jdbccustomerdao">

		<property name="datasource" ref="datasource" />
	</bean>

	<bean id="customersimpledao" class="com.h3.customer.dao.impl.simplejdbccustomerdao">

		<property name="datasource" ref="datasource" />
	</bean>

	<bean id="datasource"
		class="org.springframework.jdbc.datasource.drivermanagerdatasource">

		<property name="driverclassname" value="com.mysql.jdbc.driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/h3java" />
		<property name="username" value="root" />
		<property name="password" value="password" />
	</bean>

</beans>
但是,在企业环境中,部署的细节通常只可以由系统管理员或数据库管理员来'触碰',他们可能会拒绝直接访问你的bean的配置文件,它们会要求部署配置一个单独的文件,例如,一个简单的性能(properties)文件,仅具有部署细节。

propertyplaceholderconfigurer示例

为了解决这个问题,可以使用 propertyplaceholderconfigurer 类通过一个特殊的格式在外部部署细节到一个属性(properties )文件,以及访问bean的配置文件 – ${variable}.

创建一个属性文件(database.properties),包括数据库的详细信息,把它放到你的项目类路径。
jdbc.driverclassname=com.mysql.jdbc.driver
	jdbc.url=jdbc:mysql://localhost:3306/h3_db
	jdbc.username=root
	jdbc.password=123456
在声明bean配置文件和提供一个propertyplaceholderconfigurer映射到 刚才创建的“database.properties”属性文件。
<bean 
		class="org.springframework.beans.factory.config.propertyplaceholderconfigurer">

		<property name="location">
			<value>database.properties</value>
		</property>
	</bean>
完整的示例
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
	xsi:schemalocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean
		class="org.springframework.beans.factory.config.propertyplaceholderconfigurer">

		<property name="location">
			<value>database.properties</value>
		</property>
	</bean>

	<bean id="customerdao" class="com.h3.customer.dao.impl.jdbccustomerdao">

		<property name="datasource" ref="datasource" />
	</bean>

	<bean id="customersimpledao" 
                class="com.h3.customer.dao.impl.simplejdbccustomerdao">

		<property name="datasource" ref="datasource" />
	</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>

</beans>
可替代用法
还可以使用 propertyplaceholderconfigurer 于某个常量,分享给所有其他bean。例如,定义在一个属性文件中的日志文件的位置,并通过  ${log.filepath} 访问不同的 bean 配置文件的属性值。

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册