2017-04-05 91 views
0

我具有兩個服務種類中的&乙事務彈簧DATA JPA

Class A{ 

function a1() 
{ 
a2() 
} 

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

function a3(){ 
    B.b1() 
} 

} 

從控制器I調用函數A1(),用於服務A,由A1()我調用函數A 2(),其具有的事務註釋,然後調用a3()然後調用b1()服務B.我的事務是否工作?我的意思是在a2()中的所有工作都將處於事務處理或不處理狀態?

編輯

從評論,我想自我調用,但它不工作, 我已經試過這

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

    @Autowired 
    private ApplicationContext applicationContext; 

    private Service self; 

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

但它給錯誤

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 

我也試過這個

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

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

} 

這是給這個錯誤

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

通知的成員方法不會根本沒有額外的配置獲得交易http://stackoverflow.com/questions/3423972/spring-transaction-method-call-by-the-method-within-the-same-class-does-not-wo – WildDev

+0

所以我應該將a1移動到不同的班級嗎?另外,如果我將a1移動到差異類別,那麼a3將獲得交易或不是?還有什麼是分配額外配置 –

+0

是的好方法。或者,您可以使用'@ Resource'註釋來使用自注入,或者使用'@ PostConstruct'來設置它自己。但是,第一種選擇看起來更好。 http://stackoverflow.com/questions/5152686/self-injection-with-spring – WildDev

回答

1

沒有也不會因爲它是一個內部方法調用。 Spring使用代理,因此在這種情況下@Transactional幾乎沒用。

如果這種情況下

Class A{ 

function a1(){ 
a2() 
} 

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

class B{ 
function a3(){ 
} 
} 

答案是yes.By默認@Transactional已要求的傳播,所以A3()將在交易.spring DOC transaction propagation

+0

不會,因爲它是內部方法調用。 Spring使用代理,因此在這種情況下'@ Transactional'幾乎沒用。 –

+0

感謝您的提醒 –