Spring 使用外部属性文件

来源:互联网 发布:2083欧洲独立宣言 知乎 编辑:程序博客网 时间:2024/06/06 00:28

Spring 使用外部属性文件


一、说在前面

在配置文件里配置 bean 时,有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等),而这些部署细节实际上需要和 bean配置相分离。


Spring 提供了一个 PropertyPlaceholderConfigurer 的 BeanFactory 后置处理器, 这个处理器允许用户将 Bean 配置的部分内容外移到属性文件中。 可以在 Bean 配置文件里使用形式为 ${xxx} 的变量, PropertyPlaceholderConfigurer 从属性文件里加载属性, 并使用这些属性来替换变量。


二、以数据库配置为例

1、beans-properties.xml配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 导入属性文件 --><context:property-placeholder location="classpath:db.properties"/><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></beans>


2、db.properties属性文件

user=rootpassword=111111driverClass=com.mysql.jdbc.DriverjdbcUrl=jdbc:mysql:///spring

3、测试函数

package com.at.beans.properties;import java.sql.SQLException;import javax.sql.DataSource;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestProperties {public static void main(String[] args) throws SQLException {ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-properties.xml");DataSource dataSource = (DataSource) ctx.getBean("dataSource");System.out.println(dataSource.getConnection());/*System.out.println(dataSource.getConnection("root", "111111"));*/}}

4、输出结果

五月 23, 2017 8:21:29 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@218fcdce: startup date [Tue May 23 20:21:28 CST 2017]; root of context hierarchy五月 23, 2017 8:21:29 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [beans-properties.xml]五月 23, 2017 8:21:29 下午 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties信息: Loading properties file from class path resource [db.properties]五月 23, 2017 8:21:29 下午 com.mchange.v2.log.MLog <clinit>信息: MLog clients using java 1.4+ standard logging.五月 23, 2017 8:21:29 下午 com.mchange.v2.c3p0.C3P0Registry banner信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]五月 23, 2017 8:21:30 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hgecch9oery6778hvxyv|7e0d5447, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hgecch9oery6778hvxyv|7e0d5447, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql:///spring, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]com.mchange.v2.c3p0.impl.NewProxyConnection@8a93430


By luoyepiaoxue2014

微博地址: http://weibo.com/luoyepiaoxue2014 点击打开链接

原创粉丝点击