@Target:注解的作用目标
@Target(ElementType.TYPE) //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR) //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包
@Retention(RetentionPolicy.RUNTIME) //注解会在class字节码文件中存在,在运行时可以通过反射获取到
@Documented 说明该注解将被包含在javadoc中
自定义一个Log.java类
在aspect获取自定义注解的 title action两个属性的值
引用处
Aspect拦截处获取自定义注解的属性的值
拿到注解类的参考代码
import org.aspectj.lang.reflect.MethodSignature;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
public LimitInterface getAnnotationLimitInter(JoinPoint jp) {
Signature signature = jp.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if(method!=null) {
LimitInterface limitInterface = method.getAnnotation(LimitInterface.class);
return limitInterface;
}
return null;
}
拿到参数的代码
Object[] objArray = JoinPoint.getArgs();
@Before("limitInterfacePointCut()")
public void beforeMethod(JoinPoint jp) {
String methodName = jp.getSignature().getName();
LimitInterface limitInterface = getAnnotationLimitInter(jp);
if(limitInterface!=null) {
//拿到了主机的信息
long inTimeSeconds = limitInterface.inTimeSeconds();
int maxCount = limitInterface.maxCount();
int lockTimeSeconds = limitInterface.lockTimeSeconds();
String LimieKey = null;
//开始拿参数的信息
Signature signature = jp.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if(method!=null) {
int i=0;
boolean flag=false;
Parameter[] parameters = method.getParameters();
for (Parameter parameter : parameters) {
if(parameter.isAnnotationPresent(LimitKey.class)) {
flag = true;
break;
}
i++;
}
if(flag) {
Object[] objArray = jp.getArgs();
LimieKey = objArray[i].toString();
}
}
System.out.println("inTimeSeconds="+inTimeSeconds);
System.out.println("maxCount"+maxCount);
System.out.println("lockTimeSeconds="+lockTimeSeconds);
System.out.println("LimieKey="+LimieKey);
// mainBusinMethod(inTimeSeconds,maxCount,lockTimeSeconds,LimieKey);//拿到了这些值,假如这些值
}
}
如何使用的