Interceptor Demon

Posted watchfluture

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Interceptor Demon相关的知识,希望对你有一定的参考价值。

 

package cn.itcast.chain.action;

public class TestAction {
public String execute(){
System.out.println("test action");
return "aaa";
}
}

----------------------------------------------

package cn.itcast.chain.interceptor;

import cn.itcast.chain.invocation.ActionInvocation;
/**
* 拦截器的总的接口
* @author zd
*
*/
public interface Interceptor {
String intercept(ActionInvocation invocation);
}

 

------------------------------------

package cn.itcast.chain.interceptor.impl;

import cn.itcast.chain.interceptor.Interceptor;
import cn.itcast.chain.invocation.ActionInvocation;

public class Test1Interceptor implements Interceptor{

public String intercept(ActionInvocation invocation) {
System.out.println("1-start");
String result = invocation.invoke();
System.out.println("1-end");
return result;
}

}

----------------------------------

public class Test2Interceptor implements Interceptor{

public String intercept(ActionInvocation invocation) {
System.out.println("2-start");
String result = invocation.invoke();
System.out.println("2-end");
return result;
}

}

-------------------------------------------

package cn.itcast.chain.invocation;

public interface ActionInvocation {
String invoke();
}

----------------------------

 

package cn.itcast.chain.invocation.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cn.itcast.chain.action.TestAction;
import cn.itcast.chain.interceptor.Interceptor;
import cn.itcast.chain.interceptor.impl.Test1Interceptor;
import cn.itcast.chain.interceptor.impl.Test2Interceptor;
import cn.itcast.chain.interceptor.impl.Test3Interceptor;
import cn.itcast.chain.invocation.ActionInvocation;

public class DefaultActionInvocation implements ActionInvocation{
/**
* 声明了所有的拦截器的迭代器的形式
*/
private Iterator<Interceptor> interceptors;
private String resultCode = null;
public DefaultActionInvocation(){
/**
* 初始化拦截器
*/
init();
}

public void init(){
/**
* 创建了一个拦截器的集合
*/
List<Interceptor> inter = new ArrayList<Interceptor>();
/**
* 创建了三个具体的拦截器
*/
Test1Interceptor interceptor1 = new Test1Interceptor();
inter.add(interceptor1);
Test2Interceptor interceptor2 = new Test2Interceptor();
inter.add(interceptor2);
Test3Interceptor interceptor3 = new Test3Interceptor();
inter.add(interceptor3);
this.interceptors = inter.iterator();
}

public String invoke() {
if(interceptors.hasNext()){//判断是否有下一个拦截器
/**
* 如果有,先返回下一个拦截器
*/
Interceptor interceptor = interceptors.next();
resultCode = interceptor.intercept(DefaultActionInvocation.this);
}else{
resultCode = executeAction();
}
return resultCode;
}

private String executeAction(){
return new TestAction().execute();
}
}

 

 

---------------------------------

package cn.itcast.chain.test;

import java.util.HashMap;

import org.junit.Test;

import cn.itcast.chain.invocation.impl.DefaultActionInvocation;

public class TestAction {
@Test
public void testAction(){
DefaultActionInvocation invocation = new DefaultActionInvocation();
System.out.println(invocation.invoke());
}

@Test
public void testMap(){
int i=1;
Integer b=1;
if(i==b){
System.out.println("true");

}






}


}






































































































以上是关于Interceptor Demon的主要内容,如果未能解决你的问题,请参考以下文章

过滤器(Filter)与拦截器(Interceptor )区别

如何启动postman interceptor

Interceptor--拦截器

SpringMVC中使用Interceptor拦截器

SpringMVC中使用Interceptor拦截器

CXF对Interceptor拦截器的支持