本文是对参考连接的一个补充,只讨论@within@target的区别。如果不懂注解的、不懂AOP的先去补一下。

参考连接

概述

@within@target是在配置切点的时候使用到的两个修饰符,都是基于注解来配置切点。

比如当前有注解@A

  • "@within(com.annotation.other.A1)"该配置就是:如果某个类上标注了注解@A,那么该类中的所有方法就会被匹配为切点,并且该类的子类中没有重写的父类方法也会被匹配为切点(看不懂的别急,后面有例子)
  • "@target(com.annotation.other.A1)"该配置就是:如果某个类上标注了注解@A,那么该类中的所有方法就会被匹配为切点。

约定

本文说的子类和父类关系都是类之间的子类和父类。不涉及注解继承,也就是自定义注解中没有标注元注解–@Inherited。

@within@target正确用法是 "@within/@target(注解的全类名)",以下简写"@within/@target(@A1)"

实例

类图

两个自定义注解

//注解@A1
@Retention(RetentionPolicy.RUNTIME)
@Target( ElementType.TYPE)
public @interface A1 {}//注解@A2
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface A2 {}

三个有继承关系的类

@A1
public  class Human {public void say(String sentence){System.out.println("Human says:" + sentence);}public void run(){System.out.println("Human runs." );}public void jump(){System.out.println("Human jump." );}
}@A2
public class Man  extends Human{@Overridepublic void run(){System.out.println("Man runs." );}public void testMan(){System.out.println("Man testMan." );}
}public class Boy extends Man{@Overridepublic void jump(){System.out.println("Boy jump." );}public void test(){System.out.println("Bot test...");}
}

切面

@Aspect
@Component
public class HumanAspect {@Before("@within(com.annotation.other.A1)")public void execute1(){System.out.println("@within --- A1");}@Before("@target(com.annotation.other.A1)")public void execute2(){System.out.println("@target --- A1");}@Before("@within(com.annotation.other.A2)")public void execute3(){System.out.println("@within --- A2");}@Before("@target(com.annotation.other.A2)")public void execute4(){System.out.println("@target --- A2");}
}

配置类

@Configuration
@ComponentScan("com.annotation.other")
@EnableAspectJAutoProxy
public class HumanManager {@Bean(name = "human")public Human getHuman() {return new Human();}@Bean(name = "man")public Man getMan() {return new Man();}@Bean(name = "boy")public Boy getBoy() {return new Boy();}
}

输出结果

--------------- This is a Human ---------------
@within --- A1
@target --- A1
Human says:hello!
@within --- A1
@target --- A1
Human jump.
@within --- A1
@target --- A1
Human runs.
---------------------This is a Man ---------------
@within --- A1
@target --- A2
Human says:hello!
@within --- A1
@target --- A2
Human jump.
@within --- A2
@target --- A2
Man runs.
---------------------This is a Boy ---------------
@within --- A1
Human says:hello!
Boy jump.
@within --- A2
Man runs.
Bot test...

结合类图来分下下输出结果

对于Human方法的输出情况。可以确定的是,如果类上标注了@within@target指定的注解,那么该类的所有方法就会被匹配为切点。

对于Man方法的输出情况,Man只标有注解@A2,Man的父类标有注解@A1
- 继承了父类,但没有重写的方法会被@within(@A1)匹配为切点,不会被@target(@A1)匹配。

- 继承父类并在子类重写的方法不会被@within(@A1)匹配为切点,也不会被@target(@A1)匹配为切点。

- 对于@within(@A2)、@target(@A2)会匹配Man类中非继承或继承但重写或自己的方法为切点。

对于Boy类输出情况 ,Boy类上没有注解,Boy继承了Man类,Man类继承了Human类。Man类表有注解@A2,Human类标有注解@A1。 Boy重写了属于Human类的jump()方法,Boy类有自己的方法test()

  • @winthin(@A1)匹配了Boy类继承Human类但并未重写的方法,@winthin(@A2)匹配了继承自Man类但并为重写的方法。

总结

对于匹配不具有继承性且为运行时的注解

即被匹配注解标注了@Retention(RetentionPolicy.RUNTIME),但没有标注
@Inherited

  • @within会匹配到标注了指定注解的类,并且在该类的子类中,那些没有重写的父类方法也会被匹配到。
    -@target只匹配标注了指定注解的类。不涉及任何其他类。

