文章内容
实现步骤
1、写一个切面
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 | @Aspect @Component public class LogInfoAspect { @Pointcut (value = "@annotation(com.main.entity.annotation.LogInfo)" ) public void LogInfoAspect(){ } // 在这里定义前置切面 @Before ( "LogInfoAspect()" ) public void beforeMethod(JoinPoint joinPoint) { // 这里执行保存日志的动作 System.out.println( "before ......." ); //得到被切方法的参数 System.out.println(joinPoint.getArgs()[ 0 ]); } } |
2、写一个注解
1 2 3 4 5 6 | @Retention(RetentionPolicy.RUNTIME) @Documented @Target(ElementType.METHOD) public @interface LogInfo { boolean value() default true; } |
3、Controller的引用
1 2 3 4 5 | @LogInfo @RequestMapping(value = "/trrtt") public String trrtt(HttpServletRequest request, Model model) { return “success” } |