Tuesday, April 26, 2011

Spring's dynamic proxies for AOP

Till spring 2.0, spring's implementation is called as proxy-based AOP implementation irrespective of which AOP library you are using. These proxies essentially wrap a targ et object (eg. BankAccount instance) in order to apply aspects (SecurityManager calls) before and after delegation of the target object.
These proxies appear as the class of the target object to any client, making the proxies simple drop-in replacements anywhere the original target is used.

So before using AOP, code used to look like this:

But after using proxy pattern the code will look like this:

Since the client would be unaware that he is taling to the different class, the Proxy generated by Spring would either be sublcass of BankAccount or implement interfaces exposed by BankAccount class so that client class remains transparent to the changes in the configuration.
Client invokes methods exposed by the proxy, which in turn will execute interceptors configured for target bean.

Client will never know that he is calling the method of a proxy class. Proxy classes are wrappers around our actual components and contain same signature methods taking it 100% transparent to the system.

In spring, proxy classes are used in many places apart from AOP as well.

See how to configure the weaving method and proxy method of AOP in spring.

No comments:

Post a Comment