INTERFACES or TARGET_CLASS: Which proxyMode should I choose?

后端 未结 3 1381
小鲜肉
小鲜肉 2020-12-07 17:44

I am looking for a way to store my object and it seems that the best approach is to use proxies. I found 2 annotation in the internet, which one should I use :



        
3条回答
  •  清歌不尽
    2020-12-07 18:37

    While going through a blog post provided in the comments above, I found a comment stating cons of interface-based proxies.

    On the post, user Flemming Jønsson posted this:

    Be careful with using interface-based proxies.

    If you are using Spring Security or Spring Transactions you might experience oddities when using interface-based proxies.

    E.g. if you have a bean T and that bean has methods a() and b() that are both annotated transactional. Calls from other beans directly to a() or b() will behave properly (as configured). However if you introduce an internal call - where a() calls b() then b's transactional metadata will have no effect. The reason is that when you are using interface-based proxies the internal call will not go through the proxy - and thus the transactional interceptor will not have a chance to start a new transaction.

    The same goes for Security. If method a() only requires USER-role but calls b() that requires ADMIN-role, then the internal call from a to b will be performed for any USER with no warnings. Same reason as above, internal calls do not go through the proxy and thus the security interceptor does not have a chance to act upon the call to b() from a().

    To solve issues like these use targetClass.

提交回复
热议问题