简体   繁体   中英

Adding proxy to Spring managed beans

I am working on a web project and one of its requirements is to call some store procedures before each actions in the database (for init/debug/audit purposes). My service layer uses Spring to manage the transactions, meaning that they are clearly delimited.

My idea is to add a proxy class via annotation, to call these SP. I read a lot of blogs, articles on writing annotations, AOP, AspectJ, proxying and so on, but I don't understand how I can use it for my need.

One of my problems is how to ensure that my proxy will be called by the Spring proxy (after it started the transaction)? Another one is how to triggers the creation of my own proxies? Any precision will be welcome because so far, I understand the main idea, but I have no clue on how I can really do that.

I think you should start by reading a bit more about AOP and AspectJ, I can recommend you the "Aspect Oriented Spring" chapter of the well-known "Spring in Action" book. It will answer all your question.

How to ensure that my proxy will be called by the Spring proxy (after it started the transaction) ?

Well you don't do it yourself, Spring ensures that your advices are called according to the way you configured them.

How to triggers the creation of my own proxies ?

I'm not sure I understood the question, but proxies needed by Spring to perform AOP will be created by Spring so that you do not have to trigger any proxy creation yourself.

You can use Spring advice to call SP . Use advice ordering to run SP with in same transaction. <tx:annotation-driven/> can takes order attribute thru which you can control which proxy applied first.

from Spring Reference

Advice Ordering

The highest precedence advice runs first "on the way in" (so given two pieces of before advice, the one with highest precedence runs first). "On the way out" from a join point, the highest precedence advice runs last (so given two pieces of after advice, the one with the highest precedence will run second).

Finally, I successfully implemented it, I described how here

Hope it will help...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM