课程表

Spring 入门

Spring IoC 容器

Spring 依赖注入

Spring Beans 自动装配

Spring 基于注解的配置

Spring 框架

Spring 事务管理

Spring Web MVC 框架

工具箱
速查手册

基于 AOP 的 XML架构

当前位置:免费教程 » Java相关 » Spring

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
  9.  
  10. <!-- bean definition & AOP specific configuration -->
  11.  
  12. </beans>

你还需要在你的应用程序的 CLASSPATH 中使用以下 AspectJ 库文件。这些库文件在一个 AspectJ 装置的 ‘lib’ 目录中是可用的,否则你必须从网上下载并引用。

  • aspectjrt.jar

  • aspectjweaver.jar

  • aspectj.jar

  • aopalliance.jar

 声明一个 aspect

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

  1. <aop:config>
  2. <aop:aspect id="myAspect" ref="aBean">
  3. ...
  4. </aop:aspect>
  5. </aop:config>
  6. <bean id="aBean" class="...">
  7. ...
  8. </bean>

这里,“aBean” 将被配置和依赖注入,就像前面的章节中你看到的其他的 Spring bean 一样。

声明一个切入点

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

  1. <aop:config>
  2. <aop:aspect id="myAspect" ref="aBean">
  3. <aop:pointcut id="businessService"
  4. expression="execution(* com.xyz.myapp.service.*.*(..))"/>
  5. ...
  6. </aop:aspect>
  7. </aop:config>
  8. <bean id="aBean" class="...">
  9. ...
  10. </bean>

下面的示例定义了一个名为 “businessService” 的切入点,该切入点将与 com.tutorialspoint 包下的 Student 类中的 getName() 方法相匹配:

  1. <aop:config>
  2. <aop:aspect id="myAspect" ref="aBean">
  3. <aop:pointcut id="businessService"
  4. expression="execution(* com.tutorialspoint.Student.getName(..))"/>
  5. ...
  6. </aop:aspect>
  7. </aop:config>
  8. <bean id="aBean" class="...">
  9. ...
  10. </bean>

声明建议

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

  1. <aop:config>
  2. <aop:aspect id="myAspect" ref="aBean">
  3. <aop:pointcut id="businessService"
  4. expression="execution(* com.xyz.myapp.service.*.*(..))"/>
  5. <!-- a before advice definition -->
  6. <aop:before pointcut-ref="businessService"
  7. method="doRequiredTask"/>
  8. <!-- an after advice definition -->
  9. <aop:after pointcut-ref="businessService"
  10. method="doRequiredTask"/>
  11. <!-- an after-returning advice definition -->
  12. <!--The doRequiredTask method must have parameter named retVal -->
  13. <aop:after-returning pointcut-ref="businessService"
  14. returning="retVal"
  15. method="doRequiredTask"/>
  16. <!-- an after-throwing advice definition -->
  17. <!--The doRequiredTask method must have parameter named ex -->
  18. <aop:after-throwing pointcut-ref="businessService"
  19. throwing="ex"
  20. method="doRequiredTask"/>
  21. <!-- an around advice definition -->
  22. <aop:around pointcut-ref="businessService"
  23. method="doRequiredTask"/>
  24. ...
  25. </aop:aspect>
  26. </aop:config>
  27. <bean id="aBean" class="...">
  28. ...
  29. </bean>

你可以对不同的建议使用相同的 doRequiredTask 或者不同的方法。这些方法将会作为 aspect 模块的一部分来定义。

基于 AOP 的 XML 架构的示例

为了理解上面提到的基于 AOP 的 XML 架构的概念,让我们编写一个示例,可以实现几个建议。为了在我们的示例中使用几个建议,让我们使 Eclipse IDE 处于工作状态,然后按照如下步骤创建一个 Spring 应用程序:

步骤 描述
1 创建一个名为 SpringExample 的项目,并且在所创建项目的 src 文件夹下创建一个名为 com.tutorialspoint 的包。
2 使用 Add External JARs 选项添加所需的 Spring 库文件,就如在 Spring Hello World Example 章节中解释的那样。
3 在项目中添加 Spring AOP 指定的库文件 aspectjrt.jar, aspectjweaver.jaraspectj.jar
4 com.tutorialspoint 包下创建 Java 类 LoggingStudentMainApp
5 src 文件夹下创建 Beans 配置文件 Beans.xml
6 最后一步是创建所有 Java 文件和 Bean 配置文件的内容,并且按如下解释的那样运行应用程序。

