`

PropertyPlaceholderConfigurer模式从properties加载数据源参数失败的解决方案

阅读更多

        今天关注项目中的各配置文件参数设置是否恰当,然后就发现数据源是直接把各参数配置在applicationContent.xml文件当中,同时项目中其它模块又用到了properties配置文件引入属性值的做法,于是就想把数据源配置的参数也迁移到properties配置文件中来,便于以后的修改。

 

        由于使用的是springmvc框架(spring3.1.1+mybatis3.1.1+mybatis-spring-1.1.1),所以就在applicationContent.xml中配置PropertyPlaceholderConfigurer来加载properties配置文件,谁想这种以前在项目中应用很好使的方式今天怎么也通不过,配置完成,重新部署后就报如下错误:

 

严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' defined in ServletContext resource [/****/application-context.xml]: Initialization of bean failed;

 

nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in ServletContext resource[/***/application-context.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource';

 

nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/*****/application-context.xml]: Initialization of bean failed;

 

nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'checkoutTimeout';

 

nested exception is java.lang.NumberFormatException: For input string: "${cpool.checkoutTimeout}"

。。。。。。

 

      一开始怀疑是spring版本问题,就搜索了一下“spring3  PropertyPlaceholderConfigurer”,结果发现还真有类似的提问,只不过比我问的更准确,一下就把问题定位到了问题的根源-------mybatis下的MapperScannerConfigurer扫描模式造成了bean的加载顺序改变从而使得PropertyPlaceholderConfigurer无法正常加载。

 

       具体说来就是,myabatis使用MapperScannerConfigurer扫描模式后他会优先于PropertyPlaceholderConfigurer执行,所以这个时候,${cpool.checkoutTimeout}还没有被properties文件里面的值所替换,所以出现TypeMismatchException,然后就异常了

 

       知道了异常原因所在,那么问题解决结会快一些了,于是按照相关搜索,查阅下面的帖子,基本找到解决方案:

http://www.oschina.net/question/188964_32305

 

     当然,毕竟不是一模一样的错误,还是需要做一些调整,下面把调整的内容列出来,希望对同样问题的朋友有帮助,请留意标红的地方:

 

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="locations">
		<list>
			<value>classpath:xxxxx.properties</value>
		</list>
	</property>
</bean>
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClassName}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="autoCommitOnClose" value="true"/>
		<property name="checkoutTimeout" value="${cpool.checkoutTimeout}"/>
</bean>

<!--只要下面bean的id不叫sqlSessionFactory,就成-->

<bean id="ysqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
    <property name="mapperLocations" >
        <list>
	     <value>classpath:com/xxx/xxx/xxx/*.xml</value>
	     <value>classpath:com/xxx/xxx/xxx/*.xml</value>
        </list>
     </property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
	<constructor-arg index="0" ref="ysqlSessionFactory"></constructor-arg>
	<constructor-arg index="1" value="BATCH"></constructor-arg>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="com.xxx.xxx.dao,com.xxx.xxx.xxx.dao,com.xxx.xxx.xxx.dao"/>
         <!--核心就是添加下面一句。后面那个属性是value,不是ref,切记-->
	<property name="sqlSessionFactoryBeanName" value="ysqlSessionFactory" />
</bean>

 

 

在3.1.1版本中配置的时候提供了以下属性值关联:

 

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  

     <property name="basePackage" value="com.xxxx.dal.mapper" /> 

     <property name="sqlSessionFactoryBeanName" value="ysSqlSessionFactory" />

     <!--<property name="sqlSessionFactory" ref="ysSqlSessionFactory"></property> -->

</bean>

改用sqlSessionFactoryBeanName注入就没有问题(不要使用sqlSessionFactory属性注入,使用sqlSessionFactoryBeanName注入),因为这时不会立即初始化sqlSessionFactory,传入的只是名字,非bean,所以不会引发提前初始化问题。。

 

 

最后再备注两个链接:

PropertyPlaceholderConfigurer介绍和应用

http://www.cnblogs.com/yl2755/archive/2012/05/06/2486752.html

Mybatis学习系列:

http://legend2011.blog.51cto.com/3018495/908956

分享到:
评论
1 楼 di1984HIT 2014-10-22  
不错,谢谢啦!!

相关推荐

    Spring PropertyPlaceholderConfigurer配置文件加载器集成ZooKeeper来实现远程配置读取

    当需要读取配置时,这个加载器会从ZooKeeper的指定节点读取数据,而不是从本地属性文件。这样,每次启动或运行时,服务可以从ZooKeeper获取最新的配置,而无需重启。 以下是一个简单的步骤概述: 1. 在ZooKeeper上...

    Spring中PropertyPlaceholderConfigurer的使用

    PropertyPlaceholderConfigurer 是 Spring 框架中的一个重要组件,用于加载和管理 Properties 文件。它能够将 Properties 文件中的键值对注入到 Spring 的 bean 中,从而实现了配置的外部化和动态化。 基本使用方法...

    Spring属性占位符PropertyPlaceholderConfigurer的使用

    当Spring容器启动时,`PropertyPlaceholderConfigurer`会自动找到`config.properties`文件,读取其中的`db.url`属性,并替换掉`${db.url}`,从而完成数据源的配置。 除了基本的属性文件加载,`...

    加载properties配置文件的几种方法

    Spring提供了`PropertyPlaceholderConfigurer`类,可以方便地从.properties文件中读取属性。首先,在Spring的配置文件(如`applicationContext.xml`)中定义一个bean,然后指定properties文件的位置: ```xml ...

    springmvc动态切换数据源demo

    // 这里可以根据业务逻辑动态选择数据源,例如从请求上下文或线程Local中获取 return "dataSource1"; // 默认使用数据源1 } }; routingDataSource.setTargetDataSources( Collections.singletonMap(...

    SSH框架中的多数据源配置.DOC

    在配置数据源连接信息时,我们使用了Spring框架的PropertyPlaceholderConfigurer来实现外部配置文件的注入。这种方式能够使得应用程序更加灵活和可维护。 在配置事务管理时,我们使用了Spring框架的...

    Spring3.0 配置文件中加载Properties文件的小例子

    Properties文件则常用于存储应用程序的配置参数,如数据库连接字符串、系统环境变量等。本篇将详细讲解如何在Spring 3.0的配置文件中加载Properties文件,以便在运行时动态获取和使用这些配置。 首先,我们需要一个...

    SSM 读取properties文件

    这是一个Spring的bean定义类,它允许我们从properties文件中加载和解析属性值,然后将这些值注入到其他bean的属性中。首先,我们需要创建一个properties文件,例如`application.properties`,并放入项目的类路径下...

    Spring+Ibatis 访问多个数据源

    在企业级应用开发中,有时候我们需要访问多个不同的数据源,比如在一个系统中既有Oracle数据库又有SQL Server数据库。这种情况下,Spring框架提供了强大的支持来管理多个数据源,使得我们可以灵活地根据业务需求切换...

    spring无法读取properties文件数据问题详解

    Spring 框架在读取 properties 文件数据时可能会遇到一些问题,本文将对这些问题进行详细的解释和解决。 问题一:Controller 中无法读取 properties 文件数据 在 Spring 框架中,Controller 中注入的 @Value 配置...

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    在Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面包含这些数据源的jar文件和依赖文件及配置文件。。 如Bonecp目前听说是最快的数据源,速度是传统的c3p0的25倍, bonecp.properties文件: ...

    Spring加载properties文件的方法

    在Spring框架中,加载properties文件是常见的配置管理方式,这有助于将应用程序的配置参数与源代码分离,便于维护和更新。本文将详细介绍Spring加载properties文件的两种主要方法:XML方式和注解方式。 ### XML方式...

    spring4.0引用properties

    Properties文件是一种常见的存储配置属性的方式,它允许开发者将配置参数与代码分离,从而实现更好的可维护性和灵活性。本实例将深入探讨如何在Spring 4.0中引用和使用properties文件。 首先,我们需要创建一个...

    6 用Properties补充hibernate.cfg.xml配置

    2. **加载Properties文件**:在应用程序启动时,使用`Properties`类加载这个文件: ```java Properties props = new Properties(); FileInputStream fis = new FileInputStream("hibernate.properties"); props....

    Spring动态加载配置文件

    除了这两种方式,Spring Boot引入了更强大的`ConfigDataLocationResolver`和`ConfigDataLoader`接口,它们允许我们自定义配置数据的加载逻辑,支持更丰富的数据源,如Git、HTTP等。 在实际应用中,我们可能还需要...

    java 获取properties的几种方式(csdn)————程序.pdf

    例如,Spring的`PropertyPlaceholderConfigurer`和`&lt;context:property-placeholder&gt;`适用于Spring应用,而`ResourceBundle`适合处理本地化,`Properties`类则是一个通用解决方案。理解并熟练掌握这些方法,将有助于...

    spring,配置文件从属性文件读取JDBC连接的相关参数

    这段配置告诉Spring在类路径下查找`jdbc.properties`并将其内容加载到`PropertyPlaceholderConfigurer` bean中。 3. **注入属性值**: 现在,我们可以在其他bean的定义中使用`@Value`注解来注入属性值。例如,...

    SrpingDruid数据源加密数据库密码的示例代码

    Druid 数据源提供了相关的解决方案,我们可以使用 Druid 的加密机制来加密数据库密码。 知识点2:Druid 数据库密码加密 Druid 数据库密码加密是使用 Druid 的加密机制来加密数据库密码的过程。在这个过程中,我们...

    org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

    此外,Spring 3.1引入了`PropertySourcesPlaceholderConfigurer`,它可以处理多种类型的属性源,包括系统属性、环境变量以及从JNDI等不同来源获取的属性。 `spring1.docx`和`spring2.docx`可能是两份关于Spring框架...

    Spring如何使用PropertyPlaceholderConfigurer读取文件

    使用PropertyPlaceholderConfigurer可以解决一些问题,例如,每一次加载配置文件时,我们都需要手工读取配置文件,这样编码麻烦且代码不优雅。使用PropertyPlaceholderConfigurer可以将配置文件的路径放在java虚拟机...

Global site tag (gtag.js) - Google Analytics