1、 spring-ocntent.xml中添加配置:
<!-- 开启aop注解方式 -->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
<!-- 搜索spring控件 -->
<context:component-scan base-package="com.prh.log"></context:component-scan>
base-package="com.prh.log"中的 com.prh.log是Aspectj类所在的包名称,意思是要找个包里的类要进行注释扫描
2、添加Aspectj类:
package com.prh.log;
import org.apache.log4j.Logger;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogInterceptor {
private final Logger logger = Logger.getLogger(LogInterceptor.class);
@Before(value = "execution(* com.prh.controller.*.*(..))")
public void before(){
logger.info("login start!");
}
@After(value = "execution(* com.prh.controller.*.*(..))")
public void after(){
logger.info("login end!");
}
}
value = "execution(* com.prh.controller.*.*(..))"意思是切入点为com.prh.controller包下的所有类的所有方法