当前位置: 首页 > news >正文

网站建设与管理作业/品牌宣传策划方案

网站建设与管理作业,品牌宣传策划方案,网络游戏排行榜前十手游,网站建设前期准备方案一、注解的继承性回顾 被Inherited元注解标注的注解标注在类上的时候,子类可以继承父类上的注解。注解未被Inherited元注解标注的,该注解标注在类上时,子类不会继承父类上标注的注解。注解标注在接口上,其子类及子接口都不会继承…
一、注解的继承性回顾
  1. 被@Inherited元注解标注的注解标注在类上的时候,子类可以继承父类上的注解。
  2. 注解未被@Inherited元注解标注的,该注解标注在类上时,子类不会继承父类上标注的注解。
  3. 注解标注在接口上,其子类及子接口都不会继承该注解
  4. 注解标注在类或接口方法上,其子类重写该方法不会继承父类或接口中方法上标记的注解

根据注解继承的特性,我们再做AOP切面拦截的时候会遇到拦截不到的问题,今天我们就讲解下对这些特殊情况如何解决,对源码不做过渡深入的讲解。

二、注解标注在父类、接口、父类方法、接口方法上如何通过子类拦截,首先了解下几个核心类

AnnotationMatchingPointcut切点类是用来判定是否需要拦截类、父类、实现接口中方法,其中一共四个构造函数如下:

	/*** Create a new AnnotationMatchingPointcut for the given annotation type.* @param classAnnotationType the annotation type to look for at the class level*/
public AnnotationMatchingPointcut(Class<? extends Annotation> classAnnotationType) {this(classAnnotationType, false);}/*** Create a new AnnotationMatchingPointcut for the given annotation type.* @param classAnnotationType the annotation type to look for at the class level* @param checkInherited whether to also check the superclasses and interfaces* as well as meta-annotations for the annotation type* @see AnnotationClassFilter#AnnotationClassFilter(Class, boolean)*/public AnnotationMatchingPointcut(Class<? extends Annotation> classAnnotationType, boolean checkInherited) {this.classFilter = new AnnotationClassFilter(classAnnotationType, checkInherited);this.methodMatcher = MethodMatcher.TRUE;}/*** Create a new AnnotationMatchingPointcut for the given annotation types.* @param classAnnotationType the annotation type to look for at the class level* (can be {@code null})* @param methodAnnotationType the annotation type to look for at the method level* (can be {@code null})*/public AnnotationMatchingPointcut(@Nullable Class<? extends Annotation> classAnnotationType,@Nullable Class<? extends Annotation> methodAnnotationType) {this(classAnnotationType, methodAnnotationType, false);}/*** Create a new AnnotationMatchingPointcut for the given annotation types.* @param classAnnotationType the annotation type to look for at the class level* (can be {@code null})* @param methodAnnotationType the annotation type to look for at the method level* (can be {@code null})* @param checkInherited whether to also check the superclasses and interfaces* as well as meta-annotations for the annotation type* @since 5.0* @see AnnotationClassFilter#AnnotationClassFilter(Class, boolean)* @see AnnotationMethodMatcher#AnnotationMethodMatcher(Class, boolean)*/public AnnotationMatchingPointcut(@Nullable Class<? extends Annotation> classAnnotationType,@Nullable Class<? extends Annotation> methodAnnotationType, boolean checkInherited) {Assert.isTrue((classAnnotationType != null || methodAnnotationType != null),"Either Class annotation type or Method annotation type needs to be specified (or both)");if (classAnnotationType != null) {this.classFilter = new AnnotationClassFilter(classAnnotationType, checkInherited);}else {this.classFilter = new AnnotationCandidateClassFilter(methodAnnotationType);}if (methodAnnotationType != null) {this.methodMatcher = new AnnotationMethodMatcher(methodAnnotationType, checkInherited);}else {this.methodMatcher = MethodMatcher.TRUE;}}

以上四个构造函数,支持仅标记在类上注解、仅标记方法上的注解、即指定标记类上且标记在方法上的注解(是否拦截父类及接口上标记的方法)三种构造方式。

