理解Spring AOP

示例来自 汪云飞的 《Spring boot 实战》, 重点是看 LogAspect.java 上面有我的注释.

@Before("annotationPointCut()")    // 4  这是使用@xxxxx的方式拦截,具体到这里就是所有标注为@Action 的方法都会被拦截

@Before("execution(*com.wisely.highlight_spring4.ch1.aop.DemoMethodService.*(..))") // 5 这是使用拦截规则被拦截,具体到这里就是指所有com.wisely.highlight_spring4.ch1.aop.DemoMethodService里面的方法都会被拦截

这两种貌似只能两者选其一


具体代码如下

com.wisely.highlight_spring4.ch1.aop.Main

 

1st.      @interface 

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface Action {

    String asd();

}

 

 

2nd.      @Aspect

package com.wisely.highlight_spring4.ch1.aop;

 

import java.lang.reflect.Method;

 

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

import org.aspectj.lang.reflect.MethodSignature;

import org.springframework.stereotype.Component;

 

@Aspect// 1

@Component// 2

public class LogAspect {

 

@Pointcut("@annotation(com.wisely.highlight_spring4.ch1.aop.Action)") // 3

   public void annotationPointCut() {

   };

 

// @Before("annotationPointCut()")    // 4  这是使用@xxxxx的方式拦截,具体到这里就是所有标注为@Action 的方法都会被拦截

   @Before("execution(*com.wisely.highlight_spring4.ch1.aop.DemoMethodService.*(..))") // 5 这是使用拦截规则被拦截,具体到这里就是指所有com.wisely.highlight_spring4.ch1.aop.DemoMethodService里面的方法都会被拦截

   public void before(JoinPoint joinPoint) {

      MethodSignaturesignature = (MethodSignature) joinPoint.getSignature();

      Methodmethod =signature.getMethod();

     

      Actionaction =method.getAnnotation(Action.class);

     

      // 如果使用 5 行的方式拦截,则action将会为null, action.asd()将会抛出异常 

      System.out.println("before方法规则式拦截被拦截的方法 ="

             + method.getName()

             + ", 标注中属性asd的值 = " + action.asd()

             + ", 被拦截的对象的包名 = " + signature.getDeclaringTypeName());

   }

  

// @After("annotationPointCut()") //6   这是使用@xxxxx的方式拦截,具体到这里就是所有标注为@Action 的方法都会被拦截

   @After("execution(*com.wisely.highlight_spring4.ch1.aop.DemoMethodService.*(..))")  // 7  这是使用拦截规则被拦截,具体到这里就是指所有com.wisely.highlight_spring4.ch1.aop.DemoMethodService里面的方法都会被拦截

   public void after(JoinPoint joinPoint) {

      MethodSignaturesignature = (MethodSignature) joinPoint.getSignature();

      Methodmethod =signature.getMethod();

     

      // 如果使用 7 行的方式拦截,则action将会为null 

      Actionaction =method.getAnnotation(Action.class);

 

     

      System.out.println("after方法规则式拦截被拦截的方法 = "

      + method.getName()

      + ", method.getAnnotation(Action.class) =" + (null==action ? "null" : "null")

      + ", 被拦截的对象的包名 = " + signature.getDeclaringTypeName());

   }

}

 

 

 

3rd.      Main

package com.wisely.highlight_spring4.ch1.aop;

 

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

 

public class Main {

   public static void main(String[] args) {

       AnnotationConfigApplicationContext context =

                   new AnnotationConfigApplicationContext(AopConfig.class); //1

       

       DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);

       

       DemoMethodService demoMethodService = context.getBean(DemoMethodService.class);

       

//    demoAnnotationService.add();

//   

//    demoAnnotationService.delete();

       

       demoMethodService.add();

       

       context.close();

   }

 

}

 

 

4th.      @Configuration

package com.wisely.highlight_spring4.ch1.aop;

 

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.EnableAspectJAutoProxy;

 

@Configuration

@ComponentScan("com.wisely.highlight_spring4.ch1.aop")

@EnableAspectJAutoProxy//1

public class AopConfig {

 

}

 

 

5th.      Annotatiuon标记的被拦截类

package com.wisely.highlight_spring4.ch1.aop;

 

import org.springframework.stereotype.Service;

 

@Service

public class DemoAnnotationService {

   @Action(asd = "注解式拦截的add操作aaaaaaa1111")

   public void add() {

      System.out.println("有标注  DemoAnnotationService add 方法被调用了!");

   }

 

  

   @Action(asd="注解式拦截的delete操作bbbb1111")

    public void delete(){

      System.out.println("有标注  DemoAnnotationService delete 方法被调用了!");

   }

}

 

 

 

6th.      正则表达式定义的被拦截类

package com.wisely.highlight_spring4.ch1.aop;

 

import org.springframework.stereotype.Service;

 

@Service

public class DemoMethodService {

   public void add(){

      System.out.println("没有标注  DemoMethodService add 方法被调用了!");

   }

}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值