Guice AOP works only on instances created by guice. You create new
FirstServiceImpl(secondServiceImpl) manually and so guice can't apply
transactional on it.
So it's "by design" behaviour.
Try to avoid doing guice job: let it create all instances for you.
If you have a complex case and bean class to instantiate is known only at
runtime, you can still use injector.getInstance(KnownJustNow.class) to ask
guice to construct instance for you (with aop).
суббота, 21 ноября 2015 г., 10:07:39 UTC+6 пользователь Kohei Nozaki
написал:
>
> Hello,
>
> @Transactional annotation doesn't work for an instance provided by a
> method annotated as @Provides. My situation is as follows. I have two
> classes in the reproducer. A class named FirstServiceImpl which is depends
> on SecondServiceImpl. These classes have following methods:
>
> // Method in FirstServiceImpl
> @Transactional
> public void firstSave(final MyEntity myEntity) {
> secondServiceImpl.secondSave(myEntity);
> }
>
> // Method in SecondServiceImpl
> public void secondSave(final MyEntity myEntity) {
> em.persist(myEntity);
> }
>
> And FirstService is produced by a method annotated as @Provided:
>
> // Method in MyModule.
> @Provides
> FirstService provideFirstService(final SecondServiceImpl
> secondServiceImpl) {
> return new FirstServiceImpl(secondServiceImpl);
> }
>
> In that situation, I expect transaction will begin on
> FirstServiceImpl#firstSave() both of following two cases:
>
> (a) Acquire an instance by calling injector.getInstance(FirstService.class)
> (b) Acquire an instance by calling
> injector.getInstance(FirstServiceImpl.class)
>
> But it doesn't work for (a). It looks like that @Transactional doesn't
> affect for an instance produced by @Provided method. I found a workaround
> that putting @Transactional to SecondServiceImpl#secondSave() make it work,
> But unfortunately I can't make it for real code at my job.
>
> I've pushed the reproducer to GitHub:
> https://github.com/lbtc-xxx/guice-persist-tx
>
> Is this behavior by design? Or are there any better workaround?
>
> Thanks.
>
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-guice/8e8fdeea-703c-48fd-be7e-5f11dc2cf158%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.