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?

2
  • 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 7, 2017 at 14:44
  • Maybe you can also use an exposed proxy instance via <aop:aspectj-autoproxy expose-proxy="true"/> or @EnableAspectJAutoProxy(exposeProxy = true), see SPR-10454 and JavaDoc.
    – kriegaex
    Apr 7, 2017 at 19:27

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.