作者:zzc1684
出处:
struts2.1.6+hibernate3.3+spring3.0遇到的问题 首先进行单元测试:出现问题
- package com.zzc.junit;
- import java.util.List;
- import javax.annotation.Resource;
- import org.junit.Test;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
- import org.springframework.transaction.annotation.Transactional;
- import com.zzc.dao.SqlServerDao;
- @ContextConfiguration("classpath:applicationContext.xml")
- public class Test1 extends AbstractJUnit4SpringContextTests {
- SqlServerDao sqlServerDAO;
- @Test
- public void query() {
- List<String> list = sqlServerDAO.query();
- for(String s : list) {
- System.out.println(s);
- }
- }
- public SqlServerDao getSqlServerDAO() {
- return sqlServerDAO;
- }
- @Resource(name="sqlServerDao")
- public void setSqlServerDAO(SqlServerDao sqlServerDAO) {
- this.sqlServerDAO = sqlServerDAO;
- }
- }
问题1: Caused by: java.lang.NoClassDefFoundError: org/junit/runners/BlockJUnit4ClassRunner上网查看了下,原来是junit的jar包版本问题,spring2.5.6配搭junit4.4是没有问题的,但是spring3.0配搭junit4.4,使用了
@ContextConfiguration等spring3.0新的annotation就会有问题,将junit4.4改为junit4.82版本,问题解决。
问题2: org/aopalliance/intercept/MethodInterceptor Spring3.0 运行问题
报的错误是:java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor
上网查了下,这可能是spring3.0特有的问题,2.5就没有!出错的原因是少了个包,解决的方案是去
下载地址:
加到路径里,就行了!
spring3.0比2.5需要好多额外的包,除了上面的aopalliance外,还可能会需要asm和cglib!
进行测试,再次抛出异常:
问题3:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlServerDao' defined in
file [E:\dormitory\Dormitory\WebRoot\WEB-INF\classes\com\zzc\dao\impl\SqlServerDaoImpl.class]: BeanPostProcessor before
instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Aspect
Caused by: java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Aspect
加入aspectjrt.jar和aspectjweaver.jar,问题解决
问题4: Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class
[com.mchange.v2.c3p0.ComboPooledDataSource] for bean with name 'sqlServerDataSource' defined in class path resource
[applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: com.mchange.v2.c3p0.ComboPooledDataSource
Caused by: java.lang.ClassNotFoundException: com.mchange.v2.c3p0.ComboPooledDataSource
一看知道,少了c3p0的jar,因为我使用了c3p0的数据源
- <bean id="mysqlDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
- <property name="driverClass" value="org.gjt.mm.mysql.Driver"/>
- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dormitory?
- seUnicode=true&characterEncoding=UTF-8"/>
- <property name="user" value="root"/>
- <property name="password" value="123456"/>
- <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
- <property name="initialPoolSize" value="3"/>
- <!-- 连接池中保留的最小连接数。 -->
- <property name="minPoolSize" value="1"/>
- <!-- 连接池中保留的最大连接数。Default: 15 -->
- <property name="maxPoolSize" value="300"/>
- <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
- <property name="maxIdleTime" value="60"/>
- <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
- <property name="acquireIncrement" value="5"/>
- <!-- 每60秒检查所有连接池中的空闲连接。Default: 0 -->
- <property name="idleConnectionTestPeriod" value="60"/>
- </bean>
加入c3p0-0.9.1.2.jar,问题解决