spring使用context:property-placeholder载不进属性问题

环境:spring3.1.1+mybatis3.2.8+mybatis-spring1.2.3

今天整合了SpringMVC + MyBatis,发现了一个问题,在这里做个记录,各位如果遇到相同的问题,可以参考下。

<!-- 引入jdbc配置文件 -->
<context:property-placeholder location="classpath:prop/jdbc.properties" />

引入文件时出现下面的错误,提示dataSource中的使用资源文件中key对应的value值没有引入进来。

### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class ‘${jdbc.driver}‘] with root cause java.lang.ClassNotFoundException: ${jdbc.driver}

首先确认jdbc.properties引入的路径没有问题。其次往下看。

解决案:

1. xml 头部将 default-autowire="byName"去掉。

2. 修改SqlSessionFactory。

<!-- 创建SqlSessionFactory,同时指定数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
    <property name="mapperLocations" value="classpath*:maper/*.map.xml" />
</bean> 

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="org.com.mars.dao" />
</bean>

改成:

<!-- 创建SqlSessionFactory,同时指定数据源 -->
<bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
    <property name="mapperLocations" value="classpath*:maper/*.map.xml" />
</bean> 

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="org.com.mars.dao" />
    <property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory" />
</bean>

这样,dataSource中就可以正常使用.properties文件中的key-value了。

附:完整的配置文件,这个文件是用来完成spring和mybatis的整合的xml。

spring-resources.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"
	default-autowire="byName" default-lazy-init="false">

	<!-- 自动扫描组件,需要把controller去掉,否则影响事务管理 -->
    <context:component-scan base-package="org.com.mars">
        <context:exclude-filter type="regex"
            expression="org.com.mars.controller.*" />
    </context:component-scan>

	<!-- 引入jdbc配置文件 -->
    <context:property-placeholder location="classpath:prop/jdbc.properties" /> 

	<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean> 

    <!-- 声明式事务管理 -->
    <aop:config>
        <aop:advisor pointcut="execution(* org.com.mars.service..*.*(..))"
            advice-ref="myAdvice" />
    </aop:config>
    <tx:advice id="myAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="query*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="list*" propagation="SUPPORTS" read-only="true" />  

            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />  

            <tx:method name="*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>  

    <!-- 创建SqlSessionFactory,同时指定数据源 -->
    <bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
        <property name="mapperLocations" value="classpath*:maper/*.map.xml" />
    </bean> 

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="org.com.mars.dao" />
        <property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory" />
    </bean>

    <!-- 可通过注解控制事务 -->
    <tx:annotation-driven />

    <!-- 数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<!-- Connection Info -->
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />

		<!-- Connection Pooling Info -->
		<property name="maxActive" value="${dbcp.maxActive}" />
		<property name="maxIdle" value="${dbcp.maxIdle}" />
		<property name="defaultAutoCommit" value="false" />
		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="3600000" />
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="3600000" />
	</bean>

</beans>

jdbc.properties

##jdbc
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

#dbcp settings
dbcp.initialSize=5
dbcp.maxActive=20
dbcp.maxIdle=10

jdbc.dbType=mysql
时间: 2024-07-30 20:51:22

spring使用context:property-placeholder载不进属性问题的相关文章

Spring BeanPostProcessor与动态加载数据源配置

前言: 本文旨在介绍Spring动态配置数据源的方式,即对一个DataSource的配置诸如jdbcUrl,user,password,driverClass都通过运行时指定,而非由xml静态配置. Spring构造Context的参数一般只包含配置文件路径和类加载器,如果需要达到动态传入配置参数的目的,需要Spring在初始化数据源相关bean的时候能够对原有配置执行修改或替换,为方便处理,本文将定义一个名为DynamicDataSourceConfigHolder的公共类提供配置数据存储.

spring中 context:property-placeholder 导入多个独立的 .properties配置文件

spring中 context:property-placeholder 导入多个独立的 .properties配置文件? Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的 Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceh

Spring boot 国际化自动加载资源文件问题

Spring boot 国际化自动加载资源文件问题 最近在做基于Spring boot配置的项目.中间遇到一个国际化资源加载的问题,正常来说只要在application.properties文件中定义正确的资源文件路径,Spring boot就启动时就会自动加载资源. spring.messages.basename=i18n/message 但是我的项目修改后获取消息时系统报错,找不到对应语言的资源配置.于是试图找到原因.Google好久都没找到,简直好像就我一个人遇到这鬼问题一样??.只好自

spring中 context:property-placeholder 导入多个独立的配置文件

spring中 context:property-placeholder 导入多个独立的 .properties配置文件? Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的 Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceh

spring中context:property-placeholder解析

1.替代常量 应用场所:1在开发阶段我们连接数据库时的连接url,username,password,driverClass等 2分布式应用中client端访问server端所用的server地址,port,service等 2.参数需经常变动 在项目开发阶段和交付阶段数据库的连接信息往往是不同的,分布式应用也是同样的情况.  能不能有一种解决方案可以方便我们在一个阶段内不需要频繁书写一个参数的值,而在不同阶段间又可以方便的切换参数配置信息 spring3中提供了一种简便的方式就是context

spring中context:property-placeholder/元素 转载

spring中context:property-placeholder/元素  转载 1.有些参数在某些阶段中是常量 比如 :a.在开发阶段我们连接数据库时的连接url,username,password,driverClass等 b.分布式应用中client端访问server端所用的server地址,port,service等 c.配置文件的位置 2.而这些参数在不同阶段之间又往往需要改变 比如:在项目开发阶段和交付阶段数据库的连接信息往往是不同的,分布式应用也是同样的情况. 期望:能不能有一

Spring中资源的加载ResourceLoader

Spring中资源的加载是定义在ResourceLoader接口中的,它跟前面提到的抽象资源的关系如下: ResourceLoader的源码 [java] view plain copy public interface ResourceLoader { /** Pseudo URL prefix for loading from the class path: "classpath:" */ String CLASSPATH_URL_PREFIX = ResourceUtils.CL

严重: Servlet.service() for servlet [spring] in context with path [/XX] threw exception [Request

一.描述:最近在使用springmvc+spring+hibernate4.0进行整合开发时出现了 严重: Servlet.service() for servlet [spring] in context with path [/XX] threw exception [Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thr

Spring 使用context:annotation-config的设置

Spring 使用context:annotation-config的设置: 还是需要声明Bean的,并且还可能自己定义Annotation: xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.