面向方面编程Java-Get注释参数
我定义了3个类,如下所示。我要做的是,向注释提供输入,然后当注释函数抛出异常时,使用这些参数做一些事情 因此,基本上,在面向方面编程Java-Get注释参数,java,annotations,aspectj,Java,Annotations,Aspectj,我定义了3个类,如下所示。我要做的是,向注释提供输入,然后当注释函数抛出异常时,使用这些参数做一些事情 因此,基本上,在aferThrowing函数中的CreateFluxoTicket类中,我想要访问“shortDescription”和“details”属性 我浏览了许多链接和答案,但没有得到参数。我试图做的一件事如下所示,但参数列表是空的(输出是“参数类型大小=0”) 公共类临时类{ @Fluxo(shortDescription=“shortDescription”,details=“d
aferThrowing
函数中的CreateFluxoTicket
类中,我想要访问“shortDescription”和“details”属性
我浏览了许多链接和答案,但没有得到参数。我试图做的一件事如下所示,但参数列表是空的(输出是“参数类型大小=0”)
公共类临时类{
@Fluxo(shortDescription=“shortDescription”,details=“details”)
公共空间{
系统输出打印项次(50/0);
}
}
@保留(RetentionPolicy.RUNTIME)
@目标(ElementType.METHOD)
public@interface Fluxo{
字符串shortDescription();
字符串详细信息();
}
@面貌
公共类票据{
@后置(切入点=“@annotation(metrics.annotations.Fluxo)”,抛出=“e”)
以下异常后的公共无效(接合点接合点,可丢弃e){
签名签名=jointPoint.getStaticPart().getSignature();
if(签名实例of MethodSignature){
MethodSignature ms=(MethodSignature)签名;
类[]parameterTypes=ms.getParameterTypes();
System.out.println(“参数类型size=“+parameterTypes.length”);
对于(最终类pt:参数类型){
System.out.println(“参数类型:“+pt”);
}
}
}
}
您可以将注释绑定到类似于异常的参数。试试这个:
package metrics.aspect;
import metrics.annotations.Fluxo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class CreateFluxoTicket {
@AfterThrowing(
pointcut = "execution(* *(..)) && @annotation(fluxo)",
throwing = "throwable"
)
public void afterThrowingException(
JoinPoint thisJoinPoint,
Fluxo fluxo,
Throwable throwable
) {
System.out.println(thisJoinPoint + " -> " + fluxo.shortDescription());
}
}
如您所见,我还将切入点限制为方法执行
s,因为否则调用
s也会触发通知,并且您会看到两次输出
package metrics.aspect;
import metrics.annotations.Fluxo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class CreateFluxoTicket {
@AfterThrowing(
pointcut = "execution(* *(..)) && @annotation(fluxo)",
throwing = "throwable"
)
public void afterThrowingException(
JoinPoint thisJoinPoint,
Fluxo fluxo,
Throwable throwable
) {
System.out.println(thisJoinPoint + " -> " + fluxo.shortDescription());
}
}