Why doesn't Spring's @Transactional work on protected methods?

前端 未结 3 953
醉梦人生
醉梦人生 2020-12-17 01:48

From Does Spring @Transactional attribute work on a private method?

When using proxies, you should apply the @Transactional annotation only to metho

3条回答
  •  孤街浪徒
    2020-12-17 02:17

    Additional info to the other answers.

    Here is an example picture from the Spring blog:

    As you can see the proxy is wrapped around the implementing class (here the AccountServiceImpl) and the proxy itself implements only methods from the AccountService interface. An Interface provides only public methods (public contract), so a proxy can not be wrapped around the protected method and provide the transactional behavior, which it provides on public methods.

    Calling methods in the same service is possible, if you would use AspectJ, but I'm not sure if this also counts for protected methods, because I haven`t used it until now.

提交回复
热议问题