使用@Autowired的@Transaction注释 - Spring [英] Using @Transaction annotation with @Autowired - Spring

查看:75
本文介绍了使用@Autowired的@Transaction注释 - Spring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有一个带@Service注释的类和一些奇怪的发生器:当我在某些方法中放置注释@Transaction我无法启动tomcat服务器时,我得到错误:无法自动装配...在另一个我使用@Autowired时上课,但是当我删除@Transaction时一切正常。

Well, i have a Class with @Service annotation and some strange occurrs: When i put the annotation @Transaction in some method i can't start tomcat server, i got error: "Can not autowired..." in another class when i use my @Autowired, but when i remove the @Transaction all works fine.

我的班级标题是:

@Service(value = "caixaBO")
public class CaixaBOImpl extends BasicBOImpl {

我的方法签名是:

@Transactional(propagation = Propagation.REQUIRED)
public void movimentarCaixaPaciente(String descricao, double valor,
        Paciente paciente) {

当我使用@Autowired CaixaBOImpl caixaBO时,我在初始化tomcat时出错,因为注释@Transaction。

When i use @Autowired CaixaBOImpl caixaBO, i got error in tomcat initialized because the annotation @Transaction.

编辑1:

1 - 我在applicationContext.xml中有transactionManager,看:

1 - I have transactionManager in applicationContext.xml, look:

<!-- Configuracao do gerente de transacoes do Spring -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

2 - 我在pom.xml中有CGLIB,看:

2 - I have CGLIB in pom.xml, look:

<dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>2.2.2</version>
        </dependency>

编辑2

我的堆栈跟踪:

Grave: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pedidoProteseBO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private br.com.odontonew.financeiro.bo.CaixaBOImpl br.com.odontonew.odonto.bo.PedidoProteseBOImpl.caixaBO; nested exception is java.lang.IllegalArgumentException: Can not set br.com.odontonew.financeiro.bo.CaixaBOImpl field br.com.odontonew.odonto.bo.PedidoProteseBOImpl.caixaBO to com.sun.proxy.$Proxy36
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:282)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:204)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4701)
    at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5204)
    at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5199)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private br.com.odontonew.financeiro.bo.CaixaBOImpl br.com.odontonew.odonto.bo.PedidoProteseBOImpl.caixaBO; nested exception is java.lang.IllegalArgumentException: Can not set br.com.odontonew.financeiro.bo.CaixaBOImpl field br.com.odontonew.odonto.bo.PedidoProteseBOImpl.caixaBO to com.sun.proxy.$Proxy36
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    ... 20 more
Caused by: java.lang.IllegalArgumentException: Can not set br.com.odontonew.financeiro.bo.CaixaBOImpl field br.com.odontonew.odonto.bo.PedidoProteseBOImpl.caixaBO to com.sun.proxy.$Proxy36
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.lang.reflect.Field.set(Field.java:741)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:504)
    ... 22 more


推荐答案

我可以想到有两种情况可能导致这种情况

There are two situations I can think of that might cause this


  1. 您尚未声明 TransactionManager ,因此无法应用 @Transactional 建议。

  2. 您的 @Autowired 目标被声明为类型的字段CaixaBOImpl 并且您的类路径上没有 CGLIB 库,以便Spring创建类代理。因此,它会创建由于类型不匹配而无法注入的JDK代理。 您可以在此处获取 CGLIB 。或者,您可以更改 @Autowired 目标字段以键入 BasicBOImpl 。如果您在应用程序中注入类型为 BasicBOImpl 的其他bean而不用id来限定它们,这将是一个问题。

  1. You haven't declared a TransactionManager and therefore @Transactional advice cannot be applied.
  2. Your @Autowired target is declared as a field of type CaixaBOImpl and you don't have CGLIB libraries on your classpath for Spring to create class proxies. As such, it creates JDK Proxies which it fails to inject because the types don't match. You can get the CGLIB libraries here. Or you can change the @Autowired target field to type BasicBOImpl. This will be a problem if you are injecting other beans of type BasicBOImpl in your application without qualifying them with an id.

看起来即使你有CGLIB代理,Spring仍然使用JDK代理。将 tx:annotation-driven 声明更改为

It seems that even though you have the CGLIB proxies, Spring is using JDK proxies anyway. Change your tx:annotation-driven declaration to

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

这篇关于使用@Autowired的@Transaction注释 - Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