Struts2 Interceptor Life Cycle

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.


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 ?

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();
    }
}
  

  

  The AbstractInterceptor actually implements the Interceptor interface and provides default implementation ( blank method definition) of the init() and destroy() life cycle methods, which gives the developer the flexibility to extend this class without providing the default implementation of these two methods as opposed to Interceptor interface. So, AbstractInterceptor is  mostly used where the init and destroy life cycle is not needed. However it doesn't mean that we can't use AbstractInterceptor when we need life cycle implementation. We still can override the life cycle methods from inside AbstractInterceptor. The Interceptor interface can be implemented directly while the life cycle methods are needed.


    Here is my struts.xml file
 

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <package name="test" extends="struts-default" >
   <interceptors>
    <interceptor name="lifecycle"
                class="com.bullraider.apps.interceptors.InterceptorLifeCycle" />
   </interceptors>
   <action name="Test" class="com.bullraider.apps.actions.TestAction" >
    <interceptor-ref name="lifecycle" />
      <result name="success">/success.jsp</result>
   </action>
 </package>
</struts>

  

Not much different from my last tutorials struts.xml file. What it explain is that  TestAction is tied with the lifecycle interceptor.

And my action class is not much different

public class TestAction {
    public String execute()
    {
        System.out.println("Inside Action");
        return "success";
    }
}

  

  Download the interceptorlifecylce.war  and run in the container.


As you might have noticed already, the life cycles are are very similar to the Servlet filters.

The constructor and the init() method called only once when the container or the application is starting( when the StrutsPrepareAndExecuteFilter initialized to be precise, from web.xml)
and the intercept() method is called only when it is referred in the action(action execution). And the destroy method called only once, when the container or application is stopped or un-deployed.

But now the big question is when do we need these two life cycle methods init() and destroy() ?

For some reason you might need any resources like a database connection or EJB bean for any matter. Then init() method is the place where you can do the initialization. And definitely you don’t want to keep the resources open when the application is not running , destroy() is the place to do the cleanup activities.

In the next tutorial we will cover how to pass information to interceptor and how to control method execution from within struts.xml

原文地址:https://www.cnblogs.com/hephec/p/4588293.html