【问题标题】:Error creating bean if have @Repository or a PersistenceExceptionTranslationPostProcessor bean如果有 @Repository 或 PersistenceExceptionTranslationPostProcessor bean,则创建 bean 时出错
【发布时间】:2011-06-02 03:39:13
【问题描述】:

我是春天的菜鸟,我正在使用 Gary Mak 的《Spring Recipes: A question solution approach》一书学习春天。

无论如何我在使用时遇到了这个问题 PersistenceExceptionTranslationPostProcessor。

这是我的 applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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-2.5.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

  <context:property-placeholder location="jdbc.properties"/>

  <tx:annotation-driven proxy-target-class="true" />

  <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName"
                  value="${jdbc.driverClassName}" />       
      <property name="url"
                  value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml" /> -->
 <property name="dataSource" ref="dataSource" />
 <property name="mappingResources">
   <list>
    <value>com/hibernateproj2/domain/Employee.hbm.xml</value>
   </list>
 </property> 
 <property name="hibernateProperties">
    <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
 </property>
</bean> 

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>


<bean id="employeeDaoImpl" class="com.hibernateproj2.dao.EmployeeDaoImpl">
   <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="hibWorker" class="HibWorker" >
  <property name="employeeDaoImpl" ref="employeeDaoImpl" />
</bean>

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

</beans>

.......

@Repository
    public class EmployeeDaoImpl implements EmployeeDao{
           ...........
    }


    public class HibWorker implements Worker{
    .......................

     @Transactional
    public void doTrans(){
       ......................
       }
       ...............

    }

    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

在这种情况下,我在加载 applicationContext.xml 时遇到以下错误

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext .xml");

   log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibWorker' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy5 implementing com.hibernateproj2.dao.EmployeeDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.hibernateproj2.dao.EmployeeDaoImpl] for property 'employeeDaoImpl'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy5 implementing com.hibernateproj2.dao.EmployeeDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.hibernateproj2.dao.EmployeeDaoImpl] for property 'employeeDaoImpl': no matching editors or conversion strategy found
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
 at java.security.AccessController.doPrivileged(Native Method)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
 at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
 at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
 at Main.main(Main.java:10)
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy5 implementing com.hibernateproj2.dao.EmployeeDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.hibernateproj2.dao.EmployeeDaoImpl] for property 'employeeDaoImpl'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy5 implementing com.hibernateproj2.dao.EmployeeDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.hibernateproj2.dao.EmployeeDaoImpl] for property 'employeeDaoImpl': no matching editors or conversion strategy found
 at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:391)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1288)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1249)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
 ... 14 more
Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy5 implementing com.hibernateproj2.dao.EmployeeDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.hibernateproj2.dao.EmployeeDaoImpl] for property 'employeeDaoImpl': no matching editors or conversion strategy found
 at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:219)
 at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)
 at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:386)
 ... 18 more

但是当我注释掉 EmployeeDaoImpl 上的 @Repository 注释或者注释掉 PersistenceExceptionTranslationPostProcessor bean 声明时,applicationContext 会正确加载。

    <!-- <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />  -->

or

//@Repository
public class EmployeeDaoImpl implements EmployeeDao{
       ...........
}

我想知道为什么会这样。谁能给我解释一下。

【问题讨论】:

  • @Repository 类实际上没有实现interface 时会发生此错误 - 但我假设是这种情况......并且存在proxy-target-class="true"。请在/如果您找到解决方案时发布解决方案。

标签: java spring


【解决方案1】:

HibWorker.employeeDaoImpl 的类型为EmployeeDaoImpl,因此无法为其分配HibernateDao 类型的异常翻译代理。

通常以这种方式限制依赖项的实现是一种不好的做法,您应该在声明依赖项时使用接口 (EmployeeDao)。如果你按照这种方式,你也不需要proxy-target-class = "true"&lt;tx:annotation-driven&gt;

另一种解决方案是为PersistenceExceptionTranslationPostProcessor 启用目标类代理。

另请参阅:

【讨论】:

  • 那么既然HibernateDao类型的异常翻译代理不能分配给它,那么HibernateDao类型的代理怎么办呢?春天有没有叫HibernateDao的接口?当我用谷歌搜索它时,我找不到它。您说“替代解决方案是为 PersistenceExceptionTranslationPostProcessor 启用目标类代理”。该怎么做?
  • @A_Var:固定,我的意思是EmployeeDao
  • 您说“替代解决方案是为 PersistenceExceptionTranslationPostProcessor 启用目标类代理”。怎么做?请解释一下。
猜你喜欢
  • 2017-06-05
  • 2021-09-02
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
  • 2016-06-24
  • 2017-08-20
  • 2013-10-06
  • 2015-06-29
相关资源
最近更新 更多