zoukankan      html  css  js  c++  java
  • Struts2中拦截器实现AOP的原理分析

    在Struts2中,使用了拦截器来实现AOP(面向切面编程)的功能,下面来模拟实现该功能:

    基于 Struts 2 拦截器实现细粒度的基于角色的存取控制:http://www.ibm.com/developerworks/cn/java/j-lo-struts2-rbac/

    图片网址:http://blog.csdn.net/yezi77321660/article/details/3960779

    接口类:Aops.java

    package com.testaop;
    
    import com.testaop.imp.TestAop;
    
    public interface Aops {
        
        public void before();
        
        public void intercept(TestAop testAop);
        
        public void after();
        
    }

    实现类:AopsImplA.java

    package com.testaop.imp;
    
    import com.testaop.Aops;
    
    public class AopsImplA implements Aops{
        
        @Override
        public void before() {
            System.out.println("执行了A的before");
        }
        
        @Override
        public void intercept(TestAop testAop) {
            before();
            testAop.invoke();
            after();
        }
        
        
        @Override
        public void after() {
            System.out.println("执行了A的after");
        }
        
        
    }

    AopsImplB.java

    package com.testaop.imp;
    
    import com.testaop.Aops;
    
    public class AopsImplB implements Aops{
        
        @Override
        public void before() {
            System.out.println("执行了B的before");
        }
        
        @Override
        public void intercept(TestAop testAop) {
            before();
            testAop.invoke();
            after();
        }
        
        
        @Override
        public void after() {
            System.out.println("执行了B的after");
        }
        
        
    }

    AopsImplC.java

    package com.testaop.imp;
    
    import com.testaop.Aops;
    
    public class AopsImplC implements Aops{
        
        @Override
        public void before() {
            System.out.println("执行了C的before");
        }
        
        @Override
        public void intercept(TestAop testAop) {
            before();
            testAop.invoke();
            after();
        }
        
        
        @Override
        public void after() {
            System.out.println("执行了C的after");
        }
        
        
    }

    测试类:TestAop.java,类似与ActionInvocation

    package com.testaop.imp;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    import com.testaop.Aops;
    
    public class TestAop {
    
        
        private Iterator<Aops> aopIterators;
        
        public void invoke(){
            
            if(aopIterators.hasNext()){
                Aops aopsImpl  = (Aops)aopIterators.next();
                aopsImpl.intercept(TestAop.this);
            }else{
                System.out.println("执行了action");
            }
            
            
        }
        
        public static void main(String[] args) {
            
            TestAop testAop = new TestAop();
            
            List<Aops> list = new ArrayList<Aops>();
            Aops aopsImplA  = new AopsImplA();
            Aops aopsImplB  = new AopsImplB();
            Aops aopsImplC  = new AopsImplC();
            list.add(aopsImplA);
            list.add(aopsImplB);
            list.add(aopsImplC);
            
            testAop.aopIterators = list.iterator();
            testAop.invoke();
        }
        
    }

    执行结果:

    执行了A的before
    执行了B的before
    执行了C的before
    执行了action
    执行了C的after
    执行了B的after
    执行了A的after

     实现了AOP的功能,其中执行了action使用了before和after的通知类型

     项目源码下载 testaop.rar

    I believe that we are who we choose to be. Nobody‘s going to come and save you, you‘ve got to save yourself. 我相信我们成为怎样的人是我们自己的选择。没有人会来拯救你,你必须要自己拯救自己。
  • 相关阅读:
    Linux 学习 step by step (1)
    ubuntu server nginx 安装与配置
    ubuntu server samba服务器配置
    iOS app集成支付宝支付流程及后台php订单签名处理
    mac 连接windows 共享内容
    linux 文件查找,which,whereis,locate,find
    ubuntu server vsftpd 虚拟用户及目录
    ubuntu server 安装 mantis bug tracker 中文配置
    ubuntu server vsftpd 匿名用户上传下载及目录设置
    linux 用户管理,用户权限管理,用户组管理
  • 原文地址:https://www.cnblogs.com/caroline/p/2936135.html
Copyright © 2011-2022 走看看