gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:25:41 28 4
gpt4 key购买 nike

我是 Spring 的菜鸟,我正在使用 Gary Mak 的《Spring Recipes: A Problem Solution Method》一书来学习 Spring。

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

我想知道为什么会发生这种情况。谁能给我解释一下吗?

最佳答案

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

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

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

另请参阅:

关于java - 如果有 @Repository 或 PersistenceExceptionTranslationPostProcessor bean,则创建 bean 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4572852/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com