您的位置:首页 > 编程语言 > Java开发

spring 和mybatis整合时 使用context:property-placeholder载不进属性 还报org.springframework.beans.factory.BeanCrea

2016-04-17 11:32 447 查看
在spring核心配置文件(applicationContext.xml)中配置

<!-- 加载数据库连接信息的properties文件 -->

<context:property-placeholder location="classpath:db.properties"/>

<!-- 使用第三方的数据库连接池dbcp -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="${jdbc.driver}"></property>

<property name="url" value="${jdbc.url}"></property>

<property name="username" value="${jdbc.username}"></property>

<property name="password" value="${jdbc.password}"></property>

<!-- 开发阶段数据库最大的连接建议设置小一点 可设置3个 -->

<property name="maxActive" value="${jdbc.maxActive}"></property>

<property name="maxIdle" value="${jdbc.maxIdle}"></property>

</bean>

<!-- 配置SqlSessionFactory -->

<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">

<!-- 注入数据源 -->

<property name="dataSource" ref="dataSource"></property>

<!-- 配置SqlMapConfig.xml文件 -->

<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>

</bean>

<!--使用mybatis自动扫描mapper接口 代码如下-->

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

<!-- 会话工厂 -->

<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"></property>

<!-- 扫描包路径 多个包中间使用半角逗号分隔 -->

<property name="basePackage" value="cn.xing.ssm.dao.mapper"></property>

</bean>

问题:在spring 和mybatis组合时 使用该配置方式会报异常

异常为:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' defined in
class path resource [spring/applicationContext-dao.xml]: Cannot resolve reference to bean 'sqlSessionFactoryBean' while setting bean property
'sqlSessionFactoryBeanName'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'sqlSessionFactoryBean' defined in class path resource [spring/applicationContext-dao.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
class path resource [spring/applicationContext.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 'maxActive'; nested exception is java.lang.NumberFormatException: For input
string: "${jdbc.maxActive}"

问题分析

在spring里使用org.mybatis.spring.mapper.MapperScannerConfigurer 进行自动扫描的时候,设置了sqlSessionFactory 的话,可能会导致
PropertyPlaceholderConfigurer失效,也就是用${jdbc.username}这样之类的表达式,将无法获取到properties文件里的内容。 导致这一
原因是因为,MapperScannerConigurer实际是在解析加载bean定义阶段的,这个时候要是设置sqlSessionFactory的话,会导致提前初始化 一些类,这个时候,PropertyPlaceholderConfigurer还没来得及替换定义中的变量,导致把表达式当作字符串复制了。 但如果不设置

sqlSessionFactory 属性的话,就必须要保证sessionFactory在spring中名称一定要是sqlSessionFactory
,否则就无法自动注入。又或者直接定 义 MapperFactoryBean ,再或者放弃自动代理接口方式。

解决方法:

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

代码实现:

<!-- 加载数据库连接信息的properties文件 -->

<context:property-placeholder location="classpath:db.properties"/>

<!-- 使用第三方的数据库连接池dbcp -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="${jdbc.driver}"></property>

<property name="url" value="${jdbc.url}"></property>

<property name="username" value="${jdbc.username}"></property>

<property name="password" value="${jdbc.password}"></property>

<!-- 开发阶段数据库最大的连接建议设置小一点 可设置3个 -->

<property name="maxActive" value="${jdbc.maxActive}"></property>

<property name="maxIdle" value="${jdbc.maxIdle}"></property>

</bean>

<!-- 配置SqlSessionFactory -->

<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">

<!-- 注入数据源 -->

<property name="dataSource" ref="dataSource"></property>

<!-- 配置SqlMapConfig.xml文件 -->

<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>

</bean>

<!--使用mybatis自动扫描mapper接口 代码如下-->

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

<!-- 会话工厂 -->

<property name="sqlSessionFactory" value="sqlSessionFactoryBean"></property>

<!-- 扫描包路径 多个包中间使用半角逗号分隔 -->

<property name="basePackage" value="cn.xing.ssm.dao.mapper"></property>

</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: