当前位置: 移动技术网 > IT编程>开发语言>Java > Spring AOP的几种实现方式总结

Spring AOP的几种实现方式总结

2019年07月22日  | 移动技术网IT编程  | 我要评论
aop核心概念 1、横切关注点 对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点 2、切面(aspect) 类是对物体特征的抽象,切面就是对横切关注点

aop核心概念

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"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
  xmlns:aop="http://www.springframework.org/schema/aop" 
  xmlns:tx="http://www.springframework.org/schema/tx" 
  xsi:schemalocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> 
     
    <bean id="concreteimplementor" class="com.marving.aop.concreteimplementor" /> 
  
    <bean id="interceptorhandler" class="com.marving.aop.interceptorhandler" /> 
     
    <aop:config> 
      <aop:aspect id="interceptor" ref="interceptorhandler"> 
        <aop:pointcut id="addallmethod" expression="execution(* com.marving.aop.abstration.*(..))" /> 
        <aop:before method="dosomething" pointcut-ref="addallmethod" /> 
        <aop:after method="dosomething" pointcut-ref="addallmethod" /> 
      </aop:aspect> 
    </aop:config> 
</beans> 

其中abstration为接口,concreteimplementor为实现类,interceptorhandler为代理拦截类。

public interface <span style="font-size:12px;">abstration</span> { 
  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自动地完成这项工作。

具体配置如下: 

<bean id="myinterceptor" class="com.yesjpt.interceptor. myinterceptor"></bean>  
<bean  
  class="org.springframework.aop.framework.autoproxy.beannameautoproxycreator">  
  <property name="beannames">  
    <list>  
      <value>*service</value>  
    </list>  
  </property>  
  <property name="interceptornames">  
    <list>  
      <value>myinterceptor</value>  
    </list>  
  </property>  
 </bean> 

其中*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 加入

<aop:aspectj-autoproxy/> 

 创建切面处理类

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在拦截方法后,需要返回一个方法执行结果,否则,原方法不能正常执行。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网