2017-04-05 2 views
0

Ich habe zwei Serviceklasse A & BTransaktion Spring Data JPA

Class A{ 

function a1() 
{ 
a2() 
} 

**@Transactional** 
funciton a2(){ 
a3() 
} 

function a3(){ 
    B.b1() 
} 

} 

Aus-Controller rufe ich die Funktion a1() für Service-A, von a1() ich die Funktion a2() aufrufen, welches Transaktions Annotation, die dann a3() aufruft, die dann b1() für Service B aufruft. Funktioniert meine Transaktion? Ich meine die ganze Arbeit in a2(), wird in Transaktion sein oder nicht?

Edited

Aus den Kommentaren, ich versuche zu Selbst Aufruf aber es funktioniert nicht, Ich habe schon versucht, diese

@Service(value="TableService") 
public class TableService implements Service { 

    @Autowired 
    private ApplicationContext applicationContext; 

    private Service self; 

    @PostConstruct 
    private void init() { 
     self = applicationContext.getBean(TableService.class); 
    } 
} 

Aber es gibt Fehler

at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.3.3.RELEASE.jar:1.3.3.RELEASE] 
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'TableService' must be of type [com.example.service.TableService], but was actually of type [com.sun.proxy.$Proxy110] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:378) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:220) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:351) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1066) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at com.example.service.TableService.init(TableService.java:26) ~[classes/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_91] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_91] 
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_91] 
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:354) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:305) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    ... 23 common frames omitted 

Ich habe es auch versucht

@Service(value="TableService") 
public class TableService implements Service { 

    @Resource(name="TableService") 
    private Service self; 

} 

Es wird unter denen diesem Fehler

at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.3.3.RELEASE.jar:1.3.3.RELEASE] 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.service.TableService com.example.web.DemoController.tableService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.service.TableService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    ... 22 common frames omitted 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.service.TableService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    ... 24 common frames omitted 
+1

Mitteilung die Mitglied Methoden nicht Erhalten Sie die Transaktion überhaupt ohne eine zusätzliche Konfiguration http://stackoverflow.com/questions/3423972/spring-transaction-method-call-by-the-method-within-the-same-class-does-not-wo – WildDev

+0

So Soll ich a1 in eine andere Klasse verschieben? Auch wenn ich a1 in die diff-Klasse verschiebe, wird a3 eine Transaktion erhalten oder nicht? Auch, was ist die gute Art der Zuweisung zusätzlicher Konfiguration –

+0

ja du musst. Alternativ können Sie die Selbstinjektion mit '@ Resource' Annotation verwenden oder diese selbst mit' @ PostConstruct' einrichten. Die erste Option sieht jedoch besser aus. http://stackoverflow.com/questions/5152686/self-injection-with-spring – WildDev

Antwort

1

Nein, es wird nicht, wie es ein interner Methodenaufruf ist. Spring verwendet Proxies und als solches ist das @Transactional in diesem Fall ziemlich nutzlos.

Wenn dieses Szenario

Class A{ 

function a1(){ 
a2() 
} 

**@Transactional** 
function a2(){ 
B.a3() 
} 

class B{ 
function a3(){ 
} 
} 

Die Antwort ist yes.By Standard @Transactional eine Ausbreitung von ERFORDERLICH hat, so a3() in der Transaktion wird .spring doc transaction propagation

+0

Nein, es wird nicht als interner Methodenaufruf verwendet. Spring verwendet Proxies und daher ist das '@ Transactional' in diesem Fall ziemlich nutzlos. –

+0

Danke für Ihre Erinnerung –