彻底搞懂SpringAOP中的@within和@target相关推荐

  1. 彻底搞懂 JS 中 this 机制

    彻底搞懂 JS 中 this 机制 摘要:本文属于原创,欢迎转载,转载请保留出处:https://github.com/jasonGeng88/blog 目录 this 是什么 this 的四种绑定规 ...

  2. 15个示例让你搞懂Linux中的cd命令

    15个示例让你搞懂Linux中的cd命令 在Linux中,cd(更改目录)命令是新手和系统管理员最重要且使用最广泛的命令之一.对于没有头绪的管理员来说,cd是导航到其他目录以检查日志,执行程序/应用程 ...

  3. java 自旋锁_搞懂Java中的自旋锁

    轻松搞懂Java中的自旋锁 前言 在之前的文章<一文彻底搞懂面试中常问的各种"锁">中介绍了Java中的各种"锁",可能对于不是很了解这些概念的同学 ...

  4. 分分搞懂c#中的委托

    分分搞懂c#中的委托: 不说废话,不来虚的概念,不管代码是否有意义,看我的优化之路,你会理解委托了: 源代码1 public class test{//我们不管代码是否有意义,我们直接看代码重构和一步 ...

  5. 一文搞懂Qt中的颜色渐变(QGradient Class)

    一文搞懂Qt中的颜色渐变(QGradient Class) 1, 快速开始! Qt中与颜色渐变有关的类是QGradient 其中它又有三个子类:QLinearGradient.QRadialGradi ...

  6. 帮你彻底搞懂JS中的prototype、__proto__与constructor(图解)

    帮你彻底搞懂JS中的prototype.__proto__与constructor(图解) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文 ...

  7. 这一篇彻底搞懂JS中的prototype、__proto__与constructor真的很好

    文章目录 1. 前言 2. _ _ proto _ _ 属性 3. prototype属性 4. constructor属性 5. 总结 提示:不要排斥,静下心来,认真读完,你就搞懂了!(可以先看一下 ...

  8. (转)帮你彻底搞懂JS中的prototype、__proto__与constructor(图解)

    文章目录 1. 前言 2. _ _ proto _ _ 属性 3. prototype属性 4. constructor属性 5. 总结 提示:不要排斥,静下心来,认真读完,你就搞懂了!(可以先看一下 ...

  9. 彻底搞懂javascript中的replace函数

    javascript这门语言一直就像一位带着面纱的美女,总是看不清,摸不透,一直专注服务器端,也从来没有特别重视过,直到最近几年,javascript越来越重要,越来越通用.最近和前端走的比较近,借此 ...

最新文章

  1. 「Django」rest_framework学习系列-用户认证
  2. java web 开发应用 ----过滤器
  3. 计算机组装与维修是几级考试,计算机组装与维修期末考试试卷讲解学习.pdf
  4. 简述python程序结构_python架构的概念讲解
  5. vsim生成VCD波形文件(verilog)
  6. mysql-学习-7-20170517-mysql分区
  7. python自定义异常类时、可以继承的类是_Python异常类型及处理、自定义异常类型、断言...
  8. vuex modules ajax,VUE项目爬坑---6、vuex的真正存在的意义是什么
  9. 顺丰同城:香港IPO发行价定为16.42港元
  10. 20-linux下ElasticSearch.6.2.2集群安装与head、Kibana、X-Pack..插件的配置安装
  11. 如何快速看透一个人?
  12. 简易检测wifi信号强度协助检测网络
  13. 电子设计教程7:线性稳压电源的工作原理
  14. 中图分类号,文献标识码,文章编号
  15. Android实战——RecyclerView条目曝光埋点
  16. 不间断电源 (UPS)全国产化电子元件推荐方案
  17. R语言1——R的安装和相关介绍
  18. 最近有点沉迷switch游戏
  19. CF--1000D - Yet Another Problem On a Subsequence
  20. 盘点:2022年豆瓣评分8.0以上的计算机书籍有哪些?

热门文章

  1. 【Hadoop 】Hadoop datanode启动不起来的原因总结
  2. 龙芯1b(LS1B200)使用LVGL7.0.1组件 使用中文的坑
  3. 笔记 学习51单片机串口中断
  4. sql-用户流失,回流问题
  5. 公共关系学知识点整理
  6. linux获取图标接口,Linux ioctl接口
  7. MacClean360 for mac(系统清理软件)
  8. r语言npsurv_R语言常用包分类总结 - osc_mf6gua6n的个人空间 - OSCHINA - 中文开源技术交流社区...
  9. 计算机绘图二维三维实用教程,计算机绘图二维三维实用教程教学课件作者王建勇第二章.ppt...
  10. 通过注册表更改组策略(禁止安装软件)bat脚本