了解Spring AOP [英] Understanding Spring AOP

查看:63
本文介绍了了解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.

这个想法是,您无需定义要在源代码中修改的所有代码点并手动对其进行修改,而是可以定义规则来确定如何在代码库中找到感兴趣的点以及您想要的修饰点对他们做.这些规则称为方面(AOP的 A ).

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天全站免登陆