我今天早上试图将spring与hibernate整合在一起。我想使用spring transaction manager。但得到以下错误。该错误与@Trasactional Annotation有关。如果我删除注释,我就能从弹簧容器中获取bean。
Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy19 cannot be cast to com.hibernate.dao.EvntDAOImpl
at com.hibernate.action.HibernateAction.main(HibernateAction.java:17)
我粘贴在源代码下面。
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hibernate-tutorials</groupId>
<artifactId>hibernate-tutorials</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<spring-framework.version>4.0.3.RELEASE</spring-framework.version>
<hibernate.version>4.3.5.Final</hibernate.version>
<slf4j.version>1.7.5</slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Spring ORM support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.2.0</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
休眠配置
<?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd&#34;&gt;
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.hibernate.model.Evnt</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernatey.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
EvntDAOImpl.java
package com.hibernate.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.hibernate.model.Evnt;
@Repository(value="evntDAO")
@Transactional
public class EvntDAOImpl implements EvntDAO {
@Autowired
private SessionFactory sessionFactory;
@Override
public void storeEvnt(Evnt evnt) {
sessionFactory.getCurrentSession().save(evnt);
}
}
HibernateAction.java
package com.hibernate.action;
import java.util.Date;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hibernate.dao.EvntDAO;
import com.hibernate.dao.EvntDAOImpl;
import com.hibernate.model.Evnt;
public class HibernateAction {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
EvntDAOImpl evntDAO = (EvntDAOImpl) context.getBean("evntDAO");
Evnt evnt = new Evnt();
evnt.setTitle("first Event");
evnt.setDate(new Date());
evntDAO.storeEvnt(evnt);
}
}
提前致谢...
答案 0 :(得分:5)
问题出在您注射EventDaoImpl
的地方。
只需替换
@Autowired EventDaoImpl eventDaoImpl
与
@Autowired EventDao eventDao
无论你需要Spring来自动修复dao。
或者,如果您从applicationContext获取bean,请使用:
EvntDAO evntDAO = (EvntDAO) context.getBean("evntDAO");
问题是由于在dao的实现代码中使用@Transactional
注释意味着Spring将为其创建JDK dynamic proxy,这不能转换为实现类。 Here是Spring的AOP功能的完整文档(完全解释了JDK动态代理和CGLib类代理的创建)。
这实际上意味着因为@Transactional
,当你致电context.getBean("evntDAO")
时,你不会回到你的EventDaoImpl
(正如人们所期望的那样),但实际上你得到了返回一个由Spring创建的类java.lang.reflect.Proxy
的对象。该代理对象实现EventDao
(因此可以强制转换),但它的签名与EventDaoImpl
无关(因此它ClassCastException
,因为它不会扩展EventDaoImpl
{1}})。当在代理上调用方法时,在调用实际委托给EventDaoImpl
之前和/或之后发生了各种事情(在实际调用EventDaoImpl
之前/之后或之后发生的事情是由Spring控制的,尽管InvocationHandler
)的实施。