自定义注解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" />
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章