JFinal学习--03Interceptor

JFinal中,Interceptor是其实现AOP的核心。

使用Interceptor有两种方式:

1.在JFinalConfig的实现类中配置,如:

public void configInterceptor(Interceptors me) {
    me.add(new DemoInterceptor());
}

其中DemoInterceptor继承了 com.jfinal.aop.Interceptor。

2.在Controller类或者Controller类的方法上,添加注解 @Before(DemoInterceptor.class),这里可以添加多个Interceptor。

JFinal会在项目启动的时候,将Interceptor缓存到内存中(通过单例模式)。

整个流程大致如下:

a.JFinalFilter的init方法中获取配置文件 JFinalConfig。
b.将配置文件中配置的全局拦截器、service拦截器存入 单例的InterceptorManager实例相应属性中。
c.获取Controller类,获取Controller类和其方法的@Before注解中定义的Interceptor类。并保存在InterceptorManager的singletonMap中。

另外,使用enhance或duang方法设置的Interceptor是通过动态代理获取的。在调用相应的方法时,会通过动态代理封装相应的类。

猜你喜欢

转载自blog.csdn.net/leoxyk/article/details/50790570
今日推荐