Showing posts with label proxy. Show all posts
Showing posts with label proxy. Show all posts

Monday, 25 April 2011

Weaving style of working with AOP

 Earlier we saw proxy style of handling aop, now we will see weaving style. Spring 2.5 supported LTW (load time weaving).
Weaving means bytecode instrumentation.

Types of Weaving
Code weaving comes in three flavors:

  • Load-time weaving(LTW): Aspect weaving is performed by the class loader when classes are first loaded.
    Weave class files when being loaded in VM
    i.e. Add aspect to class files OR Aspect + class files
  • Compile-time weaving: Aspects are weaved into the class files when they are compiled.
    Aspect + source code = class files
  • Binary Weaving (linker)
    Aspect + source/byte code = class files


Aspectwerkz and AspectJ works with both compile and load-time weaving.

Compile time weaving and binary weaving requires a seperate AspectJ compiler, which has nothing to do with spring. You will have to seperate lean AspectJ or try it out. Eclipse plugins are also available for AspectJ making it easier to achieve the same.

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


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.