手写拦截器

拦截器在框架中经常用到,最常见的SpringMVC对请求的路径进行拦截做权限过滤。Mybatis在执行sql之前提供拦截器,提供对执行sql进行修改,最常见的是分页。

 

拦截器也算设计模式中的一种。相信对设计模式很熟悉的人,写起来还是很简单的。那么如何手写拦截器:

 

package com.util.interceptor;

/**
 *
项目名称:lic-demo
  * @author chunl
 */

public interface Executor {

   
public void execute();

}

 

package com.util.interceptor;



import java.util.ArrayList;

import java.util.List;



/**

 * 项目名称:lic-demo

 * 

 * @author chunl

 */

public class Interceptor {





    private List interceptorList;



    public List getInterceptorList() {

        return interceptorList;

    }



    public Interceptor() {

        this.interceptorList = new ArrayList<>();

    }



    public void addInterceptor(List interceptorRegister) {

    }



    public void executedInterceptor() {

        for (Executor exec: interceptorList) {

            exec.execute();

        }

    }





}

 

 

package com.util.interceptor;



import java.util.List;



/**

 * 项目名称:lic-demo

 * @author chunl

 */

public class InteceptorImpl extends Interceptor {



    @Override

    public void addInterceptor(List interceptorRegister) {



        interceptorRegister.add(new Executor() {

            @Override

            public void execute() {

                System.out.println("执行拦截器Inteceptora");

            }

        });



        super.addInterceptor(interceptorRegister);

    }

}

 

 

 

 

测试类如下:

package com.util.interceptor;



import java.io.File;

import java.util.ArrayList;

import java.util.List;



/**

 * 项目名称:lic-demo

 *  @author chunl

 */

public class Test {



    public List> getClasses() throws ClassNotFoundException {



        // 获取当前类路径

        String path = this.getClass().getResource("").getPath();



        // 获取包名

        String packageName = this.getClass().getPackage().getName();





        File file = new File(path);

        File[] files = file.listFiles();



        List> list = new ArrayList<>();

        for (File f : files) {

            String className = f.getName();

            if (className.contains(".")) {

                int length = className.indexOf(".");

                className = packageName + "." + className.substring(0, length);

                Class classes = Class.forName(className);



                // 排除掉匿名内部类和接口

                if(!classes.isAnonymousClass() && !classes.isInterface()){

                    list.add(classes);

                }

            }

        }

        return list;

    }



    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {



        Test test = new Test();

        List> list = test.getClasses();

        Interceptor exceutor = new Interceptor();

        for (Class clazz : list) {

            if (clazz.newInstance() instanceof Interceptor) {

                Interceptor exceutor1 = (Interceptor) clazz.newInstance();

                exceutor1.addInterceptor(exceutor.getInterceptorList());

            }

        }

        exceutor.executedInterceptor();

    }



}

 

你可能感兴趣的:(手写拦截器)