自定義註解aop

首先要有一個註解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
	public @interface  MyLog {
}

然後有一個切面類

@Component
@Aspect
public class MyLogAspect {

    @Pointcut("@annotation(cn.alibaba.MyAnnotation.MyLog)")
    public void logPointCut(){

    }

    @Around("logPointCut()")
    public void logAround(ProceedingJoinPoint joinPoint){
        //方法名稱
        String methodName = joinPoint.getSignature().getName();
        System.out.println("開始:"+methodName+"方法");
        try {
            joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("結束:"+methodName+"方法");
    }

}
	@RequestMapping("/testLog3")
    @ResponseBody
    @MyLog
    public void testLog3(){
        System.out.println("testlog3 ing...");
    }

測試controlller就可以了

如果是springmvc項目 配置文件還要加一句開啓aspectj

<aop:aspectj-autoproxy proxy-target-class="true" />
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章