Spring 在XML中声明切面/AOP

在Spring的AOP配置命名空间中,我们可以找到声明式切面选择,看下面:

<aop:config><!– AOP定义开始 –><aop:pointcut/><!– 定义切入点 –><aop:advisor/><!– 定义AOP通知器 –><aop:aspect><!– 定义切面开始 –><aop:pointcut/><!– 定义切入点 –><aop:before/><!– 前置通知 –><aop:after-returning/><!– 后置返回通知 –><aop:after-throwing/><!– 后置异常通知 –><aop:after/><!– 后置通知(不管通知的方法是否执行成功) –><aop:around/><!– 环绕通知 –><aop:declare-parents/><!– 引入通知 –></aop:aspect><!– 定义切面结束 –></aop:config><!– AOP定义结束 –>一、声明切面

切面就是包含切入点和通知的对象,在Spring容器中将被定义为一个Bean,Schema方式的切面需要一个切面支持Bean,该支持Bean的字段和方法提供了切面的状态和行为信息,并通过配置方式来指定切入点和通知实现。

切面使用<aop:aspect>标签指定,ref属性用来引用切面支持Bean。

切面支持Bean“aspectSupportBean”跟普通Bean完全一样使用,切面使用“ref”属性引用它。

二、 声明切入点

切入点在Spring中也是一个Bean,Bean定义方式可以有很三种方式:

1)在<aop:config>标签下使用<aop:pointcut>声明一个切入点Bean,该切入点可以被多个切面使用,对于需要共享使用的切入点最好使用该方式,该切入点使用id属性指定Bean名字,在通知定义时使用pointcut-ref属性通过该id引用切入点,expression属性指定切入点表达式:

<aop:config><aop:pointcut expression="execution(* cn.com.ztz.spring.service.ShowServiceImpl.*(..))" id="pointcut"/><aop:aspect ref="audienceAspect" ><aop:before pointcut-ref="pointcut" method="taskSeats"/></aop:aspect></aop:config>

<aop:config><aop:aspect ref="audienceAspect" ><aop:pointcut expression="execution(* cn.com.ztz.spring.service.ShowServiceImpl.*(..))" id="pointcut"/><aop:before pointcut-ref="pointcut" method="taskSeats"/></aop:aspect></aop:config>

<aop:config><aop:aspect ref="audienceAspect" ><aop:before pointcut="execution(* cn.com.ztz.spring.service.ShowServiceImpl.*(..))" method="taskSeats"/></aop:aspect></aop:config>三、 声明通知

直接来看个例子:

package cn.com.ztz.spring.service;public interface ShowService {public void show();}package cn.com.ztz.spring.service;public class ShowServiceImpl implements ShowService{@Overridepublic void show() {showBefore();//showError();//异常测试showEnd();}public void showBefore(){System.out.println("showBefore============");}public void showError(){System.out.println("showError============");throw new RuntimeException();}public void showEnd(){System.out.println("showEnd===============");}}package cn.com.ztz.spring.service;public class AudienceAspect {public void taskSeats(){System.out.println("等候节目开始===");}public void applaud(){System.out.println("鼓掌=========");}public void demandRefund(){System.out.println("退钱离场======");}} <bean id="showService" class="cn.com.ztz.spring.service.ShowServiceImpl"/><bean id="audienceAspect" class="cn.com.ztz.spring.service.AudienceAspect"/><aop:config><aop:pointcut id="pointcut" expression="execution(* cn.com.ztz.spring.service.ShowServiceImpl.*(..))"/><aop:aspect ref="audienceAspect" ><aop:before pointcut-ref="pointcut" method="taskSeats"/><aop:after-returning pointcut-ref="pointcut" method="applaud"/><aop:after-throwing pointcut-ref="pointcut" method="demandRefund"/></aop:aspect></aop:config>public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");ShowService hs = ctx.getBean("showService", ShowService.class);System.out.println("======================================");hs.show();System.out.println("======================================");}控制台输出结果:

======================================等候节目开始===showBefore============showEnd===============鼓掌===============================================四、 为通知传递参数

现在有很多人在观看演出,我想传入一个人姓名,怎么办了,来看下:

public interface ShowService {public void showBefore(String param);}public class ShowServiceImpl implements ShowService{public void showBefore(String param){System.out.println("showBefore============");}}<pre name="code" class="java">public class AudienceAspect {public void taskSeats(String param){System.out.println(param+",等候节目开始===");}} <bean id="showService" class="cn.com.ztz.spring.service.ShowServiceImpl"/><bean id="audienceAspect" class="cn.com.ztz.spring.service.AudienceAspect"/><aop:config><aop:aspect ref="audienceAspect" ><aop:before pointcut="execution(* cn.com.ztz.spring.service.ShowServiceImpl.*(..)) and args(param)"method="taskSeats(java.lang.String)"arg-names="param"/></aop:aspect></aop:config>public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");ShowService hs = ctx.getBean("showService", ShowService.class);System.out.println("======================================");hs.showBefore("张三");System.out.println("======================================");}控制台输出结果:

关于爱情的句子:情不知所起,一往而情深。

Spring 在XML中声明切面/AOP

相关文章:

你感兴趣的文章:

标签云: