Spring Aop自定义注解用在Controller层

 前提项目用的框架是SpringMVC。

切面类:

@Aspect
//@Component 把这个注掉是为了不让Spring中扫描,应该让SpringMVC扫描
public class SysLogAop {
    @Pointcut("@annotation(com.thinkgem.jeesite.common.annotation.SysLogAnnotation)")
    public void requestPointcut(){}


    @Around("requestPointcut()")
    public Object around(ProceedingJoinPoint point) throws Throwable{
        long startTime = System.currentTimeMillis();
        Object[] objArr = point.getArgs();
        Object obj = point.proceed();
        long endTime = System.currentTimeMillis();
        System.out.println("响应时间:"+(endTime-startTime));
        return obj;
    }
}

 

自定义注解:

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLogAnnotation {
   String title();
}

 Spring-mvc.xml:添加aop的schema



    
    Spring MVC Configuration
   
   
   
   
      
   
    
   
   
   

 Controller层使用

@SysLogAnnotation(title="试题标引审核管理-公共题标识")
@RequestMapping("isPublic")
public String isPublic( Question question, Model model){
   model.addAttribute( "userId", UserUtils.getUser().getId() );
   return "modules/ykd/question/question_isPublic_index";
}

你可能感兴趣的:(SpringMVC)