applicationContext.xml中设置读取jdbc.properties文件

日期: 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  


二、 在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}" />  
        <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:context/conf/controller.xml</param-value>  

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

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

<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 
对于性能的影响应该不在这个范畴 







  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值