spring property value 引用properties文件和直接设置的区别
发布网友
发布时间:2023-04-02 00:31
我来回答
共1个回答
热心网友
时间:2023-11-24 07:41
1.PropertyPlaceholderConfigurer类
它是把属性中的定义的变量(var)替代,spring的配置文件中使用${var}的占位符
<beans>
<bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>db.properties</value></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>
</beans>
db.properties文件
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://proction:9002
jdbc.username=sa
jdbc.password=root
2.PropertyOverrideConfigurer类
跟PropertyPlaceholderConfigurer功能一样,不过用法不一样.不用占位符,在属性文件中
直接定义属性的值,这样就允许有默认值
<beans>
<bean id="configBean" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
<property name="location"><value>db.properties</value></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property>
<property name="url"><value>jdbc:hsqldb:hsql://proction:9002</value></property>
<property name="username"><value>test</value></property>
<property name="password"><value>123456</value></property>
</bean>
</beans>
db.properties文件
dataSource.username=admin
热心网友
时间:2023-11-24 07:41
1.PropertyPlaceholderConfigurer类
它是把属性中的定义的变量(var)替代,spring的配置文件中使用${var}的占位符
<beans>
<bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>db.properties</value></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>
</beans>
db.properties文件
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://proction:9002
jdbc.username=sa
jdbc.password=root
2.PropertyOverrideConfigurer类
跟PropertyPlaceholderConfigurer功能一样,不过用法不一样.不用占位符,在属性文件中
直接定义属性的值,这样就允许有默认值
<beans>
<bean id="configBean" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
<property name="location"><value>db.properties</value></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property>
<property name="url"><value>jdbc:hsqldb:hsql://proction:9002</value></property>
<property name="username"><value>test</value></property>
<property name="password"><value>123456</value></property>
</bean>
</beans>
db.properties文件
dataSource.username=admin