..


Sponsored Links

AOP - Proxies

Before moving to see how you create with their aspect pointcuts and advice, it is very important to understand how the proxies.
A proxy is nothing more than a wrapper (container) that contains an object and exposes all public medodi object. When you invoke a method on the proxy the actual implementation of the method is delegated to the object Wrap, thus making the presence of the proxy entirely transparent to the user.
The proxy uses Spring AOP to implement than the simple delegate methods also deal with various management advice.

We understand how does a proxy implement one very simple. First we create a simple interface:






 {public interface SimpleInterface



	

  



 public void f1 ();



	

  



 public void f2 ();







 }



we create even a simple implementation:





 SimplePojo {public class implements SimpleInterface





  



 @ Override



  



 public void f1 () {



    



 System.out.println ("F1");



    



 f2 ();



  



 }





  



 @ Override



  



 public void f2 () {



    



 System.out.println ("F2");



  



 }







 }



We note that the method f1 () makes a call to f2 ().
Let's now create the proxy, as already said we need to create a container of an object and expose all of its methods, then the proxy will implement the interface and then delegate SimpleInterface all method calls to the real object:






 SimpleProxy {public class implements SimpleInterface





  



 Private SimpleInterface delegated;





  



 public SimpleProxy (SimpleInterface delegate) {



    



 super ();



    



 this.delegate = delegate;



  



 }





  



 @ Override



  



 public void f1 () {



    



 System.out.println ("Delegating F1 ()");



    



 delegate.f1 ();



  



 }





  



 @ Override



  



 public void f2 () {



    



 System.out.println ("Delegating F2 ()");



    



 delegate.f2 ();



  



 }







 }



In our simple example, the proxy does is print a message which says that is delegating the call.

We create now a main test to see how our proxy:






 public class Main {





  



 public static void main (String [] args) {



    



 / / Create an instance of the bean

 

    



 SimpleInterface realBean SimplePojo = new ();





    



 System.out.println ("##### bean #####");



    



 realBean.f1 ();



    



 System.out.println ();



    



 realBean.f2 ();





    



 System.out.println ();





    



 / / Create an instance of the proxy



    



 SimpleInterface SimpleProxy proxy = new (new SimplePojo ());





    



 System.out.println ("##### proxy #####");



    



 proxy.f1 ();



    



 System.out.println ();



    



 proxy.f2 ();



  



 }







 }



The first block of code gives the following output:





 ##### Bean #####







 F1







 F2









 F2



Now one would expect that the output of the second block is the following:





 ##### ##### Proxy







 Delegating F1 ()







 F1







 Delegating F2 ()







 F2









 Delegating F2 ()







 F2



Invce not! The output will be as follows:





 ##### ##### Proxy







 Delegating F1 ()







 F1







 F2









 Delegating F2 ()







 F2



As we can see the call that the method f1 () is the method f2 () does not pass through the proxy, because once you have delegated the execution of the method to the real object all internal calls will not be intercepted by proxy.
For this reason you can not create advice on methods that are called from other methods of the object, or rather can be created only if these methods are used by external objects, because otherwise the advice is not executed.
Proxy

Spring Java Guide
E-Learning
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
PHP (Course) PHP (Course)
Full course for creating dynamic Web sites. From 49 €.
Ruby and Ruby on Rails (Course) Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €.
Sponsored Links