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

网站组织结构图手机网站设计框架

网站组织结构图,手机网站设计框架,广东网站开发需要多少钱,wordpress用什么php版本好Quarkus 基于CDI和拦截器实现AOP功能(进阶版) 拦截器的属性成员拦截器的重复使用基于属性成员和重复使用的拦截器的发消息案例 本节来了解一下拦截器高级特性(拦截器的重复使用和属性成员),官网说明:https:…

Quarkus 基于CDI和拦截器实现AOP功能(进阶版)

      • 拦截器的属性成员
      • 拦截器的重复使用
      • 基于属性成员和重复使用的拦截器的发消息案例

本节来了解一下拦截器高级特性(拦截器的重复使用和属性成员),官网说明:https://cn.quarkus.io/guides/cdi-reference#repeatable-interceptor-bindings。

拦截器的属性成员

拦截器自己是个注解,而注解是有属性的,所以我们时可以在自定义的拦截器注解中添加属性成员的,这样在拦截器使用的时候有更多扩展空间。

💡注意:很重要,quarkus对属性成员使用时的限制是这些属性成员必须是要被@Nonbinding注解所标注的,否则在使用有设置属性的拦截器时该拦截器功能不会生效。

@Nonbinding

  • 注解用于 CDI(Contexts and Dependency Injection)中的自定义注解,以标记不影响注解唯一性的属性。换句话说,当你使用 @InterceptorBinding 创建自定义注解并将其应用于拦截器时,默认情况下所有属性都参与注解的唯一性判断。这意味着,如果两个注解的属性值不同,它们将被视为不同的注解。
  • 有时你可能希望某些属性不影响注解的唯一性。这时你就可以使用 @Nonbinding 注解这些属性,使其在比较注解时被忽略。
  • 当你创建自定义注解并在拦截器上使用时,如果该注解有属性且这些属性未被标记为 @Nonbinding,这些属性将会影响注解的唯一性判断。这意味着拦截器上使用的注解和方法上使用的注解必须完全一致(包括所有属性的值),否则拦截器不会生效。
  • 在自定义注解的属性上添加 @Nonbinding 注解,使这些属性不影响注解的唯一性判断。这样,拦截器上使用的注解和方法上使用的注解可以被认为是相同的,即使它们的属性值不同,这样拦截器就会生效。

示例代码

package com.xxx.interceptor;import javax.enterprise.util.Nonbinding;
import javax.interceptor.InterceptorBinding;
import java.lang.annotation.*;@InterceptorBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SendMessage {/*** 消息类型 : "sms"表示短信,"email"表示邮件*/@NonbindingString sendType() default "sms";
}

拦截器的重复使用

允许在同一位置重复使用同一个注解,这是java注解的通用功能,并非quarkus独有,但是在quarkus中使用时有些需要注意的限制。

quarkus对重复使用同一拦截器注解的限制:

  1. 可以作用在方法上
  2. 不能作用在类上
  3. 不能作用在stereotypes上

关于2和3,官方的说法是将来会解决(This might be added in the future)

示例代码

