欢迎使用CSDN-markdown编辑器
来源:互联网 发布:四维星瓷砖软件 编辑:程序博客网 时间:2024/06/06 12:54
转自:http://blog.csdn.net/wu631464569/article/details/51898871?readlog
applicationContext.xml中设置读取jdbc.properties文件
<div class="article_bar clearfix"> <div class="artical_tag"> <span class="original">原创</span> <span class="time">2016年07月13日 15:58:58</span> </div> <ul class="article_tags clearfix csdn-tracking-statistics" data-mod="popu_377"> <li class="tit">标签:</li> <li><a href="http://so.csdn.net/so/search/s.do?q=java&t=blog" target="_blank">java</a> <span>/</span></li> <li><a href="http://so.csdn.net/so/search/s.do?q=java web&t=blog" target="_blank">java web</a> <span style="display: none;">/</span></li> </ul> <ul class="right_bar"> <li><button class="btn-noborder"><i class="icon iconfont icon-read"></i><span class="txt">5568</span></button></li> <!--<li><button class="btn-noborder"><i class="icon iconfont icon-dashang-"></i><span class="txt">0</span></button></li>--> <li class="edit" style="display: none;"> <a class="btn-noborder" href="http://write.blog.csdn.net/postedit/51898871"> <i class="icon iconfont icon-bianji"></i><span class="txt">编辑</span> </a> </li> <li class="del" style="display: none;"> <a class="btn-noborder" onclick="javascript:deleteArticle(fileName);return false;"> <i class="icon iconfont icon-shanchu"></i><span class="txt">删除</span> </a> </li> </ul> </div> <div id="article_content" class="article_content csdn-tracking-statistics" data-mod="popu_519" data-dsm="post" style="overflow: hidden;">
日期: 2016-7-13
内容: applicationContext.xml中设置读取jdbc.properties文件
一、 jdbc.properties的主要内容:
里面的内容主要是数据库连接和配置的一些基本信息。
- # JDBC Configuration
- jdbcDriverClassName=com.mysql.jdbc.Driver
- jdbcUrl=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
- hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
- jdbcUsername=root
- jdbcPassword=root
- # DBCP Pool settings
- jdbcInitialSize=5
- jdbcMaxActive=10
- jdbcMaxIdle=5
- jdbcMaxWait=30000
- jdbcValidationQuery=select 1
# JDBC Configuration# DBCP Pool settings jdbcInitialSize=5 jdbcMaxActive=10 jdbcMaxIdle=5 jdbcMaxWait=30000 jdbcValidationQuery=select 1
jdbcDriverClassName=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
jdbcUsername=root
jdbcPassword=root
二、 在applicationContext.xml中怎么读取jdbc.properties的配置信息;
- <?xml version=“1.0” encoding=“UTF-8”?>
- <beans xmlns=“http://www.springframework.org/schema/beans” xmlns:context=“http://www.springframework.org/schema/context” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:aop=“http://www.springframework.org/schema/aop”
- xmlns:tx=“http://www.springframework.org/schema/tx” xmlns:p=“http://www.springframework.org/schema/p” xmlns:util=“http://www.springframework.org/schema/util” xmlns:jdbc=“http://www.springframework.org/schema/jdbc”
- xmlns:cache=“http://www.springframework.org/schema/cache”
- xsi:schemaLocation=”
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd
- http://www.springframework.org/schema/jdbc
- http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
- http://www.springframework.org/schema/cache
- http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/util
- http://www.springframework.org/schema/util/spring-util.xsd”>
- <!– 引入jdbc配置文件 –>
- <bean id=“propertyConfigurer” class=“org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
- <property name=“locations”>
- <list>
- <value>classpath*:jdbc.properties</value>
- </list>
- </property>
- </bean>
- <!– dataSource 配置 –>
- <bean id=“dataSource” class=“com.alibaba.druid.pool.DruidDataSource” init-method=“init” destroy-method=“close”>
- <!– 基本属性 url、user、password –>
- <property name=“url” value=“{jdbc.url}"</span><span> </span><span class="tag">/></span><span> </span></span></li><li class=""><span> <span class="tag"><</span><span class="tag-name">property</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"username"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"{jdbc.username}” />
- <property name=“password” value=“${jdbc.password}” />
- <!– 配置初始化大小、最小、最大 –>
- <property name=“initialSize” value=“1” />
- <property name=“minIdle” value=“1” />
- <property name=“maxActive” value=“20” />
- <!– 配置获取连接等待超时的时间 –>
- <property name=“maxWait” value=“60000” />
- <!– 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 –>
- <property name=“timeBetweenEvictionRunsMillis” value=“60000” />
- <!– 配置一个连接在池中最小生存的时间,单位是毫秒 –>
- <property name=“minEvictableIdleTimeMillis” value=“300000” />
- <property name=“validationQuery” value=“SELECT ‘x’” />
- <property name=“testWhileIdle” value=“true” />
- <property name=“testOnBorrow” value=“false” />
- <property name=“testOnReturn” value=“false” />
- <!– 打开PSCache,并且指定每个连接上PSCache的大小 –>
- <property name=“poolPreparedStatements” value=“false” />
- <property name=“maxPoolPreparedStatementPerConnectionSize” value=“20” />
- <!– 配置监控统计拦截的filters –>
- <property name=“filters” value=“stat” />
- </bean>
- </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 引入jdbc配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:jdbc.properties</value> </list> </property> </bean> <!-- dataSource 配置 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本属性 url、user、password --> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="1" /> <property name="minIdle" value="1" /> <property name="maxActive" value="20" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="60000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x'" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> <property name="poolPreparedStatements" value="false" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <!-- 配置监控统计拦截的filters --> <property name="filters" value="stat" /> </bean> </beans>
在此推荐一篇java读取.properties的文章: http://www.cnblogs.com/xdp-gacl/p/3640211.html
三、 需要注意的点:
1、 classpath:“ <value>classpath*:jdbc.properties</value>”指的是什么?
以下内容参考文章: http://chengtong-java.iteye.com/blog/2254482
classpath 路径在每个J2ee项目中都会用到,即WEB-INF下面的classes目录,所有src目录下面的java、xml、properties等文件编译后都会在此,
所以在开发时常将相应的xml配置文件放于src或其子目录下;
引用classpath路径下的文件,只需在文件名前加classpath:(需保证该文件确实位于classpath路径下);
比如:
- <param-value>classpath:applicationContext-*.xml</param-value>
<param-value>classpath:applicationContext-*.xml</param-value>
或者引用其子目录下的文件,如
- <param-value>classpath:context/conf/controller.xml</param-value>
<param-value>classpath:context/conf/controller.xml</param-value>
classpath* 的使用:当项目中有多个classpath路径,并同时加载多个classpath路径下(此种情况多数不会遇到)的文件,*就发挥了作用,如果不加*,
则表示仅仅加载第一个classpath路径,代码片段:
- <param-value>classpath*:context/conf/controller*.xml</param-value>
<param-value>classpath*:context/conf/controller*.xml</param-value>
首先 classpath是指 WEB-INF文件夹下的classes目录
解释classes含义:
1.存放各种资源配置文件 eg.init.properties log4j.properties struts.xml
2.存放模板文件 eg.actionerror.ftl
3.存放class文件 对应的是项目开发时的src目录编译文件
总结:这是一个定位资源的入口
如果你知道开发过程中有这么一句话:惯例大于配置 那么也许你会改变你的想法
对于第二个问题
这个涉及的是lib和classes下文件访问优先级的问题: lib>classes
对于性能的影响应该不在这个范畴
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- C# 反射通过类名实例化类
- Android 开发笔记:Android特效 五种Toast详解
- Ubuntu中root用户和user用户的相互切换
- 建了索引却不走索引案例分析
- Java 使用动态代理来动态修改注解值
- 欢迎使用CSDN-markdown编辑器
- 全局环境变量
- 安装 ros by example vol 1 代码
- Linux 线程
- ofbiz——excel导出/加载工具类
- CALIPSO数据下载方法与可视化
- Retrofit2 封装
- Java序列化与反序列化
- 文件操作一些特性