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;
}*/
}