这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
public interface SayHello {
String hello();
}
@ApplicationScoped
@Named("A")
public class SayHelloA implements SayHello {
@Override
public void hello() {
Log.info("hello from A");
}
}
@ApplicationScoped
@Named("B")
public class SayHelloB implements SayHello {
@Override
public void hello() {
Log.info("hello from B");
}
}
@ApplicationScoped
@Named("C")
public class SayHelloC implements SayHello {
@Override
public void hello() {
Log.info("hello from C");
}
}
@ApplicationScoped
@SendMessage(sendType="sms")
public class SayHelloA implements SayHello {
@ApplicationScoped
@SendMessage(sendType="sms")
@SendMessage(sendType="email")
public class SayHelloC implements SayHello {
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"表示邮件
* @return
*/
@Nonbinding
String sendType() default "sms";
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface SendMessageList {
SendMessage[] value();
}
}
package com.bolingcavalry.interceptor.impl;
import com.bolingcavalry.interceptor.define.SendMessage;
import com.bolingcavalry.interceptor.define.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 {
@AroundInvoke
Object execute(InvocationContext context) throws Exception {
// 先执行被拦截的方法
Object rlt = context.proceed();
// 获取被拦截方法的类名
String interceptedClass = context.getTarget().getClass().getSimpleName();
// 代码能走到这里,表示被拦截的方法已执行成功,未出现异常
// 从context中获取通知类型,由于允许重复注解,因此通知类型可能有多个
List<String> allTypes = getAllTypes(context);
// 将所有消息类型打印出来
Log.infov("{0} messageTypes : {1}", interceptedClass, allTypes);
// 遍历所有消息类型,调用对应的方法处理
for (String type : allTypes) {
switch (type) {
// 短信
case "sms":
sendSms();
break;
// 邮件
case "email":
sendEmail();
break;
}
}
// 最后再返回方法执行结果
return rlt;
}
/**
* 从InvocationContext中取出所有注解,过滤出SendMessage类型的,将它们的type属性放入List中返回
* @param invocationContext
* @return
*/
private List<String> getAllTypes(InvocationContext invocationContext) {
// 取出所有注解
Set<Annotation> bindings = InterceptorBindings.getInterceptorBindings(invocationContext);
List<String> allTypes = new ArrayList<>();
// 遍历所有注解,过滤出SendMessage类型的
for (Annotation binding : bindings) {
if (binding instanceof SendMessage) {
allTypes.add(((SendMessage) binding).sendType());
}
}
return allTypes;
}
/**
* 模拟发送短信
*/
private void sendSms() {
Log.info("operating success, from sms");
}
/**
* 模拟发送邮件
*/
private void sendEmail() {
Log.info("operating success, from email");
}
}
@ApplicationScoped
@Named("A")
public class SayHelloA implements SayHello {
@SendMessage
@Override
public void hello() {
Log.info("hello from A");
}
}
@ApplicationScoped
@Named("B")
public class SayHelloB implements SayHello {
@SendMessage(sendType = "email")
@Override
public void hello() {
Log.info("hello from B");
}
}
@ApplicationScoped
@Named("C")
public class SayHelloC implements SayHello {
@SendMessage
@SendMessage(sendType = "email")
@Override
public void hello() {
Log.info("hello from C");
}
}
@QuarkusTest
public class SendMessageTest {
@Named("A")
SayHello sayHelloA;
@Named("B")
SayHello sayHelloB;
@Named("C")
SayHello sayHelloC;
@Test
public void testSendMessage() {
sayHelloA.hello();
sayHelloB.hello();
sayHelloC.hello();
}
}
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |