使用spring+mybaits,将数据库连接信息写在jdbc.properties中,
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <value>classpath:jdbc.properties</value>
- </property>
- </bean>
- <property name="driverClass">
- <value>${mysql.driver}</value>
- </property>
- <property name="jdbcUrl">
- <value>${mysql.url}</value>
- </property>
- <property name="user">
- <value>${mysql.user}</value>
- </property>
- <property name="password">
- <value>${mysql.password}</value>
- </property>
将连接信息写死,查询没有问题,但是配置信息通过属性文件加载却报如下错误:
Could not load driverClass ${jdbc.driverClass} ,我使用的mybatis相应jar版本如下:
mybatis-3.2.6.jar mybatis-spring-1.2.2.jar,在网上查看有解决办法说是因为
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" autowire="byName">
- <property name="basePackage" value="com.xxxx.xxx.mybatis.mapper" />
- <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
- </bean>
使用sqlSessionFactory注入,改用sqlSessionFactoryBeanName注入就没有问题(不要使用sqlSessionFactory属性注入,使用 sqlSessionFactoryBeanName注入),因为这时不会立即初始化sqlSessionFactory,传入的只是名字,非bean, 所以不会引发提前初始化问题。但是我使用sqlSessionFactoryBeanName直接报错了,
- Invalid property 'sqlSessionFactoryBeanName' of bean class
所以,我怀疑是mybatis相关包的问题,将包的版本改为如下:
mybatis-3.0.5.jar mybatis-spring-1.0.1.jar问题解决。