9.3.Spring 中基于 AOP 的 XML架构

来源:互联网 发布:用友数据库重装 编辑:程序博客网 时间:2024/06/06 02:48

(以下内容引用自https://www.w3cschool.cn/wkspring/omps1mm6.html)

Spring 中基于 AOP 的 XML架构

为了在本节的描述中使用 aop 命名空间标签,你需要导入 spring-aop j架构,如下所述:

<?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-3.0.xsd     http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">   <!-- bean definition & AOP specific configuration --></beans>

你还需要在你的应用程序的 CLASSPATH 中使用以下 AspectJ 库文件。这些库文件在一个 AspectJ 装置的 ‘lib’ 目录中是可用的,否则你可以在 Internet 中下载它们。

  • aspectjrt.jar

  • aspectjweaver.jar

  • aspectj.jar

  • aopalliance.jar

声明一个 aspect

一个 aspect 是使用 元素声明的,支持的 bean 是使用 ref 属性引用的,如下所示:

<aop:config>   <aop:aspect id="myAspect" ref="aBean">   ...   </aop:aspect></aop:config><bean id="aBean" class="...">...</bean>
这里,“aBean” 将被配置和依赖注入,就像前面的章节中你看到的其他的 Spring bean 一样。

声明一个切入点

一个切入点有助于确定使用不同建议执行的感兴趣的连接点(即方法)。在处理基于配置的 XML 架构时,切入点将会按照如下所示定义:

<aop:config>   <aop:aspect id="myAspect" ref="aBean">   <aop:pointcut id="businessService"   expression="execution(* com.xyz.myapp.service.*.*(..))"/>   ...   </aop:aspect></aop:config><bean id="aBean" class="...">...</bean>
下面的示例定义了一个名为 “businessService” 的切入点,该切入点将与 com.tutorialspoint 包下的 Student 类中的 getName() 方法相匹配:
<aop:config>   <aop:aspect id="myAspect" ref="aBean">   <aop:pointcut id="businessService"  expression="execution(* com.tutorialspoint.Student.getName(..))"/>   ...   </aop:aspect></aop:config><bean id="aBean" class="...">...</bean>

声明建议

你可以使用 <aop:{ADVICE NAME}> 元素在一个 中声明五个建议中的任何一个,如下所示:

<aop:config>   <aop:aspect id="myAspect" ref="aBean">      <aop:pointcut id="businessService"         expression="execution(* com.xyz.myapp.service.*.*(..))"/>      <!-- a before advice definition -->      <aop:before pointcut-ref="businessService"          method="doRequiredTask"/>      <!-- an after advice definition -->      <aop:after pointcut-ref="businessService"          method="doRequiredTask"/>      <!-- an after-returning advice definition -->      <!--The doRequiredTask method must have parameter named retVal -->      <aop:after-returning pointcut-ref="businessService"         returning="retVal"         method="doRequiredTask"/>      <!-- an after-throwing advice definition -->      <!--The doRequiredTask method must have parameter named ex -->      <aop:after-throwing pointcut-ref="businessService"         throwing="ex"         method="doRequiredTask"/>      <!-- an around advice definition -->      <aop:around pointcut-ref="businessService"          method="doRequiredTask"/>   ...   </aop:aspect></aop:config><bean id="aBean" class="...">...</bean>
你可以对不同的建议使用相同的 doRequiredTask 或者不同的方法。这些方法将会作为 aspect 模块的一部分来定义。

基于 AOP 的 XML 架构的示例

1.结构目录


2.代码

Logging.java(实际上是 aspect 模块的一个示例,它定义了在各个点调用的方法。)

package com.tutorialspoint;public class Logging {   /**     * This is the method which I would like to execute    * before a selected method execution.    */   public void beforeAdvice(){      System.out.println("Going to setup student profile.");   }   /**     * This is the method which I would like to execute    * after a selected method execution.    */   public void afterAdvice(){      System.out.println("Student profile has been setup.");   }   /**     * This is the method which I would like to execute    * when any method returns.    */   public void afterReturningAdvice(Object retVal){      System.out.println("Returning:" + retVal.toString() );   }   /**    * This is the method which I would like to execute    * if there is an exception raised.    */   public void AfterThrowingAdvice(IllegalArgumentException ex){      System.out.println("There has been an exception: " + ex.toString());      }  }

Student.java

package com.tutorialspoint;public class Logging {   /**     * This is the method which I would like to execute    * before a selected method execution.    */   public void beforeAdvice(){      System.out.println("Going to setup student profile.");   }   /**     * This is the method which I would like to execute    * after a selected method execution.    */   public void afterAdvice(){      System.out.println("Student profile has been setup.");   }   /**     * This is the method which I would like to execute    * when any method returns.    */   public void afterReturningAdvice(Object retVal){      System.out.println("Returning:" + retVal.toString() );   }   /**    * This is the method which I would like to execute    * if there is an exception raised.    */   public void AfterThrowingAdvice(IllegalArgumentException ex){      System.out.println("There has been an exception: " + ex.toString());      }  }

MainApp.java 

package test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp {   public static void main(String[] args) {      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");      Student student = (Student) context.getBean("student");      student.getName();      student.getAge();            student.printThrowException();   }}

配置文件 Beans.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-3.0.xsd     http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">   <aop:config>      <aop:aspect id="log" ref="logging">         <aop:pointcut id="selectAll"          expression="execution(* com.tutorialspoint.*.*(..))"/>         <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>         <aop:after pointcut-ref="selectAll" method="afterAdvice"/>         <aop:after-returning pointcut-ref="selectAll"                               returning="retVal"                              method="afterReturningAdvice"/>         <aop:after-throwing pointcut-ref="selectAll"                              throwing="ex"                             method="AfterThrowingAdvice"/>      </aop:aspect>   </aop:config>   <!-- Definition for student bean -->   <bean id="student" class="com.tutorialspoint.Student">      <property name="name"  value="Zara" />      <property name="age"  value="11"/>         </bean>   <!-- Definition for logging aspect -->   <bean id="logging" class="com.tutorialspoint.Logging"/> </beans>

运行Mainapp.java 输出以下消息:

Going to setup student profile.Name : ZaraStudent profile has been setup.Returning:ZaraGoing to setup student profile.Age : 11Student profile has been setup.Returning:11Going to setup student profile.Exception raisedStudent profile has been setup.There has been an exception: java.lang.IllegalArgumentException.....other exception content


让我们来解释一下上面定义的在 com.tutorialspoint 中 选择所有方法的 。让我们假设一下,你想要在一个特殊的方法之前或者之后执行你的建议,你可以通过替换使用真实类和方法名称的切入点定义中的星号(*)来定义你的切入点来缩短你的执行。

<?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-3.0.xsd     http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">   <aop:config>   <aop:aspect id="log" ref="logging">      <aop:pointcut id="selectAll"       expression="execution(* com.tutorialspoint.Student.getName(..))"/>      <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>      <aop:after pointcut-ref="selectAll" method="afterAdvice"/>   </aop:aspect>   </aop:config>   <!-- Definition for student bean -->   <bean id="student" class="com.tutorialspoint.Student">      <property name="name"  value="Zara" />      <property name="age"  value="11"/>         </bean>   <!-- Definition for logging aspect -->   <bean id="logging" class="com.tutorialspoint.Logging"/> </beans>
如果你想要执行通过这些更改之后的示例应用程序,这将会输出以下消息:
Going to setup student profile.Name : ZaraStudent profile has been setup.Age : 11Exception raised.....other exception content



阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 没有嗅觉是怎么回事 嗅觉减退的原因 鼻子没有嗅觉的原因 鼻子嗅觉不灵敏咋回事 鼻炎导致嗅觉失灵 味觉嗅觉失灵的原因 鼻炎失去嗅觉怎么办 鼻子没嗅觉是什么原因 鼻炎会失去嗅觉吗 嗅觉丧失是几级伤残 嗅觉神经损伤能自行恢复吗 狗的嗅觉是人的多少倍 嗅觉时有时无 嗅觉下降是什么原因 失去味觉和嗅觉是什么原因 鼻子嗅觉感受器 没有味觉和嗅觉 嗅觉好的猎犬 鼻子闻不到味道 感冒鼻子失去嗅觉多久能恢复 鼻子有多少个嗅觉感受器 鸡嗉 鸡嗉子 嗉怎么读 鸡嗉子果 鸡嗉囊胀气土方法 怎么练嗓音 男人磁性嗓音速成法 嗓音变得干净空灵甜美 男士魅力嗓音训练 嗓音难听怎么改变 嗓音训练手册 如何改变嗓音 嗓音粗适合唱什么歌 怎样练嗓音能唱歌好听 嗓音不好听怎么办 怎样练好嗓音 怎么让嗓音变好听 唱歌怎么唱好听