如果有@Repository或PersistenceExceptionTranslationPostProcessor bean,则创建bean时出错

时间:2011-01-01 00:56:11

标签: java spring

我是春天的新手,我正在学习春季食谱:Gary Mak的问题解决方案。

无论如何我在使用时遇到了这个问题 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{
       ...........
}

我想知道为什么会这样。有人可以解释一下这个。

1 个答案:

答案 0 :(得分:2)

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

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

替代解决方案是为PersistenceExceptionTranslationPostProcessor启用目标类代理。

另见:

相关问题