這個(gè)是弄著玩的,使用的范圍比較局限, 寫出來(lái)也比較雞肋,我只是想把這個(gè)過(guò)程記下來(lái)。
aop都知道是代碼插裝,即在方法前后增加一些額外的代碼,我們的代碼中有大量的埋點(diǎn),能否用aop以注解的方式插入埋點(diǎn),即在我們需要的方法前添加注解,執(zhí)行到該方法的時(shí)候發(fā)出埋點(diǎn)。
1.定義注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
public @interface Analyse {
String cid();
String act();
String lab();
}
定義的是編譯時(shí)注解,只能使用在方法上,包含三個(gè)字段就是我們?cè)诼顸c(diǎn)時(shí)需要需要使用的。
@Aspect
public final class AnalyseInsert {
@Around("execution(@com.sankuai.moviepro.common.aop.analy.Analyse * *(..))")
public Object analyseInsert(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
Analyse auth = method.getAnnotation(Analyse.class);
String lab = auth.lab();
if(TextUtils.isEmpty(lab)){
AnalyseUtils.analyseEvent(null,auth.cid(),auth.act());
} else {
AnalyseUtils.analyseEvent(null,auth.cid(),auth.act(),auth.lab());
}
Object object = joinPoint.proceed();
return object;
}
}
使用around注解表示所有注解了Analyse的方法都插裝代碼,然后就是獲取注解中的參數(shù)再發(fā)出埋點(diǎn)。ok