Spring AOP 前置增强拦截不到

395 阅读1分钟

最近在用AOP写一个在添加操作前统一配置创建人创建时间等基本信息的功能,但是发现无论如何都拦截不到该有的请求

<aop:aspect ref="createByHandler">
		<!-- .. 当前包和子包-->
		<aop:pointcut expression="execution(* com.isoft.edu.api..*ServiceImpl.persist(..))" id="beforeAdd" />
		<aop:before method="beforeAdd" pointcut-ref="beforeAdd" />
	</aop:aspect>

后来去看了aop原理的相关内容,发现原来spring aop默认的配置是面向接口编程的,所以定义pointcut的时候应该定义接口,而不是实现类

<aop:aspect ref="createByHandler">
		<!-- .. 当前包和子包-->
		<aop:pointcut expression="execution(* com.isoft.edu.api..*Service.persist(..))" id="beforeAdd" />
		<aop:before method="beforeAdd" pointcut-ref="beforeAdd" />
	</aop:aspect>

代码做了一些修改,如上图,问题就迎刃而解了