最近刚学spring,在事务管理这自己写了个小例子,可是老运行不通过,请大虾看看。下面是我的代码:
首先是DAO接口,UserDaoService:
package org.ultimania.dao;
import java.util.List;
import org.ultimania.model.User;
public interface UserDaoService {
public List getAll();
}
接口实现,UserDaoImpl:
package org.ultimania.dao;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.ultimania.model.User;
public class UserDaoImpl extends HibernateDaoSupport implements UserDaoService{
@SuppressWarnings("unchecked")
@Transactional(propagation=Propagation.REQUIRED)
public List getAll(){
List list = this.getHibernateTemplate().find("from User");
return list;
}
}
接下来是我的测试代码:
package org.ultimania.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.ultimania.dao.UserDaoImpl;
import org.ultimania.dao.UserDaoService;
import org.ultimania.model.User;
public class UserDaoTest {
public static void main(String[] args){
List<User> list = null;
UserDaoService userDaoImpl ;
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
userDaoImpl = (UserDaoService)ctx.getBean("userDao");
try{
list = userDaoImpl.getAll();
}catch(Exception e){
e.printStackTrace();
}finally{
if(list==null){
System.out.println("faled");
}else{
System.out.println(list.size());
}
}
}
}
然后是spring配置文件,applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="userDao" class="org.ultimania.dao.UserDaoImpl">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>
我用的是注解式事务管理的方式,hibernate的东西是没错的,现在的问题是如果我在UserDaoImpl不添加事务声明@Transactional(propagation=Propagation.REQUIRED),运行是没有错误的,可是我一添加这句声明,运行就错了。下面是异常信息:
Exception in thread "main"
java.lang.AbstractMethodError: org.springframework.orm.hibernate3.SpringTransactionFactory.beginTransaction(Lorg/hibernate/jdbc/JDBCContext;Lorg/hibernate/transaction/TransactionFactory$Context;)Lorg/hibernate/Transaction;
at org.hibernate.jdbc.JDBCContext.beginTransaction(JDBCContext.java:271)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1079)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:558)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:374)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:263)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:101)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy5.getAll(Unknown Source)
at org.ultimania.test.UserDaoTest.main(UserDaoTest.java:19)
这个问题困扰我好久了,一直没有解决,只能请大虾们帮我看看了。