ITPub博客

首页 > 架构设计 > 软件结构 > Spring AOP

Spring AOP

软件结构 作者:gung123 时间:2020-02-18 13:42:41 0 删除 编辑

AOP中的概念

Aspect(切面):指横切性关注点的抽象即为切面,它与类相似,只是两者的关注点不一样,类是对物体特征的抽象,而切面是横切性关注点的抽象(包括切入点的描述和通知的描述)。

Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,
因为spring只支持方法型的连接点,实际上joinpoint还可以是field或者构造器。

Pointcut(切入点):所谓切入点是指我们要对那些joinpoint进行拦截的定义。

Advice(通知):所谓通知是指拦截到jointpoint之后所要做的事情就是通知。通知分为前置通知、后置通知、异常通知、最终通知、环绕通知。
了解springcloud架构可以加求求:三五三六二四七二五九

Target(目标对象):代理的目标对象

Weave(织入): 指将aspects应用到target对象并导致proxy对象创建的过程称为织入

Introducton(引入):在不修改类代码的前提下,Introduction可以在运行期为类动态地添加一些方法或Field

Spring提供了两种切面使用方式,实际工作中我们可以选用其中一种 :

1 基于xml配置方式进行AOP开发
2 基于注解方式进行AOP开发  

(一)基于注解的方式

下面是基于注解的方式

Java代码

<?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:context=
"http://www.springframework.org/schema/context"   
       xmlns:aop=
"http://www.springframework.org/schema/aop"        
       xsi:schemaLocation="http:
//
           http:
//
           http:
//www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
           http:
//www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
        <aop:aspectj-autoproxy/><!-- 启动对
@AspectJ注解的支持 -->  
</beans>

Java代码

import org.aspectj.lang.ProceedingJoinPoint;  
import org.aspectj.lang.annotation.After;  
import org.aspectj.lang.annotation.AfterReturning;  
import org.aspectj.lang.annotation.AfterThrowing;  
import org.aspectj.lang.annotation.Around;  
import org.aspectj.lang.annotation.Aspect;  
import org.aspectj.lang.annotation.Before;  
import org.aspectj.lang.annotation.Pointcut;  
import org.springframework.stereotype.Component;  
  
@Aspect @Component  
public class MyInterceptor {  
  
/** 
     *@Pointcut :表示规定切入点  
     *execution() 语法规范 
     * 第一个“*”表示任意返回结果类型 
     * “cn.itcast.service.impl.PersonServiceBean”:表示对此类进行拦截, 
     * 如果是cn.itcast.service..*.*:表示对包cn.itcast.service以及子包里所 
有的类的所有方法进行拦截, 
     * (..)表示参数  
     */   
  
      
    
@Pointcut("execution(* com.mingbai.springaop.PersonServiceBean.*(..))")  
    
private void anyMethod(){}//声明一个切入点  
      
/*  @Before("anyMethod()") 
    public void doAccessCheck(){ 
        System.out.println("前置通知"); 
    }*/  
      
    
//此时的前置通知,只能拦截到参数个数和类型匹配的方法  
    
//args(name)中的name必须和方法doAccessCheck的参数一至  
    
@Before("anyMethod() && args(name)")  
    
public void doAccessCheck(String name){  
        System.out.println(name+
"前置通知");  
    }  
      
/*  @AfterReturning("anyMethod()") 
    public void doAfterReturn(){ 
        System.out.println("后置通知"); 
    }*/  
    
//得到方法的返回值  
    
@AfterReturning(pointcut="anyMethod()",returning="result")  
    
public void doAfterReturn(String result){  
        System.out.println(
"后置通知  "+result);  
    }  
      
  
    
@After("anyMethod()")  
    
public void doAfter(){  
        System.out.println(
"最终通知");  
    }  
      
/*  @AfterThrowing("anyMethod()") 
    public void doAfterThrow(){ 
        System.out.println("异常通知"); 
    }*/  
    
@AfterThrowing(pointcut="anyMethod()",throwing="e")  
    
public void doAfterThrow(Exception e){  
        System.out.println(
"异常通知------"+e.getMessage());  
    }  
      
    
@Around("anyMethod()")  
    
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{  
        System.out.println(
"环绕通知  开始");  
        Object obj = pjp.proceed();  
        System.out.println(
"环绕通知  结束");  
        
return obj;  
    }  
}

(二)基于xml配置文件的

切面只是一个普通的javabean

