先自定义一个类
在spring的xml文件中配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--注册bean--> <bean id="userService" class="com.fdw.UserServiceImpl"></bean> <bean id="log" class="com.fdw.Log"></bean> <bean id="afterLog" class="com.fdw.AfterLog"></bean> <!--配置aop,需要导入aop的约束--> <!--方式一:使用原生Spring Api接口--> <aop:config> <!--切入点:expression:表达式 excution(要执行的位置!* * * *)--> <aop:pointcut id="pointcut" expression="execution(* com.fdw.UserServiceImpl.*(..))"></aop:pointcut> <!--执行环绕增加--> <aop:advisor advice-ref="log" pointcut-ref="pointcut"></aop:advisor> <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"></aop:advisor> </aop:config> <!--方式二:自定义通知类--> <bean id="diy" class="com.fdw.DiyPointCut"></bean> <aop:config> <aop:aspect ref="diy"> <!--切入点--> <aop:pointcut id="point" expression="execution(* com.fdw.UserServiceImpl.*(..))"></aop:pointcut> <!--通知--> <aop:before method="before" pointcut-ref="point"></aop:before> <aop:after method="after" pointcut-ref="point"></aop:after> </aop:aspect> </aop:config> </beans>
测试结果: