SpringAOP的几种实现方式总结

AOP核心概念

阿克陶ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

1、横切关注点

对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点

2、切面(aspect)

类是对物体特征的抽象,切面就是对横切关注点的抽象

3、连接点(joinpoint)

被拦截到的点,因为spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器

4、切入点(pointcut)

对连接点进行拦截的定义

5、通知(advice)

所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类

6、目标对象

代理的目标对象

7、织入(weave)

将切面应用到目标对象并导致代理对象创建的过程

8、引入(introduction)

在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段

Spring 实现AOP所需要的包:

1、Spring提供的jar包

2、aopalliance.jar

3、aspectjweaver.jar

Spring 实现AOP的方式:

1、Java动态代理

该方法针对接口的实例创建代理

applicationContext.xml的配置如下:

<?xml version="1.0" encoding="UTF-8"?> 
 
     
     
  
     
     
     
       
         
         
         
       
     
 

其中Abstration为接口,ConcreteImplementor为实现类,InterceptorHandler为代理拦截类。

public interface Abstration { 
  public void operation() 
} 
//具体实现化角色 
public class ConcreteImplementor implements Implementor{ 
 
  @Override 
  public void operation() {   
    System.out.println("ConcreteImplementor"); 
  } 
 
} 
public class InterceptorHandler{  
  public void printTime(){ 
    System.out.println("CurrentTime = " + System.currentTimeMillis()); 
  } 
} 

2、CGLIB生成代理

CGLIB针对代理对象为类的情况使用。

通过实现MethodInterceptor接口,并实现 public Object intercept(Object obj, Method m, Object[] args,MethodProxy proxy) throws Throwable方法生成代理。

3、BeanNameAutoProxyCreator实现AOP

Spring为我们提供了自动代理机制,让容器为我们自动生成代理,把我们从烦琐的配置工作中解放出来,在内部,Spring 使用BeanPostProcessor自动地完成这项工作。

具体配置如下: 

  
  
    
      
      *Service  
      
    
    
      
      MyInterceptor  
      
    
  

其中*Service 为需要拦截代理的bean,以Service结尾的都 被拦截,并使用MyInterceptor 进行拦截,可配置多个拦截器,按顺序执行。

import java.lang.reflect.Method; 
import org.aopalliance.intercept.MethodInterceptor; 
import org.aopalliance.intercept.MethodInvocation; 
/** 
 * @author 
 * 
 */  
public class MyInterceptor implements MethodInterceptor{  
  
  @Override  
  public Object invoke(MethodInvocation invocation) throws Throwable {  
      
    Method method = invocation.getMethod();//获取被拦截的方法  
    Object[] arguments = invocation.getArguments();//获取拦截方法的参数  
    /* 
     * 特殊,某些权限需要做特殊处理 
     * 比如用户信息权限,在方法执行完毕返回的时候,要将电话号码与邮箱抹除 
     */  
    //环绕通知前置特殊处理  
    this.beforeReslove();  
    Object proceed = invocation.proceed();//调用目标方法  
    //环绕通知后置特殊处理  
    proceed = this.afterReslove();  
    return proceed;  
  } 
   private Object afterReslove() { 
      System.out.println("CurrentTime = " + System.currentTimeMillis()); 
     return null; 
   } 
   private void beforeReslove() { 
      System.out.println("CurrentTime = " + System.currentTimeMillis()); 
   }   
} 

4、使用注解AspectJ实现AOP

ApplicationContext.xml 加入

 

 创建切面处理类

package com.marving.aop; 
import java.util.Arrays; 
import org.aspectj.lang.ProceedingJoinPoint; 
import org.aspectj.lang.annotation.Around; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Pointcut; 
import org.springframework.stereotype.Component;  
@Aspect 
@Component  
public class AspectHandler { 
   
  @Pointcut("execution(* com.marving.service.BaseServ+.*(..))") 
  private void doMethod() { 
  } 
 
    /** 
   * This is the method which I would like to execute before a selected method 
   * execution. 
   */ 
  @Before("doMethod()") 
  public void beforeAdvice() { 
    System.out.println("before method invoked."); 
  } 
 
  /** 
   * This is the method which I would like to execute after a selected method 
   * execution. 
   */ 
  @After("doMethod()") 
  public void afterAdvice() { 
    System.out.println("after method invoked."); 
  } 
 
  // 配置controller环绕通知,使用在方法aspect()上注册的切入点 
  @Around("doMethod()") 
  public Object around(ProceedingJoinPoint pjp) throws Throwable{ 
    Object result = null; 
    String methodName = pjp.getSignature().getName(); 
    try { 
      System.out.println("The method [" + methodName + "] begins with " + Arrays.asList(pjp.getArgs())); 
      result = pjp.proceed(); 
    } catch (Throwable e) { 
      System.out.println("The method [" + methodName + "] occurs expection : " + e); 
      throw new RuntimeException(e); 
    } 
    System.out.println("The method [" + methodName + "] ends"); 
    return result; 
  } 
} 

通过表达式execution(* com.marving.service.BaseServ+.*(..)) 匹配切入点函数,并使用@Before@After@Around 对所拦截方法执行前、中、后进行拦截并执行处理函数。

@Around @Before @After三个注解的区别@Before是在所拦截方法执行之前执行一段逻辑。@After 是在所拦截方法执行之后执行一段逻辑。@Around是可以同时在所拦截方法的前后执行一段逻辑。

值得注意的是,Around在拦截方法后,需要返回一个方法执行结果,否则,原方法不能正常执行。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


当前标题:SpringAOP的几种实现方式总结
分享URL:http://pwwzsj.com/article/peeese.html