20

In Spring you can't simply call @Transactional method from the same instance, because of AOP-proxy thing. Would it be nice idea to make a self injection and call that method from self proxy instance? Do you see any drawbacks?

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
gs_vlad
  • 1,409
  • 4
  • 15
  • 29
  • 2
    There are cases when it is most elegant solution available , I have used it couple of times. Spring 4.3 introduced new feature to autowire self, IMHO spring support proves it is not a bad practice. – Vipin Apr 07 '17 at 14:44
  • Maybe you can also use an exposed proxy instance via `` or `@EnableAspectJAutoProxy(exposeProxy = true)`, see [SPR-10454](https://jira.spring.io/browse/SPR-10454) and [JavaDoc](http://docs.spring.io/spring/docs/current/javadoc-api//org/springframework/context/annotation/EnableAspectJAutoProxy.html#exposeProxy--). – kriegaex Apr 07 '17 at 19:27

1 Answers1

19

It is totally ok. Moreover there was a Jira ticket for supporting this feature using @Autowired annotations. It's fixed in Spring 4.3+ versions. However for xml-based configuration or using @Resource annotation it's working in the earlier versions.

You can see the discussion bellow this ticket. @Transactional is one of the use case for this:

Particularly interested in @Async and @Transactional use cases.

Sergii Bishyr
  • 8,331
  • 6
  • 40
  • 69