package com.example.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/*
* CONSTRUCTOR:构造器的声明
* FIELD:域声明(包括enum实例)
* LOCAL_VARIABLE:局部变量声明
* METHOD:方法声明
* PACKAGE:包声明
* PARAMETER:参数声明
* TYPE:类、接口(包括注解类型)或enum声明
*/
@Target({ElementType.PARAMETER,ElementType.METHOD}) //作用在参数声明 和方法声明
@Documented //注解是否包含在javadoc中
/*
* SOURCE:注解将被编译器丢弃
* CLASS:注解在class文件中可用,但会被VM丢弃
* RUNTIME:VM将在运行期间保留注解,因此可以通过反射机制读取注解的信息
*/
@Retention(RetentionPolicy.RUNTIME)//定义该注解的声明周期
public @interface ParamLog {
String descrityption() default "";
}
package com.example.annotation;
import java.util.Map;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import com.example.utile.Utiles;
@Aspect
@Component
public class SysParamLog {
@Pointcut("@annotation(com.example.annotation.ParamLog)")
public void controllerAspect(){
System.out.println("I am Point");
}
@Before("controllerAspect()")
public void doBefor(JoinPoint point) throws Exception{
Map<String,String> befor = (Map<String, String>) point.getArgs()[0];
System.out.println(befor);
}
@After("controllerAspect()")
public void doAfter(){
System.out.println("结束切点");
}
@SuppressWarnings("unchecked")
@AfterReturning(pointcut=("controllerAspect()"),returning="rvt")
public void afterReturn(JoinPoint point,Object rvt) throws Exception{
Map<String,Object> befor = (Map<String, Object>) point.getArgs()[0];
Map<String,Object> after = (Map<String, Object>) rvt;
Utiles.compare(befor, after);
}
/*@Around("controllerAspect()")
public void around(ProceedingJoinPoint point) throws Throwable{
Object[] args = point.getArgs();
Object rvt = point.proceed(args);
Map<String,String> befor = (Map<String, String>) args[0];
Map<String,String> after = (Map<String, String>) rvt;
System.out.println("afterReturn"+befor);
System.out.println("afterReturn"+after);
Utiles.compare(befor, after);
}*/
@AfterThrowing(pointcut="controllerAspect()",throwing="e")
public void doAfterThrowing(JoinPoint point,Throwable e) throws Exception{
System.out.println("方法出出错"+e.getStackTrace()[0].getMethodName());
}
/**
* 获取自定义注解内容
* @return
* @throws ClassNotFoundException
*/
/*public Map<String,Object> getAnnotationDescription(JoinPoint point) throws ClassNotFoundException{
Map<String,Object> map = new HashMap<String,Object>();
String targetName = point.getTarget().getClass().getName();
String targetMethod = point.getSignature().getName();
Object[] args = point.getArgs();
Class targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
for(Method method:methods){
if(method.getName().equals(targetMethod)){
Class[] clazzs = method.getParameterTypes();
if(clazzs.length==args.length){
map.put("desctription", method.getAnnotation(ParamLog.class).descrityption());
break;
}
}
}
return map;
}*/
}