基于注解和xml配置的AspectJ操作

一、 AspectJ

AspectJ不是Spring组成部分,独立AOP框架,一般把AspectJ和Spring框架一起使用,进行AOP操作

1. 切入点表达式

作用:知道对那个类里面的那个方法进行增强

语法结构:

  • execution([权限修饰符][返回类型][类全路径][方法名称]([参数列表]))

举例一:对com.zyy.dao.ManDao类的add进行增强

execution(* int com.zyy.dao.ManDao.add(..))

  • 权限修饰符可省略不写,默认为public

  • *:代表任意返回类型

二、 AOP操作(AspectJ注解)

  1. 创建类,在类里面定义方法

    public class User {
        public void add(){
            System.out.println("add.........");
        }
    }
    
  2. 创建增强类(编写增强逻辑)

    • 在增强类里面,创建方法,让不同方法代表不同通知类型

      //增强的类
      public class UserProxy {
          //前置的通知
          public void before(){
              System.out.println("before....");
          }
      }
      
  3. 进行通知的配置

    1. 在spring配置文件中,开启注解扫描

      <?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:p="http://www.springframework.org/schema/p"
             xmlns:util="http://www.springframework.org/schema/util"
             xmlns:context="http://www.springframework.org/schema/context"
             xmlns:aop="http://www.springframework.org/schema/aop"
             xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                                 http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
                                 http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop.xsd">
      
          <context:component-scan base-package="com.zyy.spring_demo"></context:component-scan>
          
      </beans>
      
    2. 使用注解创建User和UserProxy

      @Component
      public class User{}
      
      @Component
      public class UserProxy{}
      
    3. 在增强类上添加注解@Aspect

      @Component
      @Aspect //生成代理对象
      public class UserProxy{}
      
    4. 在配置文件中开启生成代理对象

      <!--    开启aspectJ生成代理对象-->
          <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
      
  4. 配置不同类型的通知

     //前置的通知
        @Before(value = "execution(* com.zyy.spring_demo.testDemo.User.add(..))")
        public void before(){
            System.out.println("before....");
        }
    
        // 最终通知
        @After(value = "execution(* com.zyy.spring_demo.testDemo.User.add(..))")
        public void after(){
            System.out.println("after....");
        }
        // 后置通知
        @AfterReturning(value = "execution(* com.zyy.spring_demo.testDemo.User.add(..))")
        public void afterReturning(){
            System.out.println("AfterReturning....");
        }
        // 异常通知
        @AfterThrowing(value = "execution(* com.zyy.spring_demo.testDemo.User.add(..))")
        public void afterThrowing(){
            System.out.println("AfterThrowing....");
        }
    
        // 环绕通知
        @Around(value = "execution(* com.zyy.spring_demo.testDemo.User.add(..))")
        public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
            System.out.println("around before....");
            // 被增强的方法执行
            proceedingJoinPoint.proceed();
            System.out.println("around after....");
        }
    
  5. 相同切入点抽取

    @pointcut(value = "execution()")
    public void pointdemo(){
    
    }
    
    //通知只需调用pointdemo()方法即可
    // @Before(value="pointdemo()")
    
  6. 设置增强类优先级

    • 在增强类上添加注解@order(数字),数值越小,优先级越高。
  7. 完全注解开发

  • 只需创建配置类
@Configuration
@ComponentScan(basePackages = {"com.zyy"})
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class ConfigAop{
}

三、 AOP操作(xml配置文件)

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

  2. 在配置文件中创建两个类对象

    <bean id="book" class=""></bean>
    <bean id="bookProxy" class=""></bean>
    
  3. 在配置文件中设置切入点

     <!--配置aop增强-->
    <aop:config>
        <!--切入点-->
    	<aop:pointcut id="p" expression="execution()"/>
    	<!--配置切面-->
    	<aop:aspect ref="bookProxy">
            <!--增强到具体方法上-->
    		<aop:before method="before" pointcut-ref="p"/>
    	</aop:aspect>
    </aop:config>
    
posted @ 2021-11-12 17:30  HandsomeToDeath  阅读(33)  评论(0)    收藏  举报