欢迎使用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&amp;t=blog" target="_blank">java</a> <span>/</span></li>                <li><a href="http://so.csdn.net/so/search/s.do?q=java web&amp;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的主要内容:


里面的内容主要是数据库连接和配置的一些基本信息。

[java] view plain copy
print?
  1. # JDBC Configuration    
  2. jdbcDriverClassName=com.mysql.jdbc.Driver    
  3. jdbcUrl=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull    
  4. hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect    
  5. jdbcUsername=root    
  6. jdbcPassword=root    
  7. # DBCP Pool settings    
  8. jdbcInitialSize=5    
  9. jdbcMaxActive=10    
  10. jdbcMaxIdle=5    
  11. jdbcMaxWait=30000    
  12. jdbcValidationQuery=select 1    
# 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


二、 在applicationContext.xml中怎么读取jdbc.properties的配置信息;


[html] view plain copy
print?
  1. <?xml version=“1.0” encoding=“UTF-8”?>    
  2. <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”    
  3.     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”    
  4.     xmlns:cache=“http://www.springframework.org/schema/cache”    
  5.     xsi:schemaLocation=”    
  6.     http://www.springframework.org/schema/context    
  7.     http://www.springframework.org/schema/context/spring-context.xsd    
  8.     http://www.springframework.org/schema/beans    
  9.     http://www.springframework.org/schema/beans/spring-beans.xsd    
  10.     http://www.springframework.org/schema/tx    
  11.     http://www.springframework.org/schema/tx/spring-tx.xsd    
  12.     http://www.springframework.org/schema/jdbc    
  13.     http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd    
  14.     http://www.springframework.org/schema/cache    
  15.     http://www.springframework.org/schema/cache/spring-cache-3.1.xsd    
  16.     http://www.springframework.org/schema/aop    
  17.     http://www.springframework.org/schema/aop/spring-aop.xsd    
  18.     http://www.springframework.org/schema/util    
  19.     http://www.springframework.org/schema/util/spring-util.xsd”>    
  20.     
  21.     <!– 引入jdbc配置文件 –>    
  22.     <bean id=“propertyConfigurer” class=“org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>    
  23.         <property name=“locations”>    
  24.             <list>    
  25.                 <value>classpath*:jdbc.properties</value>    
  26.             </list>    
  27.         </property>    
  28.     </bean>    
  29.     
  30.     <!– dataSource 配置 –>    
  31.     <bean id=“dataSource” class=“com.alibaba.druid.pool.DruidDataSource” init-method=“init” destroy-method=“close”>    
  32.         <!– 基本属性 url、user、password –>    
  33.         <property name=“url” value={jdbc.url}"</span><span>&nbsp;</span><span class="tag">/&gt;</span><span>&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">property</span><span>&nbsp;</span><span class="attribute">name</span><span>=</span><span class="attribute-value">"username"</span><span>&nbsp;</span><span class="attribute">value</span><span>=</span><span class="attribute-value">"{jdbc.username}” />    
  34.         <property name=“password” value=“${jdbc.password}” />    
  35.     
  36.         <!– 配置初始化大小、最小、最大 –>    
  37.         <property name=“initialSize” value=“1” />    
  38.         <property name=“minIdle” value=“1” />    
  39.         <property name=“maxActive” value=“20” />    
  40.     
  41.         <!– 配置获取连接等待超时的时间 –>    
  42.         <property name=“maxWait” value=“60000” />    
  43.     
  44.         <!– 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 –>    
  45.         <property name=“timeBetweenEvictionRunsMillis” value=“60000” />    
  46.     
  47.         <!– 配置一个连接在池中最小生存的时间,单位是毫秒 –>    
  48.         <property name=“minEvictableIdleTimeMillis” value=“300000” />    
  49.     
  50.         <property name=“validationQuery” value=“SELECT ‘x’” />    
  51.         <property name=“testWhileIdle” value=“true” />    
  52.         <property name=“testOnBorrow” value=“false” />    
  53.         <property name=“testOnReturn” value=“false” />    
  54.     
  55.         <!– 打开PSCache,并且指定每个连接上PSCache的大小 –>    
  56.         <property name=“poolPreparedStatements” value=“false” />    
  57.         <property name=“maxPoolPreparedStatementPerConnectionSize” value=“20” />    
  58.     
  59.         <!– 配置监控统计拦截的filters –>    
  60.         <property name=“filters” value=“stat” />    
  61.     </bean>    
  62. </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路径下);

比如:

[html] view plain copy
print?
  1. <param-value>classpath:applicationContext-*.xml</param-value>    
<param-value>classpath:applicationContext-*.xml</param-value>  

或者引用其子目录下的文件,如  

[html] view plain copy
print?
  1. <param-value>classpath:context/conf/controller.xml</param-value>    
<param-value>classpath:context/conf/controller.xml</param-value>  

 classpath* 的使用:当项目中有多个classpath路径,并同时加载多个classpath路径下(此种情况多数不会遇到)的文件,*就发挥了作用,如果不加*,

则表示仅仅加载第一个classpath路径,代码片段: 

[html] view plain copy
print?
  1. <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 

对于性能的影响应该不在这个范畴 










原创粉丝点击