自定义注解(一)

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值