Java代码 

  1. import org.aspectj.lang.ProceedingJoinPoint;  
  2.   
  3. public class MyInterceptor1 {  
  4.       
  5.   
  6.      public void doAccessCheck(){  
  7.         System.out.println( "前置通知-------");  
  8.     }  
  9.       
  10.      public void doAfterReturn(){  
  11.         System.out.println( "后置通知");  
  12.     }  
  13.       
  14.   
  15.      public void doAfter(){  
  16.         System.out.println( "最终通知");  
  17.     }  
  18.      public void doAfterThrow(){  
  19.         System.out.println( "异常通知");  
  20.     }  
  21.       
  22.      public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{  
  23.         System.out.println( "环绕通知  开始");  
  24.         Object obj = pjp.proceed();  
  25.         System.out.println( "环绕通知  结束");  
  26.          return obj;  
  27.     }  
  28. }  


配置文件 :

Java代码 

<?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:context=
"http://www.springframework.org/schema/context"   
       xmlns:aop=
"http://www.springframework.org/schema/aop"  
       xmlns:tx=
"http://www.springframework.org/schema/tx"  
       xsi:schemaLocation="http:
//
           http:
//
           http:
//www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
           http:
//www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
           http:
//www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">     
       
   
[color=brown]     <bean id=
"per" class="com.mingbai.springaop.PersonServiceBean"/>  
     <bean id=
"myInterceptor" class="com.mingbai.springaop.MyInterceptor1"/>  
     <!--    
     <aop:config>  
        <aop:aspect id=
"asp" ref="myInterceptor">  
            <aop:pointcut id=
"mycut" expression="execution(* com.mingbai.springaop.*.*(..))"/>  
            <aop:before pointcut-ref=
"mycut" method="doAccessCheck"/>  
            <aop:after-returning pointcut-ref=
"mycut" method="doAfterReturn"/>  
            <aop:after pointcut-ref=
"mycut" method="doAfter"/>  
            <aop:after-throwing pointcut-ref=
"mycut" method="doAfterThrow"/>  
            <aop:around pointcut-ref=
"mycut" method="doBasicProfiling"/>  
        </aop:aspect>  
     </aop:config>[/color]  
     -->   
     <!-- 只是拦截返回类型为java.lang.String的方法     
     <aop:config>  
        <aop:aspect id=
"asp" ref="myInterceptor">  
            <aop:pointcut id=
"mycut" expression="execution(java.lang.String com.mingbai.springaop.*.*(..))"/>  
            <aop:before pointcut-ref=
"mycut" method="doAccessCheck"/>  
            <aop:after-returning pointcut-ref=
"mycut" method="doAfterReturn"/>  
            <aop:after pointcut-ref=
"mycut" method="doAfter"/>  
            <aop:after-throwing pointcut-ref=
"mycut" method="doAfterThrow"/>  
            <aop:around pointcut-ref=
"mycut" method="doBasicProfiling"/>  
        </aop:aspect>  
     </aop:config>  
   -->   
   <!-- 返回非
void的方法 -->  
   <aop:config>  
        <aop:aspect id=
"asp" ref="myInterceptor">  
            <aop:pointcut id=
"mycut" expression="execution(!void com.mingbai.springaop.*.*(..))"/>  
            <aop:before pointcut-ref=
"mycut" method="doAccessCheck"/>  
            <aop:after-returning pointcut-ref=
"mycut" method="doAfterReturn"/>  
            <aop:after pointcut-ref=
"mycut" method="doAfter"/>  
            <aop:after-throwing pointcut-ref=
"mycut" method="doAfterThrow"/>  
            <aop:around pointcut-ref=
"mycut" method="doBasicProfiling"/>  
        </aop:aspect>  
     </aop:config>  
   <!-- 匹配第一个参数为java.lang.String,其它的无所谓   
     <aop:config>  
        <aop:aspect id=
"asp" ref="myInterceptor">  
            <aop:pointcut id=
"mycut" expression="execution(* com.mingbai.springaop.*.*(..))"/>  
            <aop:before pointcut-ref=
"mycut" method="doAccessCheck"/>  
            <aop:after-returning pointcut-ref=
"mycut" method="doAfterReturn"/>  
            <aop:after pointcut-ref=
"mycut" method="doAfter"/>  
            <aop:after-throwing pointcut-ref=
"mycut" method="doAfterThrow"/>  
            <aop:around pointcut-ref=
"mycut" method="doBasicProfiling"/>  
        </aop:aspect>  
     </aop:config>  
   -->  
     
</beans>

转载于: https://www.cnblogs.com/junjiang3/p/9033038.html

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69952307/viewspace-2676042/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-11-01

  • 博文量
    200
  • 访问量
    79622