`
raymond.chen
  • 浏览: 1417521 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Spring AOP使用整理:使用@AspectJ风格的切面声明

阅读更多

要启用基于@AspectJ风格的切面声明,需要进行以下的配置:

<!-- 启用@AspectJ风格的切面声明 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>

<!-- 通过注解定义bean。默认同时也通过注解自动注入 -->
<context:component-scan base-package="com.cjm"/>

 

基于@AspectJ风格的切面声明的源码:

/**
 * 声明本类为一个切面
 */
@Component
@Aspect
public class MyAspectJ {
	/**
	 * 声明一个切入点(包括切入点表达式和切入点签名)
	 */
	@Pointcut("execution(* com.cjm.model..*.*(..))")
	public void pointcut1(){}
	
	/**
	 * 声明一个前置通知
	 */
	@Before("pointcut1()")
	public void beforeAdvide(JoinPoint point){
		System.out.println("触发了前置通知!");
	}
	
	/**
	 * 声明一个后置通知
	 */
	@After("pointcut1()")
	public void afterAdvie(JoinPoint point){
		System.out.println("触发了后置通知,抛出异常也会被触发!");
	}
	
	/**
	 * 声明一个返回后通知
	 */
	@AfterReturning(pointcut="pointcut1()", returning="ret")
	public void afterReturningAdvice(JoinPoint point, Object ret){
		System.out.println("触发了返回后通知,抛出异常时不被触发,返回值为:" + ret);
	}
	
	/**
	 * 声明一个异常通知
	 */
	@AfterThrowing(pointcut="pointcut1()", throwing="throwing")
	public void afterThrowsAdvice(JoinPoint point, RuntimeException throwing){
		System.out.println("触发了异常通知,抛出了RuntimeException异常!");
	}
	
	/**
	 * 声明一个环绕通知
	 */
	@Around("pointcut1()")
	public Object aroundAdvice(ProceedingJoinPoint point)throws Throwable{
		System.out.println("触发了环绕通知 开始");
		Object o = point.proceed();
		System.out.println("触发了环绕通知 结束");
		return o;
	}
}

 

1、切入点表达式的格式:execution([可见性] 返回类型 [声明类型].方法名(参数) [异常])

 

2、切入点表达式通配符:
      *:匹配所有字符
      ..:一般用于匹配多个包,多个参数
      +:表示类及其子类

 

3、切入点表达式支持逻辑运算符:&&、||、!

 

4、切入点表达式关键词:
      1)execution:用于匹配子表达式。

            //匹配com.cjm.model包及其子包中所有类中的所有方法,返回类型任意,方法参数任意
            @Pointcut("execution(* com.cjm.model..*.*(..))")
            public void before(){}

 

      2)within:用于匹配连接点所在的Java类或者包。

            //匹配Person类中的所有方法
            @Pointcut("within(com.cjm.model.Person)")
            public void before(){}

 

            //匹配com.cjm包及其子包中所有类中的所有方法

            @Pointcut("within(com.cjm..*)")
            public void before(){}

 

     3) this:用于向通知方法中传入代理对象的引用。
            @Before("before() && this(proxy)")
            public void beforeAdvide(JoinPoint point, Object proxy){
                  //处理逻辑
            }

 

      4)target:用于向通知方法中传入目标对象的引用。
            @Before("before() && target(target)
            public void beforeAdvide(JoinPoint point, Object proxy){
                  //处理逻辑
            }

 

      5)args:用于将参数传入到通知方法中。
            @Before("before() && args(age,username)")
            public void beforeAdvide(JoinPoint point, int age, String username){
                  //处理逻辑
            }
 
      6)@within:用于匹配在类一级使用了参数确定的注解的类,其所有方法都将被匹配。 

            @Pointcut("@within(com.cjm.annotation.AdviceAnnotation)") - 所有被@AdviceAnnotation标注的类都将匹配
            public void before(){}

 

  @Retention(RetentionPolicy.RUNTIME)
  @Target({ElementType.TYPE, ElementType.METHOD})
  @Documented
  @Inherited
  public @interface AdviceAnnotation {

  }

  

      7)@target:和@within的功能类似,但必须要指定注解接口的保留策略为RUNTIME。
            @Pointcut("@target(com.cjm.annotation.AdviceAnnotation)")
            public void before(){}

 

      8)@args:传入连接点的对象对应的Java类必须被@args指定的Annotation注解标注。
            @Before("@args(com.cjm.annotation.AdviceAnnotation)")
            public void beforeAdvide(JoinPoint point){
                  //处理逻辑
            }

  

public class Person {
    public void say(Address address){
          //处理逻辑
   }
}

 @AdviceAnnotation
 public class Address {
   
 }

 

  如果需要在Person类的say方法被调用时触发beforeAdvide通知,那么say方法的参数对应的Java类型Address类必须要被@AdviceAnnotation标注。

 

      9)@annotation:匹配连接点被它参数指定的Annotation注解的方法。也就是说,所有被指定注解标注的方法都将匹配。
            @Pointcut("@annotation(com.cjm.annotation.AdviceAnnotation)")
            public void before(){}

 

public class Person {
      @AdviceAnnotation
      public void say(Address address){
              //处理逻辑
    }
}

  

  Person类的say方法被@AdviceAnnotation标注,所以它匹配。

 

      10)bean:通过受管Bean的名字来限定连接点所在的Bean。该关键词是Spring2.5新增的。
            @Pointcut("bean(person)")
            public void before(){}

 

            id为person的受管Bean中的所有方法都将匹配。

0
0
分享到:
评论

相关推荐

    spring aop 自定义切面示例

    spring aop 自定义切面示例 aspectj 需要相应的架包支持

    SpringBoot中的AOP+自定义注解(源代码)

    1.1 `@AspectJ` 切面类 1.2 `@Pointcut` 创建切入点 1.3 通知 1.4 Spring AOP 和 AspectJ AOP 有什么区别? 2. 在 SpringBoot 中使用 Aop 功能 2.0 创建一个SpringBoot项目 2.1 引入 POM 依赖 2.1.1 引入springboot ...

    【Spring AOP】@Aspect结合案例详解(二): @Pointcut使用@within和within

    在微服务流行的当下,在使用Spring Cloud / Spring Boot框架开发中,AOP使用的非常广泛,尤其是@Aspect注解方式当属最流行的,不止功能强大,性能也很优秀,还很舒心!所以本系列就结合案例详细介绍@Aspect方式的切...

    SpringAOP切面编程依赖jar包.rar

    学习Spring开发的AOP面向切面编程时所需要的jar包,包括com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

    Spring AOP源码深度解析:掌握Java高级编程核心技术

    Spring AOP(面向切面编程)是Java高级编程中的重要组成部分,它允许程序员以声明的方式处理关注点(例如日志、事务管理等),而不是通过硬编码。本文深入分析了Spring AOP的实现机制,让读者能够更好地理解和应用这...

    进击的编程思想!带你学Spring AOP核心编程思想教程 新角度学面向切面编程

    AOP 的全称是“Aspect Oriented Programming”,即面向切面编程,它将业务逻辑的各个部分进行隔离,使开发人员在编写业务逻辑时可以专心于核心业务,从而提高了开发效率。AOP 采取横向抽取机制,取代了传统纵向继承...

    25个经典的Spring面试问答

    Spring Framework简介:了解Spring框架的核心理念,包括依赖注入(DI)和面向切面编程(AOP)。 Spring Boot:熟悉Spring Boot,知道如何创建和管理Spring Boot项目,以及如何使用Spring Boot的自动配置功能。 ...

    springIOC核心组件分析.vsdx

    pring源代码各个模块作用 核心模块: 1 spring-core:核心模块 依赖注入IOC和DI的最基本实现 ...spring-aop:面向切面编程,CGLB,JDKProxy spring-aspects:集成AspectJ,Aop应用框架 spring-instrume

    spring的aop切面编程实例

    实现spring的aop的操作,采用AspectJ技术,通过xml的配置来实现,本人亲自测试过,aop相关架包已引入

    Spring Framewor开发手册

    2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载时织入(AspectJ load-time weaving)的...

    spring aop切面编程需要aspectjweaver-1.5.3.jar aspectj-1.9.6.jar aspectjrt-1.7.4.jar

    spring aop切面编程需要aspectjweaver-1.5.3.jar aspectj-1.9.6.jar aspectjrt-1.7.4.jar

    mybatis 拦截器 + spring aop切面 + spring事务+ 反射工具类

    内含有mybatis 拦截器实现的分页代码,spring 的事务和aop 测试、和反射工具类

    Spring实现AOP的四种方式

    配置可以通过xml文件来进行,大概有四种方式: 1. 配置ProxyFactoryBean,显式地设置...3. 通过&lt;aop:config&gt;来配置(纯POJO切面) 4. 通过&lt;aop: aspectj-autoproxy&gt;来配置,使用AspectJ的注解来标识通知及切入点

    spring2.5.chm帮助文档(中文版)

    2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载时织入(AspectJ load-time weaving...

    aop面向切面需要的jar包

    在使用spring的aop功能时,这两个jar是必须的,否则会报错,如下: Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException at java.net....

    SpringAOP切面实例讲解及应用场景(通俗易懂)

    下面是一个在spring mvc中关于切面如何使用的例子,可以指直观的理解切面到底有什么作用 1、引用 AspectJ jar 包依赖 pom.xml 文件添加依赖 org.aspectj aspectjrt 1.9.2 2、创建两个新的包 ...

    spring-AOP面向切面编程所需jar包.zip

    该压缩包包含--学习笔记(05):轻松搞定Spring全家桶(初识篇)-面向...包括spring-aspects-5.2.7.RELEASE.jar和spring-aop-5.2.7.RELEASE.jar和com.springsource.org.aspectj.weaver-1.7.2.RELEASE.jar。需要的欢迎来拿

    43 离开了 Spring AOP,我们如何切面编程?慕课专栏1

    简介我们大都知道 Spring AOP 集成了 AspectJ,可是到底什么是 AspectJ 呢官方描述a seamless aspect-oriented

    基于AspectJ的AOP开发案例源码.rar

    demo1是aspectj的Aop开发,用于用户是否登录的验证,使用注解来实现,在切面类中配置好切入点。优点:方便快捷 demo2是aspectj的Aop开发,用于用户是否登录的验证,在xml中配置好切面。 优点:便于维护,不用修改源...

    AOP 面向切面编程1

    } }用 AspectJ 注解声明切面:要在 Spring 中声明 AspectJ 切面, 只需要在 IOC 容器中将切面声明为 Bean 实例. 当在 Spr

Global site tag (gtag.js) - Google Analytics