项目迭代,新增加了两张数据表,所以项目中增加了Dao和相应的Mapper.xml文件。开发完毕,往测试环境部署,可是测试环境的bubbo服务却启动不成功,然而在IDE和在Windows下启动却是没有问题的。经过排除,不是新加的Dao和Mapper.xml的文件的问题。在linux下启动会一直报如下的问题:
2017-02-23 11:54:32,456 [main] INFO com.alibaba.druid.pool.DruidDataSource (DruidDataSource.java:456) - {dataSource-866} inited
2017-02-23 11:54:32,458 [main] DEBUG org.springframework.beans.factory.support.AbstractBeanFactory (AbstractBeanFactory.java:1380) - Ignoring bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'videoDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public final void org.mybatis.spring.support.SqlSessionDaoSupport.setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in URL [jar:file:/apply/dream-finance-server1/dream-event-provider-1.0.0-SNAPSHOT/lib/dream-event-provider-1.0.0-SNAPSHOT.jar!/META-INF/spring/spring-mybatis.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 URL [jar:file:/apply/dream-finance-server1/dream-event-provider-1.0.0-SNAPSHOT/lib/dream-event-provider-1.0.0-SNAPSHOT.jar!/META-INF/spring/spring-mybatis.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.mysql.jdbc.Util
然而打的包中有mysql-connect的jar包
项目中的数据源的配置的片段如下:
<!-- 配置sqlsessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
<!--检测别名 -->
<property name="typeAliasesPackage" value="com.dream.event.entity" />
<property name="mapperLocations" value="classpath*:mybatis/mapper/*Mapper.xml" />
</bean>
<!-- 配置扫描包,加载mapper代理对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dream.event.dao" />
</bean>
经过多次的测试,单独用这两个Dao和Mapper.xml进行启动,可以启动成功。加任意一套的话启动了大概三分钟能拿启动成功。加了这两套的话启动不成功。项目中之前有Mapper.xml文件47个,于是猜测是不是Mybatis扫描的Mappe.xml有限制,可是查找资料也没有查找到。
后来抱着试一试的态度,将项目中的mybatis-spring由原来的1.1.0升级到1.3.1;
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
加spring-jdbc的依赖是因为原来的mybatis-spring的1.1.0版本会依赖spring-jdbc,但是1.3.1的版本没有依赖,所以这里就添加了依赖,没有指定版本是因为,版本在父级中统一做了管理。
mybatis-spring的版本升级后,linux下启动成功。
现在我的解决办法是这样的,不知道低版本的为什么会出现这种问题。