拦截 aspect ,interceptor

(1)

aop 是spring 切面编程,

@aspect 

在spring-servlet.xml 中配置aop

 <beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

 <aop:aspectj-autoproxy proxy-target-class="true" />
  <bean class="com.aop.XXXX" />

 2.1 定义Pointcut

     1.  package com.sarkuya.aop.aspect;
    2.  import org.aspectj.lang.annotation.Aspect;
    3.  import org.aspectj.lang.annotation.Pointcut;
    4.  @Aspect
    5.  public class SampleAspect {
    6.      @Pointcut("execution(* com.sarkuya.service..*.*(..))")
    7.      public void inServiceLayer() {
    8.      }
    9.  }

    Pointcut是植入Advice的触发条件。每个Pointcut的定义包括2部分,一是表达式,如第6行;二是方法签名,如第7行。方法签名必须是 public及void型。可以将Pointcut中的方法看作是一个被Advice引用的助记符,因为表达式不直观,因此我们可以通过方法签名的方式为 此表达式命名。因此Pointcut中的方法只需要方法签名,而不需要在方法体内编写实际代码。(转)

 

领一种环绕就是@around

public Object arount(proceedingJoinPoint p){

String name=p.getSignature.indexOf().getName();

//登陆请求时不拦截

if(name.indexOf('login')!=-1)

//获取session

 

return p.proceed

}

 

(2)(转)

Interceptor 是spring mvc框架的

 

  1.  /** 
  2.      * preHandle方法是进行处理器拦截用的,顾名思义,该方法将在Controller处理之前进行调用,SpringMVC中的Interceptor拦截器是链式的,可以同时存在 
  3.      * 多个Interceptor,然后SpringMVC会根据声明的前后顺序一个接一个的执行,而且所有的Interceptor中的preHandle方法都会在 
  4.      * Controller方法调用之前调用。SpringMVC的这种Interceptor链式结构也是可以进行中断的,这种中断方式是令preHandle的返 
  5.      * 回值为false,当preHandle的返回值为false的时候整个请求就结束了。 
  6.      */  
  7.     @Override  
  8.     public boolean preHandle(HttpServletRequest request,  
  9.             HttpServletResponse response, Object handler) throws Exception {  
  10.         // TODO Auto-generated method stub  
  11.         return false;  
  12.     }  
  13.       
  14.     /** 
  15.      * 这个方法只会在当前这个Interceptor的preHandle方法返回值为true的时候才会执行。postHandle是进行处理器拦截用的,它的执行时间是在处理器进行处理之 
  16.      * 后,也就是在Controller的方法调用之后执行,但是它会在DispatcherServlet进行视图的渲染之前执行,也就是说在这个方法中你可以对ModelAndView进行操 
  17.      * 作。这个方法的链式结构跟正常访问的方向是相反的,也就是说先声明的Interceptor拦截器该方法反而会后调用,这跟Struts2里面的拦截器的执行过程有点像, 
  18.      * 只是Struts2里面的intercept方法中要手动的调用ActionInvocation的invoke方法,Struts2中调用ActionInvocation的invoke方法就是调用下一个Interceptor 
  19.      * 或者是调用action,然后要在Interceptor之前调用的内容都写在调用invoke之前,要在Interceptor之后调用的内容都写在调用invoke方法之后。 
  20.      */  
  21.     @Override  
  22.     public void postHandle(HttpServletRequest request,  
  23.             HttpServletResponse response, Object handler,  
  24.             ModelAndView modelAndView) throws Exception {  
  25.         // TODO Auto-generated method stub  
  26.           
  27.     }  
  28.   
  29.     /** 
  30.      * 该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行。该方法将在整个请求完成之后,也就是DispatcherServlet渲染了视图执行, 
  31.      * 这个方法的主要作用是用于清理资源的,当然这个方法也只能在当前这个Interceptor的preHandle方法的返回值为true时才会执行。 
  32.      */  
  33.     @Override  
  34.     public void afterCompletion(HttpServletRequest request,  
  35.             HttpServletResponse response, Object handler, Exception ex)  
  36.     throws Exception {  
  37.         // TODO Auto-generated method stub  
  38.           
  39.     }  
  40.       
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  3.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  6.      http://www.springframework.org/schema/context  
  7.      http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  8.      http://www.springframework.org/schema/mvc  
  9.      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  

         这样在SpringMVC的配置文件中就可以使用mvc标签了,mvc标签中有一个mvc:interceptors是用于声明SpringMVC的拦截器的。

 

        (二)使用mvc:interceptors标签来声明需要加入到SpringMVC拦截器链中的拦截器

Xml代码   收藏代码
  1. <mvc:interceptors>  
  2.     <!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 -->  
  3.     <bean class="com.host.app.web.interceptor.AllInterceptor"/>  
  4.     <mvc:interceptor>  
  5.         <mvc:mapping path="/test/number.do"/>  
  6.         <!-- 定义在mvc:interceptor下面的表示是对特定的请求才进行拦截的 -->  
  7.         <bean class="com.host.app.web.interceptor.LoginInterceptor"/>  
  8.     </mvc:interceptor>  
  9. </mvc:interceptors>  

          由上面的示例可以看出可以利用mvc:interceptors标签声明一系列的拦截器,然后它们就可以形成一个拦截器链,拦截器的执行顺序是按声明的先后顺序执行的,先声明的拦截器中的preHandle方法会先执行,然而它的postHandle方法和afterCompletion方法却会后执行。

          在mvc:interceptors标签下声明interceptor主要有两种方式:

                    (1)直接定义一个Interceptor实现类的bean对象。使用这种方式声明的Interceptor拦截器将会对所有的请求进行拦截。

                    (2)使用mvc:interceptor标签进行声明。使用这种方式进行声明的Interceptor可以通过mvc:mapping子标签来定义需要进行拦截的请求路径。

          经过上述两步之后,定义的拦截器就会发生作用对特定的请求进行拦截了。

猜你喜欢

转载自guozhijie87.iteye.com/blog/2305315