2016-02-23 34 views
0

我正在編寫基於spring-boot的應用程序。在我的代碼中,我有一個管理員類UManager,其中有一個方法保存。在這個方法中,有兩個動作參與其中。第一個使用另一個管理器保存一些數據,第二個將保存的數據保存到保存方法中。現在,當兩項行動中的一項可能失敗時,兩項行動都應該撤消。我假設,使用@Transactional註解將提供我需要的東西,就好像兩個操作之一都失敗了一樣,整個事務將被回滾並且不會對數據庫提交更改。如何讓@Component類中的方法爲@Transactional?

@Component 
public class UManager { 

    @Autowired 
    AManager aManager; 

    @Autowired 
    URepository uRepository; 

    ... some stuff happening here 

    @Transactional 
    public U save(UBuilder uBuilder) { 
      aManager.save(something); 
      uRepository.save(uBuilder.build()); 
    } 
} 

只要我將@Transactional註釋添加到save方法,應用程序就無法啓動。我收到以下錯誤:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bController': Injection of autowired dependencies failed; 

我在做什麼錯誤或錯過了什麼?

編輯: 這是堆棧跟蹤:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: UManager BController.uManager; nested exception is java.lang.IllegalArgumentException: Can not set UManager field BController.uManager to com.sun.proxy.$Proxy102 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] 
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:764) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] 
at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:357) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1124) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1113) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] 
at RApplication.main(RApplication.java:44) [classes/:na] 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_72] 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_72] 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_72] 
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_72] 
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:467) [spring-boot-maven-plugin-1.3.1.RELEASE.jar:1.3.1.RELEASE] 
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_72] 

編輯2: 的RApplication:

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.scheduling.annotation.EnableScheduling; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 

@SpringBootApplication 
@EnableScheduling 
@EnableTransactionManagement 
public class RApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(RaumbuchungApplication.class, args); //this is the line mentioned in the stacktrace 
    } 
} 

的UManager:

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 
import org.springframework.transaction.annotation.Transactional; 

import de.mischok.hfmw.raumbuchung.data.URepository; 
import de.mischok.hfmw.raumbuchung.types.U; 
import de.mischok.hfmw.raumbuchung.types.UBuilder; 

@Component 
public class UManager { 

    @Autowired 
    URepository uRepository; 

    @Transactional 
    public U save(UBuilder uBuilder){ 

    } 
} 

的BController:

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import UManager; 

@Controller 
@RequestMapping("my/Path") 
public class BController { 

    @Autowired 
    UManager uManager; 
} 

這些是代碼的重要組成部分,因爲其他部分不涉及問題。

+0

你有更多的錯誤日誌跟蹤來向我們展示嗎? – pleft

+0

這真的是你的代碼還是你的服務/回購也實現接口? –

回答

2

如果你不使用XML Spring配置,但你有一個Application類(我想從您發佈的堆棧跟蹤RApplication)來完成配置確保與註釋@EnableTransactionManagement

如:

@Configuration 
@ComponentScan 
@EnableAutoConfiguration 
@EnableJpaRepositories 
@EnableTransactionManagement 
public class Application { 
    //rest code goes here 
} 

編輯 錯誤顯示代理對象失敗,您應該自動裝入您要注入的類(UManager)的接口而不是類本身。

例如

更改UManager類實現一個接口(當然申報UManager任何公開的方法的界面太)

public class UManager implements IUManager 

,並在您bController類自動連線接口而不是類

public class BController { 
    @Autowired 
    IUManager uManager; 
} 
+0

感謝您的快速回復。我在''RApplication'類中添加了'@ EnableTransactionManagement'註釋,但唯一不同於之前的是'RApplication'中發生錯誤的行(我把它們整合到44到46之間) 。還有別的,你會建議嘗試嗎? – daniel

+0

你能否更新你的問題,包括'RApplication','UManager'和'BController'的完整代碼? – pleft

+0

@elefasGR他正在使用Spring引導,並已啓用您嘗試手動啓用(再次)的所有內容。所以這不會解決手頭的問題。 –

0

你必須首先創建一個事務管理器象下面這樣:

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="dataSource"/> 
</bean> 

然後使通過指定這一點,註解驅動:

<tx:annotation-driven transaction-manager = "txManager" /> 

現在,你應該能夠使用@Transactional

+0

感謝您的快速回復。但是我怎麼能沒有'beans.xml'呢?我正在使用彈簧引導,因此我沒有一個。 – daniel

相關問題