Spring 中基于 AOP 的 XML架构

为了使用 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. <!-- bean definition & AOP specific configuration -->
  10. </beans>

确保项目中有如下四个库文件:

  • aspectjrt.jar

  • aspectjweaver.jar

  • aspectj.jar

  • aopalliance.jar

声明一个 aspect

一个 aspect 是使用 <aop:aspect></aop: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” 将被配置和依赖注入.

声明一个切入点

使用元素<aop:pointcut>声明一个切入点.

一个切入点有助于确定使用不同建议执行的感兴趣的连接点(即方法)。在处理基于配置的 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}> 元素在一个 中声明五个建议中的任何一个,如下所示:

  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>

基于 AOP 的 XML 架构的示例

  • 新建Spring项目

  • 在项目中添加 Spring AOP 指定的库文件 aspectjrt.jar,aspectjweaver.jar 和 aspectj.jar。

  • 创建 Java 类 Logging, Student 和 MainApp

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

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

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

  1. package hello;
  2. //import org.springframework.beans.factory.annotation.Autowired;
  3. public class Student {
  4. private int age;
  5. private String name;
  6. public void setAge(int age){
  7. this.age = age;
  8. }
  9. public int getAge(){
  10. System.out.println("age:"+age);
  11. return age;
  12. }
  13. public void setName(String name){
  14. this.name = name;
  15. }
  16. public String getName(){
  17. System.out.println("name:"+name);
  18. return name;
  19. }
  20. public void printThrowException(){
  21. System.out.println("Exception raised");
  22. throw new IllegalArgumentException();
  23. }
  24. }

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

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

下面是配置文件 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. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/aop
  9. http://www.springframework.org/schema/aop/spring-aop.xsd">
  10. <aop:config>
  11. <aop:aspect id="log" ref="logging">
  12. <aop:pointcut id="selectAll" expression="execution(* hello.*.*(..))"/>
  13. <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
  14. <aop:after pointcut-ref="selectAll" method="afterAdvice"/>
  15. <aop:after-returning pointcut-ref="selectAll"
  16. returning="retVal"
  17. method="afterReturningAdvice"/>
  18. <aop:after-throwing pointcut-ref="selectAll"
  19. throwing="ex"
  20. method="AfterThrowingAdvice"/>
  21. </aop:aspect>
  22. </aop:config>
  23. <!-- Definition for student bean -->
  24. <bean id="student" class="hello.Student">
  25. <property name="name" value="番茄"/>
  26. <property name="age" value="10"/>
  27. </bean>
  28. <!-- Definition for logging aspect -->
  29. <bean id="logging" class="hello.Logging">
  30. </bean>
  31. </beans>

运行一下应用程序

  1. Going to setup student profile.
  2. name:番茄
  3. Student profile has been setup
  4. Returning:番茄
  5. Going to setup student profile.
  6. age:10
  7. Student profile has been setup
  8. Returning:10
  9. Going to setup student profile.
  10. Exception raised
  11. Student profile has been setup

注意:一定要保证项目下有库文件aspectjrt.jar和aspectjweaver.jar,否则会报错。

每天学习一点点,每天进步一点点。

Spring 中基于 AOP 的 XML架构的更多相关文章

  1. Spring中基于AOP的XML架构

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/aop-with-spring-framenwork/xml-schema-based-aop-wi ...

  2. Spring 中基于 AOP 的 @AspectJ

    Spring 中基于 AOP 的 @AspectJ @AspectJ 作为通过 Java 5 注释注释的普通的 Java 类,它指的是声明 aspects 的一种风格. 通过在你的基于架构的 XML ...

  3. Spring 中基于 AOP 的 @AspectJ注解实例

    @AspectJ 作为通过 Java 5 注释注释的普通的 Java 类,它指的是声明 aspects 的一种风格.通过在你的基于架构的 XML 配置文件中包含以下元素,@AspectJ 支持是可用的 ...

  4. Spring中基于AOP的@AspectJ

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/aop-with-spring-framenwork/aspectj-based-aop-with- ...

  5. spring中的aop的xml配置方式简单实例

    aop,即面向切面编程,面向切面编程的目标就是分离关注点,比如:一个骑士只需要关注守护安全,或者远征,而骑士辉煌一生的事迹由谁来记录和歌颂呢,当然不会是自己了,这个完全可以由诗人去歌颂,比如当骑士出征 ...

  6. spring中基于aop使用ehcache

    我就是对着这个博客看的 http://www.cnblogs.com/ctxsdhy/p/6421016.html

  7. Spring中基于xml的AOP

    1.Aop 全程是Aspect Oriented Programming 即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.Aop是oop的延续,是软件开发中的 一个热点 ...

  8. Spring 框架的概述以及Spring中基于XML的IOC配置

    Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...

  9. spring中基于注解使用AOP

    本文内容:spring中如何使用注解实现面向切面编程,以及如何使用自定义注解. 一个场景 比如用户登录,每个请求发起之前都会判断用户是否登录,如果每个请求都去判断一次,那就重复地做了很多事情,只要是有 ...

随机推荐

  1. Spring Boot的TestRestTemplate使用

    文章目录 添加maven依赖 TestRestTemplate VS RestTemplate 使用Basic Auth Credentials 使用HttpClientOption 使用RestTe ...

  2. python- 函数高级

    函数高级 一.默认参数 1.默认参数概念 默认参数指函数/方法在定义时为形参赋值,对应的形参称为默认参数. 默认参数是一个参数定义期的概念,与调用无关. 2.默认参数的作用 如果参数定义默认参数,在调 ...

  3. 获取系统DPI、系统显示比例等

    using System; using System.Drawing; using System.Runtime.InteropServices; namespace XYDES { public c ...

  4. Java 数组 之 二维数组

    转载于 : http://www.verejava.com/?id=16992693216433 public class BinaryArray { public static void main( ...

  5. 数学--数论--HDU6919 Senior PanⅡ【2017多校第九场】

    Description 给出一个区间[L,R][L,R],问该区间中所有以KK作为最小因子(大于11的)的数字之和 Input 第一行输入一整数TT表示用例组数,每组用例输入三个整数L,R,KL,R, ...

  6. C# 多线程(18):一篇文章就理解async和await

    目录 前言 async await 从以往知识推导 创建异步任务 创建异步任务并返回Task 异步改同步 说说 await Task 说说 async Task 同步异步? Task封装异步任务 关于 ...

  7. javaweb系统调优方案

    1. java代码优化 java代码优化6大原则 : https://blog.csdn.net/bunny1024/article/details/72803708 java代码优化: https: ...

  8. 使用Golang + lua实现一个值班机器人

    我们在的项目组呢,有一项工作是,收邮件(很大程度上使用邮件是为了存个底),然后从我们的系统里边查一下相关信息,然后回复个邮件的工作.虽然工作量并不大,但是会把时间切的稀碎.为了拯救我的时间,所以做了一 ...

  9. STC8A8K64S4A12通过SPI接口操作基于ST7920的LCD12864液晶模块

    文章地址:https://www.cnblogs.com/jqdy/p/12665430.html 1. 硬件连接 1.1 64引脚的STC8A8K64S4A12 使用的是最小核心板,所以引脚皆引出可 ...

  10. 03_CSS入门和高级技巧(1)

    上节课知识的复习 插入图片,页面中能够插入的图片类型:jpg.jpeg.bmp.png.gif:不能的psd.fw. 语法: <img src="路径" alt=" ...