package com.bolingcavalry.interceptor.define;import javax.enterprise.util.Nonbinding;
import javax.interceptor.InterceptorBinding;
import java.lang.annotation.*;@InterceptorBinding
@Repeatable(SendMessage.SendMessageList.class)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SendMessage {/*** 消息类型 : "sms"表示短信,"email"表示邮件*/@NonbindingString sendType() default "sms";@Target({ElementType.TYPE, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@interface SendMessageList {SendMessage[] value();}
}

基于属性成员和重复使用的拦截器的发消息案例

要求设计一个拦截器,名为SendMessage,功能是对外发送通知,通知的方式有短信和邮件两种,具体用哪种是可以通过拦截器属性设置的。

有个SendMsg的普通接口,此接口有三个实现类:SendMsgA、SendMsgB、SendMsgC,这些实现类都是bean,代码如下:

public interface SendMsg {String send();
}@ApplicationScoped
@Named("A")
public class SendMsgA implements SendMsg {@Overridepublic void send() {Log.info("send from A");}
}@ApplicationScoped
@Named("B")
public class SendMsgB implements SendMsg {@Overridepublic void send() {Log.info("send from B");}
}@ApplicationScoped
@Named("C")
public class SendMsgC implements SendMsg {@Overridepublic void send() {Log.info("send from C");}
}

需求:

  • 用SendMessage拦截器拦截SendMsgA,通知类型是短信
  • 用SendMessage拦截器拦截SendMsgB,通知类型是邮件
  • 用SendMessage拦截器拦截SendMsgC,通知类型是短信和邮件都发送

代码:

package com.xxx.interceptor;import javax.enterprise.util.Nonbinding;
import javax.interceptor.InterceptorBinding;
import java.lang.annotation.*;@InterceptorBinding
@Repeatable(SendMessage.SendMessageList.class)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SendMessage {/*** 消息类型 : "sms"表示短信,"email"表示邮件*/@NonbindingString sendType() default "sms";@Target({ElementType.TYPE, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@interface SendMessageList {SendMessage[] value();}
}
package com.xxx.interceptor.impl;import com.xxx.interceptor.SendMessage;
import com.xxx.interceptor.TrackParams;
import io.quarkus.arc.Priority;
import io.quarkus.arc.runtime.InterceptorBindings;
import io.quarkus.logging.Log;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import java.lang.annotation.Annotation;
import java.util.*;
import static io.quarkus.arc.ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS;@SendMessage
@Interceptor
public class SendMessageInterceptor {@AroundInvokeObject execute(InvocationContext context) throws Exception {// 取出所有注解,由于允许重复注解,因此通知类型可能有多个Set<Annotation> bindings = InterceptorBindings.getInterceptorBindings(invocationContext);// 获取被拦截方法的类名String interceptedClass = context.getTarget().getClass().getSimpleName();// ...// 先执行被拦截的方法Object rlt = context.proceed();// ...// 最后再返回方法执行结果return rlt;}
}

@ApplicationScoped
@Named("A")
public class SendMsgA implements SendMsg {@SendMessage@Overridepublic void send() {Log.info("send from A");}
}@ApplicationScoped
@Named("B")
public class SendMsgB implements SendMsg {@SendMessage(sendType = "email")@Overridepublic void send() {Log.info("send from B");}
}// 注意这里使用了两次SendMessage
@ApplicationScoped
@Named("C")
public class SendMsgC implements SendMsg {@SendMessage@SendMessage(sendType = "email")@Overridepublic void send() {Log.info("send from C");}
}
http://www.yayakq.cn/news/645672/

相关文章:

  • 无锡网站建设报价湛江购房网
  • 站长工具搜一搜歌曲推广平台有哪些
  • 应届生招聘去哪个网站网站上的专题 怎么设计
  • 手机销售网站模板和国外做贸易用什么网站
  • 服装门户系统网站wordpress改wp admin
  • 古风网站建设模板下载专业网站优化推广
  • 织梦系统网站模板修改深圳定制旗袍实体店
  • 东莞外贸网站建设巩义公司做网站
  • 北京网站备案号查询做网站可以用什么语言
  • 网站 橙色图书馆网站制作
  • 网站模板搭建旌阳区黄河开发建设网站
  • 电商网站建设的意义电商型企业网站建设
  • 电子商务网站建设考题与答案自学手机网站开发
  • 郑州网站优化价格如何选择大良网站建设
  • 与别人相比自己网站建设优势成都网站推广 优帮云
  • 安康网站建设公司有哪些商丘百度推广电话
  • 收集链接 做网站丽水做网站公司
  • h5网站建设功能计划表网页4399小游戏
  • 天气预报网站开发怎么搭建mysql数据库网站
  • 做淘宝用那些网站发货游戏代理0加盟费
  • 青岛哪个网站建设公司价格低还能好一些搜索引擎收录查询工具
  • 博客网站开发环境自适应网页设计规范
  • 网站设计的步骤前端工程师主要做什么
  • 建设论坛网站步骤新网 网站建立
  • 魔鬼做交易网站延边网站建设
  • 建设银行顺德分行网站网站用什么字体
  • .中国域名的网站招聘网站建设人员的要求
  • 翠峦网站建设百度应用市场
  • php网站开发技术是什么html5 手机网站开发叫才
  • 安庆 做网站网站后台内容不更新