首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

请问Mybatis和Spring结合的配置有关问题,多谢

2012-06-06 
请教Mybatis和Spring结合的配置问题,谢谢!我自己搭了一个struts2+Mybatis+Spring的框架,想通过MapperScann

请教Mybatis和Spring结合的配置问题,谢谢!
我自己搭了一个struts2+Mybatis+Spring的框架,想通过MapperScannerConfigurer来自动添加Mybatis的mapper,发现一个很奇怪的问题:

如果不使用MapperScannerConfigurer,而采取手动配置添加mapper,一切正常;
如果使用了MapperScannerConfigurer,服务器启动的时候会报错,但是报的是数据源配置读取错误,好像和MapperScannerConfigurer无关。

错误信息和spring配置文件如下:

XML code
<?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:aop="http://www.springframework.org/schema/aop"          xmlns:tx="http://www.springframework.org/schema/tx"           xmlns:jdbc="http://www.springframework.org/schema/jdbc"           xmlns:context="http://www.springframework.org/schema/context"     xsi:schemaLocation="    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd           http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"          default-autowire="byName">        <!-- 向spring注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、    PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor -->    <context:annotation-config></context:annotation-config>    <!-- 定义组件扫描的包名 -->    <context:component-scan base-package="edu.zjut" />        <!-- 导入属性配置文件 -->    <bean id="propertyConfigurer"        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="locations">            <list>                <value>classpath:mysql.properties</value>            </list>        </property>    </bean>    <!-- context:property-placeholder location="classpath:mysql.properties" / -->    <!-- 定义使用C3P0连接池的数据源 -->    <bean id="dataSource"        class="com.mchange.v2.c3p0.ComboPooledDataSource">        <!-- 指定连接数据库的JDBC驱动 -->        <property name="driverClass">            <value>${jdbc.driverClassName}</value>        </property>        <!-- 连接数据库所用的URL -->        <property name="jdbcUrl">            <value>${jdbc.url}</value>        </property>        <!-- 连接数据库的用户名 -->        <property name="user">            <value>${jdbc.user}</value>        </property>        <!-- 连接数据库的密码 -->        <property name="password">            <value>${jdbc.password}</value>        </property>        <!-- 设置数据库连接池的最大连接数 -->        <property name="maxPoolSize">            <value>${jdbc.maxPoolSize}</value>        </property>        <!-- 设置数据库连接池的最小连接数 -->        <property name="minPoolSize">            <value>${jdbc.minPoolSize}</value>        </property>        <!-- 设置数据库连接池的初始化连接数 -->        <property name="initialPoolSize">            <value>${jdbc.initialPoolSize}</value>        </property>        <!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->        <property name="maxIdleTime">            <value>${jdbc.maxIdleTime}</value>        </property>    </bean>        <!-- 集成myBaits框架,配置sqlSessionFatory -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="configLocation" value="classpath:mybatis-config.xml" />        <property name="dataSource" ref="dataSource" />    </bean>        <!-- 配置sqlSessionTemplate -->    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>    </bean>        <!-- mybatis自动扫描包下的mapper -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="org.mybatis.spring.sample.mapper" />        <!-- optional unless there are multiple session factories defined -->        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />    </bean>     <!-- bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="config.mybatis.mapper" />    </bean -->        <!-- 数据库的事务管理器配置 -->    <bean id="transactionManager"        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />    </bean>    <!-- 使用annotation定义数据库事务,这样可以在类或方法中直接使用@Transactional注解来声明事务 -->    <tx:annotation-driven transaction-manager="transactionManager" /></beans> 




[解决办法]
defined in file [D:\Workspaces\MyEclipse 10\.metadata\.me_tcat\webapps\SSM\WEB-INF\classes\applicationContext-common.xml

这里是问题的核心, 更具配置文件创建bean失败, 你要好好看一下你的
配置文件了。


错误描述:
Context initialization failed
错误产生原因:
spring注入出现问题:检测spring注入地方
1.注解注入,
是否有冲突
是否该类无法注入
2.配置文件
检测需要注入的具体实现类中,是否需要注入对象set方法编写正确
 
具体错误内容中会产生
注解注入错误,不容易看出具体哪个注入问题
配置文件注入问题,可以通过看报错日志定位错误

[解决办法]
${jdbc.maxPoolSize}这个肯定没有问题,

原因是你加入了MapperScannerConfigurer

他会优先于PropertyPlaceholderConfigurer执行,所以这个时候,${jdbc.maxPoolSize}还没有被解析呢,

故没有被mysql.properties里面的值所替换,所以出现NumberFormatException就是情理之中了

这是mybatis-spring的一个己经公开的问题

详细解决办法请看
http://code.google.com/p/mybatis/issues/detail?id=414

热点排行