美文网首页
基于注解的Spring AOP

基于注解的Spring AOP

作者: HeapOverflow | 来源:发表于2020-06-09 19:20 被阅读0次

前置条件:xml需要包含以下元素:

<aop:aspectj-autoproxy/>

步骤:

  1. 声明Aspect:告诉IoC容器,这是个切面
package com.tutorialspoint;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class Logging {
}
  1. 声明Pointcut切入点:告诉IoC容器,拦截器将要侵入的某个类---锚点
package com.tutorialspoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class Logging {
   /** Following is the definition for a pointcut to select
    *  all the methods available. So advice will be called
    *  for all the methods.
    */
   @Pointcut("execution(* com.tutorialspoint.*.*(..))")
   private void selectAll(){}
}
  1. 声明Advice:告诉IoC容器,我的各种各样的拦截器
package com.tutorialspoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
@Aspect
public class Logging {
   /** Following is the definition for a pointcut to select
    *  all the methods available. So advice will be called
    *  for all the methods.
    */
   @Pointcut("execution(* com.tutorialspoint.*.*(..))")
   private void selectAll(){}
   /** 
    * This is the method which I would like to execute
    * before a selected method execution.
    */
   @Before("selectAll()")
   public void beforeAdvice(){
      System.out.println("Going to setup student profile.");
   }
   /** 
    * This is the method which I would like to execute
    * after a selected method execution.
    */
   @After("selectAll()")
   public void afterAdvice(){
      System.out.println("Student profile has been setup.");
   }
   /** 
    * This is the method which I would like to execute
    * when any method returns.
    */
   @AfterReturning(pointcut = "selectAll()", returning="retVal")
   public void afterReturningAdvice(Object retVal){
      System.out.println("Returning:" + retVal.toString() );
   }
   /**
    * This is the method which I would like to execute
    * if there is an exception raised by any method.
    */
   @AfterThrowing(pointcut = "selectAll()", throwing = "ex")
   public void AfterThrowingAdvice(IllegalArgumentException ex){
      System.out.println("There has been an exception: " + ex.toString());   
   }  
}

被侵入的类的定义:

package com.tutorialspoint;
public class Student {
   private Integer age;
   private String name;
   public void setAge(Integer age) {
      this.age = age;
   }
   public Integer getAge() {
      System.out.println("Age : " + age );
      return age;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getName() {
      System.out.println("Name : " + name );
      return name;
   }
   public void printThrowException(){
      System.out.println("Exception raised");
      throw new IllegalArgumentException();
   }
}

测试主函数:

package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");
      Student student = (Student) context.getBean("student");
      student.getName();
      student.getAge();     
      student.printThrowException();
   }
}

运行结果:

Going to setup student profile.
Name : Zara
Student profile has been setup.
Returning:Zara
Going to setup student profile.
Age : 11
Student profile has been setup.
Returning:11
Going to setup student profile.
Exception raised
Student profile has been setup.
There has been an exception: java.lang.IllegalArgumentException
.....
other exception content

相关文章

  • spring03

    基于动态代理改造上限案例 掌握Spring AOP 基于配置文件方式 掌握Spring AOP 基于注解方式 重点...

  • Spring基于注解的AOP配置中的事务控制

    原文相关文章 Spring基于注解的AOP配置中的事务控制 在Spring基于注解的AOP事务控制配置中,使用四个...

  • Spring之使用注解配置Spring AOP

    Spring框架通过注解配置AOP是基于AspectJ实现的。 Spring框架只是直接使用了AspectJ的注解...

  • AOP笔记

    Spring提供了4种类型的AOP支持: 基于代理的经典Spring AOP 纯POJO切面 @AspectJ注解...

  • Spring Aop支持

    Spring提供了四种Aop支持: 基于代理的经典Spring Aop 纯Pojo切面 @AspectJ注解驱动的...

  • jfinal aop全局拦截器设置切点

    jfianl 的aop分为全局和基于注解两种,spring的aop很复杂,但是spring的aop可以自定义切点,...

  • Spring4-2-AOP配置

    一.AOP概念 二.AOP术语 三.Spring AOP框架AspectJ配置使用(基于注解的方式) (1)必要的...

  • 第12章-Spring基于注解配置AOP

    Spring 的 AOP 功能是基于 AspectJ 实现的,支持使用注解声明式定义 AOP 切面。 理解 AOP...

  • 06 AOP

    Spring 提供了四种AOP支撑:基于代理的经典spring aop;纯POJO切面@AspectJ注解驱动的切...

  • Spring之AOP(二)

    四、基于@AspectJ的AOP Spring除了支持Schema方式配置AOP,还支持注解方式:使用@Aspec...

网友评论

      本文标题:基于注解的Spring AOP

      本文链接:https://www.haomeiwen.com/subject/dtuetktx.html