理解 Spring AOP [英] Understanding Spring AOP

查看:38
本文介绍了理解 Spring AOP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring 3.0 框架,但我还是个新手.谁能通俗的解释一下什么是 AOP 编程?(一个简短的例子肯定会有帮助)

I am working with Spring 3.0 framework and still a novice. Can anyone explain me in layman terms what is AOP programming? (a short example will definitely help)

Spring 如何整合/增强/支持它?

How does Spring incorporate/enhance/support it?

推荐答案

AOP 是一种修改代码库中现有类的方法,以根据单独定义的规则修饰它们或改变它们的行为.这种修改可以在将类放入 jar/war 之前完成,也可以在加载代码时动态进行.

AOP is a way to modify existing classes in a code base to embellish them or change their behavior based on rules defined separately. This modification can be done before the classes are put into a jar/war, or can happen dynamically while the code is being loaded.

这个想法是,与其在源代码中找到您想要修改的所有代码点并手动修改它们,不如定义如何在代码库中找到兴趣点的规则,以及您想要的修饰对他们做.这些规则称为方面(AOPA).

The idea is, rather than finding all the points of code that you want to modify in the source code and hand modifying them, you define rules for how to find points of interest in the code base, and what embellishments you would like to do to them. These rules are called aspects (the A of AOP).

典型的例子是你想在你的代码库中获取有关各种方法的一些时间信息.你可以去找到所有感兴趣的方法,然后在顶部调用

The prototypical example is you want to get some timing information on various methods in your code base. You could go find all the methods of interest and at the top put a call to

long start = System.currentTimeMillis();

最后做

long end = System.currentTimeMillis();
System.out.println("Method time is: " + (end - start));

但那是:

  1. 可能是一堆工作
  2. 临时的,你不想弄乱你的代码库

您可以改为定义方面,说明您要修改哪些方法,以及您要在这些方法的开头和结尾执行什么操作.

You can instead define aspects that say what methods you want to modify, and that you want to do stuff at the beginning and end of these methods.

当应用 AOP 时,无论是在 jar 创建时,还是在类加载时,就好像您最初以这种方式编写类一样.

When the AOP is applied, either at jar creation time, or class loading time, it's as if you wrote the classes that way originally.

这篇关于理解 Spring AOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