第一步、在applicationContext-mvc.xml开启AOP注解扫描

<aop:aspectj-autoproxy/>

第二步、创建增强类,实现日志记录

@Component //把切面添加到spring容器中
@Aspect //变成切面类
public class WriteLog {
/**
*参数:代表目标方法对象
*
*/
public Object around(ProceedingJoinPoint pjp) throws Throwable {
//得到方法签名对象
        MethodSignature ms = (MethodSignature) pjp.getSignature();
        Method m = ms.getMethod(); //获得方法对象
        //判断方法上是否有@RequestMappering注解
        if(m.isAnnotationPresent(RequestMapping.class)){
            //获取此@RequestMappering注解
            RequestMapping mapping = m.getAnnotation(RequestMapping.class);
            System.out.println(new Date());
            System.out.println(request.getRemoteAddr()); //获取客户机IP地址
            System.out.println(m.getName());//保存方法名
            System.out.println(mapping.name()); //获取注解的name属性值
        }
Object object = pjp.proceed(); //执行真实的方法
return object;
}
}

第三步、配置切入点和切面

    <bean id="advice" class="WriteLog"/>
<aop:config>
<aop:pointcut id="pt" expression="execution(* com.alibaba.controller.*.*.*.(..))"/>
<aop:aspect ref="advice" >
<aop:before method="around" pointcut-ref="pt"/>
</aop:aspect>
</aop:config>

第四步、测试即可

05-12 13:59