In the last tutorial we did successfully create a working interceptor, but there is more to it. To successfully develop bug free interceptors we need to know more.

In this tutorial will try to understand a crucial part of interceptor. And what is that?  The answer is Life cycles of interceptors.


The interceptors has the following life cycle.

  1. Interceptor Object creation
  2. initialization
  3. intercept
  4. destroy

We will demonstrate the life cycle of the of the interceptor with the following interceptor.

package com.bullraider.apps.interceptors;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class InterceptorLifeCycle implements Interceptor{
    public InterceptorLifeCycle(){
        System.out.println(InterceptorLifeCycle.class.getSimpleName()+" Object Created");
    }
    public void destroy() {
        System.out.println(InterceptorLifeCycle.class.getSimpleName()+" Object Destroyed");
    }
    public void init() {
        System.out.println(InterceptorLifeCycle.class.getSimpleName()+" Initialized");
    }
    public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println(InterceptorLifeCycle.class.getSimpleName()+" Serving ");
        return invocation.invoke();
    }
}

 

So what we did to the InterceptorLifeCycle interceptor is, we  provided the necessary behaviors; constructor, init, destroy life cycle methods and most importantly the intercept method with proper SOPs. And, also notice that InterceptorLifeCycle interceptor implements interceptor interface. But in the last tutorial we extended an abstract class AbstractInterceptor.

Then what is the difference between AbstractInterceptor and Interceptor ?