Following are task done by the Proxy Object when we calls the business logic on the proxy object.

1.  First proxy checks whether the Business logic( methods ) to be invoked is available with the target class object or proxy interface specified by the developer in the Spring Configuration while configuring the proxy for the target class. 

2. If it found either with the target class or the proxy interface, collects the data configured in the List of InterceptorName wile configuring the ProxyFactoryBean in the XML document.

Note : List of InterceptorName contains list of Advice class object. 

3. Proxy checks for the list of Advice class object in the List of InterceptorName, to invoke the methods defined in the Advice class by getting its Object form the list.  


Note: Normally 3 method are invoked sequentially i.e,
a) before()             --> First called defined in one of the Advice class.
b) afterReturning() --> Called after before() defined in another class.
c) afterThrowing    --> Called only when exception thrown by the . 


4. All the methods are called sequentially by their respective Target Objects , present in the Spring Container. 

Note : Respective methods will be called on the basis of Advice class implementing following Interfaces. 

a) MethodBeforeAdvice  ( has before() )
b) AfterReturningAdvice  ( has afterReturning() )
c) ThrowAdvice              ( has afterThrowing() )


5. If the Business Logic executes and returns control successfully then proxy checks for any Interceptor(Advice) which has to be applied after executing successfully. 

6. If not executed successfully that means exception will be thrown and then afterThrowing() will be executed. 


7. Proxy checks for the Advice class implementing ThrowAdvice interface to class the overridden afterThrowing() method. 

Important points 

1. before () method is present in the MethodBeforeAdvice interface , so need to override in our Advice class and always will be called before executing any business logic. 

2. afterReturning() method is present in the AfterReturningAdvice interface, so need to override in our Advice class and always will be executed if business logic executes successfully. 

3. afterThrowing() method is present in the ThrowAdvice interface, so need to override in our Advice class and always will be executed only if business logic fails to executes and throws exception.  
This is the very first technique to implement Spring AOP using Spring API. Later they introduces Annotation and XML schema concept which itself uses Spring API but with Annotation/ XML schema approach. 

Using ProxyFatoryBean we are approaching or implementing AOP in our service layers to our business operations to wrap business operations with the middle level services.
In this approach we are creating the proxies either of the target class or the interfaces. 


Creating the proxy of the interface based Services classes are called as Interface based Proxying where as, Creating the proxy of the object of the Services classes are called as Class based Proxying

So basically we are writing our services class in with ways :-
1. Class model
2. Interface model.

Class Model : In this model we are simply creating a service class having different business logic with its implementation. And we are creating proxy of this service class using ProxyFactoryService.

For ex:

Class AccountService{         // Business Service
     
    public void addAccount(){ // Business Logic
        .....
    }
    public void desposit(){   // Business Logic
        .....
    }
}


Interface Model : In this approach we are creating a service class which implements an interface having defines different business logic and the class implementing this interface have the implementation of the business logic defined in its Interface.

For ex:

Interface 
interface AccountService{   
     
    public void deposit();  //Business Logic
  
 }

Business Service class
Class AccountServiceImpl implements AccountService{ // Business Service

    public void desposit(){   //Business Logic
        .....
    }

Steps to be followed for implementing AOP using Spring API

1. Consider the business services written above.
2. Configure these Business services in the Spring Configuration document. For ex -

<bean id="asTarget" class="......AccountService" /> (class based)
<bean id="asTarget1" class="......AccountServiceImpl" /> (Interface based)

Note : From above both the bean configuration we have to use any of the approach either class model or interface model but recommendable is to use Interface model. 

3. Consider the required middle level services and write its services class. For ex-
I m considering three middle level service classes.
a) TxService.class ( Transaction Service class ) 
b) SecurityService.class (Security Service class )
c) LogService.class ( Logging Service class )

4. Configure the above bean in the Spring Configuration file. 

<bean id="tx" class="......TxService" /> 
<bean id="sc" class="......SecurityService" />
<bean id="lg" class="......logService" /> 

Note : The above middle level services are applied at the joinPoints of the Business operations. So for applying joinPoints we need to write some Advice class, as follows

5. Basically we have three joinPoints and grammatically all these 3 can be used with the predefined methods.
a) Method Before JoinPoint implemented using before(...) method.
b) Method Returning joinPoint can be implemented using afterReturning(...) method.
c) Method Throeing joinPoints can be implemented using afterThrowing(...) method. 

6. Write the required advcies as per the joinPoints. 

MBAdvice class 
Class MBAdvice implements methodBeforeAdvice{ //Advice class

    public void before(){  

      // Verify User logic
      // log the Information
      // Begin Transaction  
    }
}


MRAdvice class 

Class MRAdvice implements AfterReturningAdvice{ // Advice class

    public void before(){  

      // Transaction Commit
      // Log the Information
    }
}

MTAdvice class 
Class MTAdvice implements ThrowAdvice{ // Advice class

    public void before(){  

      // Rollback Transaction
      // Log the Information 
    }
}

7. Configure the above advcies class in the Spring Configuration file.
<bean id="mba" class="......MBAdvice" /> 
<bean id="mra" class="......MRAdvice" />
<bean id="mta" class="......MTAdvice" /> 

8. Configure the ProxyFactoryBean for creating the proxy of the business service class. 

<bean id="baseProxy" class="org.springframework.aop.framework.ProxyFactoryBean"abstract="true"> 
 <property name="InterceptorsNames">
   <list>
      <value> mba </value>
      <value> mra </value>
      <value> mta </value>
   </list>
 </property>  
</bean>
 

Note:
1. Using abstract="true " means instance will not be created for the ProxyFactoryBean class.
2. Configuring Advice class bean instance using the property tag and storing in the list as because all the information which is applied to the business operations are present in Advice classes instance. So need to configure with the Proxy class.

9. Configure Proxy object for our business Services.

Configuration of Class based proxy 
<bean id="asProxy" parent="baseProxy" > 
 <property name="targetClass" value="...........AccountService" />  
  <property name="target" ref="asTarget" />  
</bean>

Configuration of Interface based proxy 
<bean id="asProxy1" parent="baseProxy" > 
 <property name="proxyInterface" value="...........AccountServiceImpl" />  
  <property name="target" ref="asTarget1" />  
</bean>

Note :
1. In both the approaching have to follow any of these and recommendation is to use Interface based proxy.
2. In the immediate configuration parent="baseProxy" means the proxy created for the configured classes will be the sub-class of ProxyFactoryBean.

Important Point :
1. By the above configuration Container create the proxy for the target object of the Business service classes configured in the document.
2. That proxy object will be wrapped with the different middle level services which is done by the Spring Container while creating proxy with the help of the Advice class configured in the ProxyFactoryBean bean configuration in the list.   

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic