Spring的学习(六)AOP操作(基于AspectJ)

AOP操作(准备)

1.Spring框架一般都是基于AspectJ实现AOP操作
(1)什么是AspectJ
AspectJ不是Spring组成部分,独立AOP框架,一般把AspectJ和Spring框架一起使用,进行AOP操作.
2.基于AspectJ实现AOP
(1)基于xml配置文件实现
(2)基于注解方式

3.在项目工程目录引入AOP依赖

spring-aop-xxx.jar
spring-aspects-xxx.jar
com.springsource.net,sf.cglib-xxx.jar
com.springsource.org.aopalliance-xxx.jar
com.springsource.org.aspectj.weaver-xxx.jar

4.切入点表达式
(1)语法接口:
execution( [权限修饰符] [返回类型] [类全路径] [方法名称] ([参数列表]));

 //举例1:对com.abc.dao.BookDao类里面的 add() 进行增强
  execution(*com.abc.dao.BookDao.add(..));
  // * 表示所有,  .. 表示参数列表
  
 //举例2:对com.abc.dao.BookDao类里面的 所有方法 进行增强
  execution(*com.abc.dao.BookDao.*(..));
 
  //举例3:对com.abc.dao所有类里面的 所有方法 进行增强
  execution(*com.abc.dao.*.*(..));

AOP操作(AspectJ注解的方式)

下面是演示:

  • 一.现在有一个User类
//要被增强的类
 public class User{
   public void add(){}
 }
  • 二.现在创建一个增强类
 public class UserProxy{
   //前置通知
   public void before(){}

 }
  • 三.进行通知的配置

(1.1)在spring配置文件中,开启注解扫描
(以下有注解等等不懂的,具体可以看我 <IOC操作bean(基于注解)>的文章)

xmlns:context="http://www.springframework.org/shema/conext"
xmlns:aop="http://www.springframework.org/shema/aop"
<context:component-scan base-package="com.abc"></context:component-scan>

(1.2)使用配置类,开启注解扫描

@Configuration
@ComponentScan(basePackage="com.abc")
//开启生成代理对象,相当于下面配置文件中的<aop:aspectj-autoproxy>
@EnableAspectJAutoProxy(proxyTargetClass=true) 
public class ConfigAop{
}

(2)使用注解创建User和UserProxy对象
在两个类上面加上 @Component

//要被增强的类
 @Component
 public class User{
   public void add(){}
 }
 @Component
 public class UserProxy{
   //前置通知
   public void before(){}
 }

(3)在增强类上面添加注解@Aspect

 @Component
 @Aspect //生成代理对象
 public class UserProxy{
   //前置通知
   public void before(){}
 }

(4)在srping配置文件中开启生成代理对象

<!--开启生成代理对象 会给有@Aspect的生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  • 四.配置不同类型的通知

(1)在增强类里面,给作为通知方法的上面添加通知类型注解,使用切入点表达式配置.

 @Component
 @Aspect //生成代理对象
 public class UserProxy{
   //@before表达前置通知
   @Before(value="execution(*com.abc.entry.User.add(..))")
   public void before(){}
   
   //@After表达最终通知,方法执行后执行
   @After(value="execution(*com.abc.entry.User.add(..))")
   public void after(){}
   
   //@AfterReturning表达后置通知/返回通知,表达方法返回结果之后执行
   @AfterReturning(value="execution(*com.abc.entry.User.add(..))")
   public void afterReturning(){}

  //@AfterThrowing表达异常通知
   @AfterThrowing(value="execution(*com.abc.entry.User.add(..))")
   public void afterThrowing(){}

   //@Around表达环绕通知,之前之后都执行
   @Around(value="execution(*com.abc.entry.User.add(..))")
   public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
      //增强之前
      //....
      
      //执行被增强的方法
      proceedingJoinPoint.proceed();
   
      //增强之后
      //....

   }

 }
  • 五.公共切入点抽取
 @Component
 @Aspect //生成代理对象
 public class UserProxy{
   //因为切入点重复,所以可以抽取一个公共切入点
   @pointcut(value="execution(*com.abc.entry.User.add(..))")
   public void pointdemo(){
   }

   //@before表达前置通知
   @Before(value="pointdemo()")
   public void before(){}
   
   //@After表达最终通知,方法执行后执行
   @After(value="pointdemo()")
   public void after(){}
   
   //@AfterReturning表达后置通知/返回通知,表达方法返回结果之后执行
   @AfterReturning(value="pointdemo()")
   public void afterReturning(){}

  //@AfterThrowing表达异常通知
   @AfterThrowing(value="pointdemo()")
   public void afterThrowing(){}
 }
  • 六.有多个增强类对同一个方法进行增强,设置增强类优先级
    (1)在增强类上添加注解@Order(数字类型值),数字类型值越小优先级越高
 @Component
 @Aspect //生成代理对象
 @Order(1)
 public class UserProxy{
   //因为切入点重复,所以可以抽取一个公共切入点
   @pointcut(value="execution(*com.abc.entry.User.add(..))")
   public void pointdemo(){
   }
   }
 @Component
 @Aspect //生成代理对象
 @Order(3)
 public class PersonProxy{
   //因为切入点重复,所以可以抽取一个公共切入点
   @pointcut(value="execution(*com.abc.entry.User.add(..))")
   public void pointdemo(){
   }

UserProxy ( @Order(1) )的优先级就会比PersonProxy高 ( @Order(3) )

AOP操作(AspectJ配置文件的方式 不常用)

1.创建两个类,增强类和被增强类

//要被增强的类
 public class User{
   public void add(){}
 }
 public class UserProxy{
   //前置通知
   public void before(){}
 }

2.在spring配置文件中注入两个类

<bean id="user" class="com.abc.entry.User"></bean>
<bean id="userProxy" class="com.abc.proxy.UserProxy"></bean>

3.在spring配置文件中配置切入点

<!--配置aop增强-->
<aop:config>
   <!--切入点-->
  <aop:pointcut id="p" expression="execution(* com.abc.entry.User.add(..)" >
  <!--配置切面-->
  <aop:aspect ref="userProxy">
   <!--增强在具体方法上-->
   <aop:before method="before" pointcut-ref="p">
 </aop:aspect>

<aop:config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值