1、在spring配置文件加入支持@AspectJ标记,如下:
<aop:aspectj-autoproxy/>
2、配置一个日志操作bean,可以像普通bean一样注解以便spring管理,但是要加上@Aspect以便Spring将他作为切面累搜索到,如下:
@Aspect @Component("logAspect") public class LogAspect extends BaseAction{ /** * */ }
3、定义切入点和方法
@Pointcut("execution(* *.*.*.service..*.add*(..)) || execution(* *.*.*.service..*.modify*(..)) ") public void insertPointcut() { /** * */ }
4、在具体处理方法上添加建议
@Before("insertPointcut()")
public void saveLog(JoinPoint joinPoint) throws Exception{ /** * */ }OK,到这一步,service子包中的所有类的所有以add和modify方法被调用时,都会调用日志处理方法saveLog。