通过上述四个构造函数可以构造如下几种切点类型:

  • 注解标注在当前类,只拦截当前类的方法
  • 注解标注在当前类的父类上,拦截父类及子类中的方法
  • 注解标注在当前类实现的接口上 ,拦截接口方法的实现方法
  • 注解标注在当前类的方法上,拦截当前类的方法
  • 注解标注在当前类父类或接口的方法上,拦截父类的方法或者当前类实现方法。

通过上述切点可以构造出我们需要的大多数场景,如果需要更灵活的实现还需要结合ComposablePointcut类,此类可以实现类级别标注的交集、并集,方法级别的交集、并集,切点级别的交集并集。

三、切点实现案例

    @Bean@Role(BeanDefinition.ROLE_INFRASTRUCTURE)public Advisor mybatisLogAdvisor(MybatisProperties properties) {//限定类级别的切点Pointcut cpc = new AnnotationMatchingPointcut(Mapper.class, properties.isCheckClassInherited());//限定方法级别的切点Pointcut mpc = new AnnotationMatchingPointcut(null, Mapper.class, properties.isCheckMethodInherited());//组合切面(并集),一、ClassFilter只要有一个符合条件就返回true,二、Pointcut pointcut = new ComposablePointcut(cpc).union(mpc);//mybatis日志拦截切面MethodInterceptor interceptor = new MybatisMethodInterceptor();//切面增强类AnnotationPointcutAdvisor advisor = new AnnotationPointcutAdvisor(interceptor, pointcut);//切面优先级顺序advisor.setOrder(AopOrderInfo.MYBATIS);return advisor;}

此拦截器可以实现对标注了Mapper方法进行日志拦截,具体实现可以参考GitHub源码

四、上述向上查询父类、父接口及其方法的核心是AnnotatedElementUtils工具类,示例参考如下:

 //返回当前类或父类或接口方法上标注的注解对象
targetDataSource = AnnotatedElementUtils.findMergedAnnotation(method, TargetDataSource.class);
//返回当前类或父类或接口上标注的注解对象
targetDataSource = AnnotatedElementUtils.findMergedAnnotation(method.getDeclaringClass(), TargetDataSource.class);

GitHub地址:https://github.com/mingyang66/spring-parent

http://www.jmfq.cn/news/5102605.html

相关文章:

  • 安阳 网站建设/河北百度推广电话
  • 简述网站开发基本流程图/网页模板代码
  • 网站开发职位/餐饮店如何引流与推广
  • 强化疫情防控/百家号seo怎么做
  • 如何看网站是谁做的/网页搜索引擎
  • 免费的舆情网站不用下载直接打开/怎么在百度上推广
  • 西宁市网站建设价格/网络营销教学大纲
  • 石家庄站布局图/平台优化是指什么
  • 免费海报制作网站/武汉今日头条最新消息
  • 专业的网站设计建设/厦门搜索引擎优化
  • 有哪些网站可以做全屏代码/seo技巧seo排名优化
  • 自己切片做网站/sem优化技巧
  • 福州专业网站建设服务商/百度账号人工客服电话
  • 网站代码500/重庆seo整站优化效果
  • 重庆网站建设公司电话/国内最新新闻
  • 线上推广媒体广告/搜索引擎优化营销
  • 网站怎么关键字优化/2024年新冠第三波症状分析
  • 做一个搜索引擎网站要多少钱/网络推广的公司更可靠
  • 怎么做彩票网站收款人/怎么做盲盒
  • wordpress录入表单写数据库/长沙seo关键词排名
  • 口腔医院网站源码/福州短视频seo方法
  • 企业网站 seo怎么做/查域名网站
  • 网站原型的交互怎么做/百度搜索seo优化技巧
  • Wordpress制作赚钱吗/seo搜索引擎优化主要做什么
  • wordpress默认页面设置/什么是seo和sem
  • 做设计用哪个素材网站好/搜索引擎营销是指
  • wordpress 网站投票/宁波做seo推广企业
  • 网站建设方案 预算/网文推广怎么做
  • 论坛的网站制作/室内设计师培训班学费多少
  • 如何做网站的外链/seo咨询师