这里是 Logging.java 文件的内容。这实际上是 aspect 模块的一个示例,它定义了在各个点调用的方法。

  1. package com.tutorialspoint;
  2. public class Logging {
  3. /**
  4. * This is the method which I would like to execute
  5. * before a selected method execution.
  6. */
  7. public void beforeAdvice(){
  8. System.out.println("Going to setup student profile.");
  9. }
  10. /**
  11. * This is the method which I would like to execute
  12. * after a selected method execution.
  13. */
  14. public void afterAdvice(){
  15. System.out.println("Student profile has been setup.");
  16. }
  17. /**
  18. * This is the method which I would like to execute
  19. * when any method returns.
  20. */
  21. public void afterReturningAdvice(Object retVal){
  22. System.out.println("Returning:" + retVal.toString() );
  23. }
  24. /**
  25. * This is the method which I would like to execute
  26. * if there is an exception raised.
  27. */
  28. public void AfterThrowingAdvice(IllegalArgumentException ex){
  29. System.out.println("There has been an exception: " + ex.toString());
  30. }
  31. }

下面是 Student.java 文件的内容:

  1. package com.tutorialspoint;
  2. public class Student {
  3. private Integer age;
  4. private String name;
  5. public void setAge(Integer age) {
  6. this.age = age;
  7. }
  8. public Integer getAge() {
  9. System.out.println("Age : " + age );
  10. return age;
  11. }
  12. public void setName(String name) {
  13. this.name = name;
  14. }
  15. public String getName() {
  16. System.out.println("Name : " + name );
  17. return name;
  18. }
  19. public void printThrowException(){
  20. System.out.println("Exception raised");
  21. throw new IllegalArgumentException();
  22. }
  23. }

下面是 MainApp.java 文件的内容:

  1. package com.tutorialspoint;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MainApp {
  5. public static void main(String[] args) {
  6. ApplicationContext context =
  7. new ClassPathXmlApplicationContext("Beans.xml");
  8. Student student = (Student) context.getBean("student");
  9. student.getName();
  10. student.getAge();
  11. student.printThrowException();
  12. }
  13. }

下面是配置文件 Beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
  9.  
  10. <aop:config>
  11. <aop:aspect id="log" ref="logging">
  12. <aop:pointcut id="selectAll"
  13. expression="execution(* com.tutorialspoint.*.*(..))"/>
  14. <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
  15. <aop:after pointcut-ref="selectAll" method="afterAdvice"/>
  16. <aop:after-returning pointcut-ref="selectAll"
  17. returning="retVal"
  18. method="afterReturningAdvice"/>
  19. <aop:after-throwing pointcut-ref="selectAll"
  20. throwing="ex"
  21. method="AfterThrowingAdvice"/>
  22. </aop:aspect>
  23. </aop:config>
  24.  
  25. <!-- Definition for student bean -->
  26. <bean id="student" class="com.tutorialspoint.Student">
  27. <property name="name" value="Zara" />
  28. <property name="age" value="11"/>
  29. </bean>
  30.  
  31. <!-- Definition for logging aspect -->
  32. <bean id="logging" class="com.tutorialspoint.Logging"/>
  33.  
  34. </beans>

一旦你已经完成的创建了源文件和 bean 配置文件,让我们运行一下应用程序。如果你的应用程序一切都正常的话,这将会输出以下消息:

  1. Going to setup student profile.
  2. Name : Zara
  3. Student profile has been setup.
  4. Returning:Zara
  5. Going to setup student profile.
  6. Age : 11
  7. Student profile has been setup.
  8. Returning:11
  9. Going to setup student profile.
  10. Exception raised
  11. Student profile has been setup.
  12. There has been an exception: java.lang.IllegalArgumentException
  13. .....
  14. other exception content

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
  9.  
  10. <aop:config>
  11. <aop:aspect id="log" ref="logging">
  12. <aop:pointcut id="selectAll"
  13. expression="execution(* com.tutorialspoint.Student.getName(..))"/>
  14. <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
  15. <aop:after pointcut-ref="selectAll" method="afterAdvice"/>
  16. </aop:aspect>
  17. </aop:config>
  18.  
  19. <!-- Definition for student bean -->
  20. <bean id="student" class="com.tutorialspoint.Student">
  21. <property name="name" value="Zara" />
  22. <property name="age" value="11"/>
  23. </bean>
  24.  
  25. <!-- Definition for logging aspect -->
  26. <bean id="logging" class="com.tutorialspoint.Logging"/>
  27.  
  28. </beans>

如果你想要执行通过这些更改之后的示例应用程序,这将会输出以下消息:

  1. Going to setup student profile.
  2. Name : Zara
  3. Student profile has been setup.
  4. Age : 11
  5. Exception raised
  6. .....
  7. other exception content
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号