美文网首页SpringFramework
使用外部属性文件

使用外部属性文件

作者: 逗比寨主 | 来源:发表于2019-07-15 18:41 被阅读0次

使用外部属性文件

1.PropertyPlaceholderConfigurer

Spring提供了PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean配置部分内容移到外部的属性文件中,在Bean的配置文件使用${var}的形式来赋值。

Spring还允许在属性文件中使用${propName},实现属性之间的引用

db.properties:属性文件

user=root
password=root
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc://mysql:///spring_study

xml配置:

<context:property-placeholder location="db.properties"></context:property-placeholder>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="user" value="${user}"></property>
    <property name="password" value="${password}"></property>
    <property name="driverClass" value="${driverClass}"></property>
    <property name="jdbcUrl" value="${jdbcUrl}"></property>
</bean>

相关文章

网友评论

    本文标题:使用外部属性文件

    本文链接:https://www.haomeiwen.com/subject/kssokctx.html