戴着假发的程序员出品 抖音ID:戴着假发的程序员 欢迎关注
AOP-XML-pointcut配置
spring应用手册(第四部分)
在这里提前说明一下,切入点的表达式配置在注解方式中已经做了详细讲解,
在注解的方式我们可以使用内联的方式进行切入点的配置,也可以使用@pointcut进行切入点声明,方便重复使用。
在XML配置中同样可以使用内联的方式进行配置:
例如:
这个方式就是我们Helloworld程序中使用的方式。
我们也可以在XML申明切入点,方便重复使用:
案例:
我们在Aspect中在添加一个方法:
/**
* @author 戴着假发的程序员
* @company http://www.boxuewa.com
* @description
*/
public class DkAspect {
//前置通知
public void before(){
System.out.println("前置通知");
}
//其他通知
public void otherBefore(){
System.out.println("其他前置通知");
}
}
配置修改如下:
<!-- AOP配置 -->
<aop:config>
<!-- 申明AspectBean,引用我们注册的dkAspect -->
<aop:aspect id="aspect" ref="dkAspcet">
<!-- 声明一个切入点,命名为pointcut1 -->
<aop:pointcut id="pointcut1"
expression="execution(* com.st.beans..*.*(..))"/>
<!--配置两个前置通知,利用pointcut-ref引用上面申明的切入点-->
<aop:before method="before" pointcut-ref="pointcut1"/>
<aop:before method="otherBefore" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
我们可以使用aop:pointcut标签申明一个切入点。然后在aop:before中使用pointcut-ref引用前面申明的切入点。
测试:
ApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext-demo9.xml");
MessageBean bean = ac.getBean(MessageBean.class);
bean.printMessage("假发的穿戴技巧");
结果: