本文主要是介绍spring:context:property-placeholder location=classpath:application.properties /,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
spring中context:property-placeholder/元素
1.有些参数在某些阶段中是常量
2.而这些参数在不同阶段之间又往往需要改变
期望:能不能有一种解决方案可以方便我们在一个阶段内不需要频繁书写一个参数的值,而在不同阶段间又可以方便的切换参数配置信息
解决:spring3中提供了一种简便的方式就是context:property-placeholder/元素
只需要在spring的配置文件里添加一句:<context:property-placeholder
#jdbc配置
test.jdbc.driverClassName=com.mysql.jdbc.Driver
test.jdbc.url=jdbc:mysql://localhost:3306/test
test.jdbc.username=root
test.jdbc.password=root
行内#号后面部分为注释
应用:
1.这样一来就可以为spring配置的bean的属性设置值了,比如spring有一个jdbc数据源的类DriverManagerDataSource
在配置文件里这么定义bean:
<bean id="testDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
</bean>
2.甚至可以将${ }这种形式的变量用在spring提供的注解当中,为注解的属性提供值
---------------------------------------------------------
外在化应用参数的配置
在开发企业应用期间,或者在将企业应用部署到生产环境时,应用依赖的很多参数信息往往需要调整,比如LDAP连接、RDBMS JDBC连接信息。对这类信息进行外在化管理显得格外重要。PropertyPlaceholderConfi
PropertyPlaceholderConfi
db.username=scott
db.password=tiger
如下内容摘自propertyplaceholderconfi
代码示例1:
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>userinfo.properties</value> </list> </property>
</bean> <bean name="userInfo" class="test.UserInfo"> <property name="username" value="${db.username}"/> <property name="password" value="${db.password}"/>
</bean>
代码示例2
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>WEB-INF/classes/datasource.properties</value>
</list>
</property>
</bean>
通过运行并分析PropertyPlaceholderConfi
<context:property-placeholder
或<context:property-placeholder location="classpath:application.properties" />
PropertyPlaceholderConfi
这篇关于spring:context:property-placeholder location=classpath:application.properties /的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!