【责任链模式】模拟Struts责任链工作流及简单AOP实现

详细代码:http://download.csdn.net/detail/tanxiang21/5402599模拟Struts2的工作责任链过程,接口调用invoke方法执行public interface ActionInvocation{ Object getAction(); boolean isExecuted(); Result getResult()
摘要由CSDN通过智能技术生成

详细代码:http://download.csdn.net/detail/tanxiang21/5402599

模拟Struts2的工作责任链过程,接口调用invoke方法执行

public interface ActionInvocation{

    Object getAction();
    boolean isExecuted();
    Result getResult() throws Exception;
    String getResultCode();
    void setResultCode(String resultCode);
    String invoke() throws Exception;
    String invokeActionOnly() throws Exception;
}
接口实现类,这里具体的流程执行内容,由外部传入,不同于Struts2才有容器注入方式
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;

public class DefaultActionInvocation implements ActionInvocation {
	protected Iterator<InterceptorMapping> interceptors;
	protected Action action = new SelfAction();

	@SuppressWarnings("rawtypes")
	private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
	private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];

	protected Result result;
	protected String resultCode;

	protected boolean executed = false;

	//这里我们外部传入内容,Struts采用inject注入以及调用init方法初始化内容
	public DefaultActionInvocation(List<InterceptorMapping> interceptorsTree,
			Action action) {
		this.interceptors = interceptorsTree.iterator();
		this.action = action;
	}

	public String invoke() throws Exception {

		if (executed) {
			throw new IllegalStateException("Action has already executed");
		}

		//遍历拦截器
		if (interceptors.hasNext()) {
			final InterceptorMapping interceptor = interceptors.next();
			resultCode = interceptor.getInterceptor().intercept(
					DefaultActionInvocation.this);
		} 
		//执行Action
		else{
			resultCode = invokeActionOnl
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值