Spring入门第九课
使用外部属性文件
在配置文件里面配置Bean时,有时需要在Bean的配置里面混入系统部署的细节信息(例如:文件路径,数据源配置信息等)而这些部署细节实际上需要和Bean配置相分离。
Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean配置的部分内容外移到属性文件中,可以在Bean配置文件里使用形式为${var}的变量,PropertyPlaceholderConfigurer从属性文件中加载属性,并使用这些属性来替换变量。
Spring还允许在属性文件中使用${propName},以实现属性之间的互相引用。
看下面如何使用外部属性文件
db.properties
user=root
password=logan123
driverclass=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql://localhost:3306/test
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.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>
package logan.spring.study.properties; import java.sql.SQLException; import javax.sql.DataSource; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-properties.xml");
DataSource datasource = (DataSource) ctx.getBean("dataSource");
System.out.println(datasource.getConnection());
}
}
输出结果为:
五月 20, 2017 10:34:14 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7aec35a: startup date [Sat May 20 22:34:14 CST 2017]; root of context hierarchy
五月 20, 2017 10:34:14 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-properties.xml]
五月 20, 2017 10:34:14 下午 com.mchange.v2.log.MLog
信息: MLog clients using java 1.4+ standard logging.
五月 20, 2017 10:34:15 下午 com.mchange.v2.c3p0.C3P0Registry
信息: Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
五月 20, 2017 10:34:15 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource
信息: 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, contextClassLoaderSource -> caller, dataSourceName -> ggeo7m9oamdcfe1ralrcg|711f39f9, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> ggeo7m9oamdcfe1ralrcg|711f39f9, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/test, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
com.mchange.v2.c3p0.impl.NewProxyConnection@6366ebe0 [wrapping: com.mysql.jdbc.JDBC4Connection@44f75083]
Spring入门第九课的更多相关文章
- Spring入门第一课:Spring基础与配置Bean
1.入门 Spring是简化java开发的一个框架,其中IoC和AOP是Spring的两个重要核心.由于Spring是非侵入性的,通过Ioc容器来管理bean的生命周期,还整合了许多其他的优秀框架,所 ...
- Spring入门第二课
看代码 package logan.spring.study; public class HelloWorld { private String name; public void setName2( ...
- Spring入门第二课:Spring配置Bean的细节
1.配置bean的作用域: 通过配置scope属性可以bean的作用域,参数有 prototype.request.session.singleton. 1)singleton为单例,IoC容器只会创 ...
- Spring入门第一课
今天先不讲Spring是什么. Spring网址:http://projects.spring.io/spring-framework/ Eclipse 安装开发IDE 在Eclipse Market ...
- Elasticsearch7.X 入门学习第九课笔记-----聚合分析Aggregation
原文:Elasticsearch7.X 入门学习第九课笔记-----聚合分析Aggregation 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. ...
- Spring入门之生命周期
好几日没有读东西了,今天本来要读mybatis原理部分,但是看到作者讲,只是学会用不用学那么深,遂直接开干spring,工作中一直用springboot,框架都是领导搭好的,每天的任务就是增删改查,挺 ...
- Spring入门学习(一)
SpringMVC基础平台补充(2016.03.03) 如果想要开发SpringMVC,那么前期依次安装好:JDK(jdk-8u74-windows-x64,安装后配置环境变量JAVA_HOME和CL ...
- NeHe OpenGL教程 第九课:移动图像
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- Spring入门(10)-Spring JDBC
Spring入门(10)-Spring JDBC 0. 目录 JdbcTemplate介绍 JdbcTemplate常见方法 代码示例 参考资料 1. JdbcTemplate介绍 JdbcTempl ...
随机推荐
- Java 学习 day02
1. 标识符 2. 常量 3. 进制的特点 4. 进制转换(十进制和二进制) 5. 进制转换(八进制和十六进制) 6. 进制转换(负数二进制) 7. 变量 8. 类型转换 9. 算术运算符 10. 转 ...
- Swift 学习笔记 (方法)
方法 是关联了特定类型的函数.类,结构体以及枚举都能定义实例方法,方法封装了给定类型特定的任务和功能.类,结构体和枚举同样可以定义类型方法,这是与类型本身关联的方法.类型方法与 Objective-C ...
- Vector 源码阅读
Vector在功能上与ArrayList是类似的,实现的数据结构也是一样的.但Vector是线程安全的,ArrayList是线程不安全的.
- LeetCode 017 4Sum
[题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
- 添加@ControllerAdvice后报错 Failed to invoke @ExceptionHandler method
首先.单独使用ControllerAdvice 无法正常工作.需要配合@EnableWebMvc 使用. @ControllerAdvice @EnableWebMvc pulbic class Ex ...
- 基于node开发的web应用,负载均衡的简单实践
集群(cluster)是一组相互独立的.通过高速网络互联的计算机,它们构成了一个组,并以单一系统的模式加以管理.一个客户与集群相互作用时,集群像是一个独立的服务器. 负载均衡(Load Balance ...
- Android6.0 旋转屏幕(五)WMS启动应用流程(屏幕方向相关)
一.强制设置方向 1.Activity 如果要强制设置一个Activity的横竖屏可以通过Manifest去设置,跟Activity相关的信息都会保存在ActivityInfo当中. android: ...
- 在eclipse创建Maven工程修改默认JRE
1. 打开Maven安装目录的setting.xml文件 2.找到profiles标签 3.加入下面配置即可 <profile> <id>jdk-1.8</id&g ...
- (转)epoll非阻塞读写规则
EPOLL技术 在linux的网络编程中,很长的时间都在使用select来做事件触发.在linux新的内核中,有了一种替换它的机制,就是epoll.相比于select,epoll最大的好处在于它不会随 ...
- lxml解析html与元素定位
lxml.beautifulsoup和正则表达式都可以解析定位html元素.前两者中,lxml比beautifulsoup效率高,所以采用lxml解析html. 方法: from lxml impor ...