自定义aop注解,并获取注解中的参数

自定义注解:

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME; 
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Documented
@Retention(RUNTIME)
@Target(METHOD)
public @interface Ypjglog {   
	String description() default "";
}

自定义切面:

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;

@Aspect
public class LogAspect {	   	
	@Around(value = "within(@org.springframework.stereotype.Controller *) && @annotation(Ypjglog)")//对有@Controller注解类中的带有@Ypjglog的方法记录访问日志
	public Object saveLog(ProceedingJoinPoint pjp) {
		Signature signature = pjp.getSignature();//此处joinPoint的实现类是MethodInvocationProceedingJoinPoint
		MethodSignature methodSignature = (MethodSignature) signature;//获取参数名
		Ypjglog ypjglog = methodSignature.getMethod().getAnnotation(Ypjglog.class);
		System.out.println(ypjglog.description());
		try {
			return pjp.proceed(); //直接pjp.proceed(),没有return ,前台页面获取不到ajax数据
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}

control类:

@Controller
@RequestMapping({ "/cfdp" })
public class CfdpController {
	
	@Ypjglog(description = "首页")
	@RequestMapping("/cfdpList")
	public String cfdp(Model model,String url) {
		return "pages/cfdp/cfdpList";
	}
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值