使用@Autowired的@Transaction注释 - Spring

时间:2013-10-27 22:22:30

标签: java spring

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

我的班级标题是:

@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。

编辑1:

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

<!-- 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,看看:

<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

2 个答案:

答案 0 :(得分:6)

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

  1. 您尚未声明TransactionManager,因此无法应用@Transactional建议。
  2. 您的@Autowired目标被声明为CaixaBOImpl类型的字段,并且您的类路径中没有CGLIB个库,以便Spring创建类代理。因此,它创建了JDK Proxies,它无法注入,因为类型不匹配。 You can get the CGLIB libraries here。或者,您可以将@Autowired目标字段更改为BasicBOImpl。如果您在应用程序中注入BasicBOImpl类型的其他bean而没有使用id限定它们,则会出现问题。
  3. 似乎即使您拥有CGLIB代理,Spring仍然使用JDK代理。将您的tx:annotation-driven声明更改为

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

答案 1 :(得分:0)

我们遇到了类似的问题,我们使用了似乎正常工作的“黑客”。只需让您的类实现一些接口即可。 它对我们有用 - 让我知道它是否适合你。