﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-礼必风-随笔分类-AOP技术杂谈</title><link>http://www.blogjava.net/bily/category/17255.html</link><description>IT不等于it</description><language>zh-cn</language><lastBuildDate>Thu, 08 Mar 2007 01:16:23 GMT</lastBuildDate><pubDate>Thu, 08 Mar 2007 01:16:23 GMT</pubDate><ttl>60</ttl><item><title>JBOSS AOP学习笔记</title><link>http://www.blogjava.net/bily/archive/2006/11/17/81744.html</link><dc:creator>礼必风</dc:creator><author>礼必风</author><pubDate>Fri, 17 Nov 2006 04:59:00 GMT</pubDate><guid>http://www.blogjava.net/bily/archive/2006/11/17/81744.html</guid><wfw:comment>http://www.blogjava.net/bily/comments/81744.html</wfw:comment><comments>http://www.blogjava.net/bily/archive/2006/11/17/81744.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/bily/comments/commentRss/81744.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/bily/services/trackbacks/81744.html</trackback:ping><description><![CDATA[
		<p>    面向方面编程(AOP)是一种新的规范，允许你达到以前面对对象方法无法达到的组织和分层你的应用程序的方法. 方面允许你很明显的把功能性的东西粘合起来，所以你就可以很方便的为程序设计很多的层。AOP可以拦截所有JAVA程序中的事件触发。</p>
		<p>    什么是AOP？</p>
		<p>    一个方面（aspect)就是一个共有的特性，具有此代表性的有：横向分离的方法，类，对象层次或者实体对象模型。它们看起来应该是组合在一起的，但是在AOP里面你不用像以前面对对象（OO）那样来组织它们了。</p>
		<p>    在传统JAVA中要加入计算时间的代码到你的应用中，你必须按以下方式：</p>
		<p>public class BankAccountDAO{</p>
		<p> public void withdraw(double amount){</p>
		<p>  long startTime = System.currentTimeMillis();</p>
		<p>  try  {</p>
		<p>    // Actual method body...</p>
		<p>  }</p>
		<p>  finally  {</p>
		<p>    long endTime = System.currentTimeMillis() - startTime;</p>
		<p>    System.out.println("withdraw took: "   endTime);</p>
		<p>  }</p>
		<p> }</p>
		<p>}</p>
		<p>    我们可以列举这里面存在的几个问题：</p>
		<p>    1。如果你要在每个你的方法中都加入这样的代理，无疑是十分糟糕的，特别是有try/catch这样的语句。</p>
		<p>    2。这里有很多代码都不是你真实需要用到的，这样就使你的程序代码十分臃肿，读起来也十分困难。而且你不得不把你的代码放在try里面......<br />    <br />    3。如果你要扩展这段代码，我们可以遇见到那将是十分困难的工作。</p>
		<p>    所以可见，这样的代码是十分难于维护，扩展和继承的，因为在这里面有很多东西分散了你对你这段代码真正要实现的东西的注意力。而且这只不过是一段最简单的例子，在真正的OOP中是很难实现对以上代码更好的表现方法的。</p>
		<p>    面对方面编程可以分离你的这些功能性，可以让你增加行为来围绕你的功能代码。例如上面的，AOP就可以使在执行你自己的代码前你可以控制执行其它的功能。</p>
		<p>    所有实现AOP的框架都有两种方式：组装关注点 和 a programmatic construct(编程实现)</p>
		<p>    JBOSS的一个横向切入关注点</p>
		<p>01. public class Metrics implements org.jboss.aop.Interceptor</p>
		<p>02. {</p>
		<p>03.   public Object invoke(Invocation invocation) throws Throwable</p>
		<p>04.   {</p>
		<p>05.   long startTime = System.currentTimeMillis();</p>
		<p>06.   try</p>
		<p>07.   {</p>
		<p>08.     return invocation.invokeNext();</p>
		<p>09.   }</p>
		<p>10.   finally</p>
		<p>11.   {</p>
		<p>12.     long endTime = System.currentTimeMillis() - startTime;</p>
		<p>13.     java.lang.reflect.Method m = ((MethodInvocation)invocation).method;</p>
		<p>14.     System.out.println("method "   m.toString()   " time: "   endTime   "ms");</p>
		<p>15.   }</p>
		<p>16.  }</p>
		<p>17. }</p>
		<p>    真正实现的功能代码是在8行调用了，这就是实现了组装关注点，使之成为了一个方面。这让我们在以后扩展实际功能的时候就十分方便了，只需要去修改具体的实现方法，而不用去关心其它关注点了。</p>
		<p>    JBOOS中具体应用这个方面</p>
		<p>    需要定义一个切入点(pointcuts)，全部通过政则表达式来实现。</p>
		<p>    Listing Three: Defining a pointcut in JBoss AOP</p>
		<p>1. &lt;bind pointcut="public void com.mc.BankAccountDAO-&gt;withdraw(double amount)"&gt;</p>
		<p>2.     &lt;interceptor class="com.mc.Metrics"/&gt;</p>
		<p>3. &lt;/bind &gt;</p>
		<p>4. &lt;bind pointcut="* com.mc.billing.*-&gt;*(..)"&gt;</p>
		<p>5.     &lt;interceptor class="com.mc.Metrics"/&gt;</p>
		<p>6. &lt;/bind &gt;</p>
		<p>    1-3定义的一个切入点的方法就是 BankAccountDAO-&gt;withdraw(double amount)</p>
		<p>    4-6定义的是一个通用的，它的切入点是所有的com.mc.billing.下面的类的方法。</p>
<img src ="http://www.blogjava.net/bily/aggbug/81744.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/bily/" target="_blank">礼必风</a> 2006-11-17 12:59 <a href="http://www.blogjava.net/bily/archive/2006/11/17/81744.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Spring AOP中文教程</title><link>http://www.blogjava.net/bily/archive/2006/11/17/81742.html</link><dc:creator>礼必风</dc:creator><author>礼必风</author><pubDate>Fri, 17 Nov 2006 04:56:00 GMT</pubDate><guid>http://www.blogjava.net/bily/archive/2006/11/17/81742.html</guid><wfw:comment>http://www.blogjava.net/bily/comments/81742.html</wfw:comment><comments>http://www.blogjava.net/bily/archive/2006/11/17/81742.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/bily/comments/commentRss/81742.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/bily/services/trackbacks/81742.html</trackback:ping><description><![CDATA[
		<p>    AOP正在成为软件开发的下一个圣杯。使用AOP，你可以将处理aspect的代码注入主程序，通常主程序的主要目的并不在于处理这些aspect。AOP可以防止代码混乱。<br />    为了理解AOP如何做到这点，考虑一下记日志的工作。日志本身不太可能是你开发的主程序的主要任务。如果能将“不可见的”、通用的日志代码注入主程序中，那该多好啊。AOP可以帮助你做到。<br />    Spring framework是很有前途的AOP技术。作为一种非侵略性的，轻型的AOP framework，你无需使用预编译器或其他的元标签，便可以在Java程序中使用它。这意味着开发团队里只需一人要对付AOP framework，其他人还是象往常一样编程。<br />    AOP是很多直觉难以理解的术语的根源。幸运的是，你只要理解三个概念，就可以编写AOP模块。这三个概念是：advice，pointcut和 advisor。advice是你想向别的程序内部不同的地方注入的代码。pointcut定义了需要注入advice的位置，通常是某个特定的类的一个 public方法。advisor是pointcut和advice的装配器，是将advice注入主程序中预定义位置的代码。</p>
		<p>    既然我们知道了需要使用advisor向主要代码中注入“不可见的”advice，让我们实现一个Spring AOP的例子。在这个例子中，我们将实现一个before advice，这意味着advice的代码在被调用的public方法开始前被执行。以下是这个before advice的实现代码：</p>
		<p>    代码:<br />package com.company.springaop.test;</p>
		<p>import java.lang.reflect.Method;<br />import org.springframework.aop.MethodBeforeAdvice;</p>
		<p>public class TestBeforeAdvice implements MethodBeforeAdvice {</p>
		<p>public void before(Method m, Object[] args, Object target)<br />throws Throwable {<br />  System.out.println("Hello world! (by "<br />    + this.getClass().getName()<br />    + ")");<br />}<br />}</p>
		<p>    接口MethodBeforeAdvice只有一个方法before需要实现，它定义了advice的实现。before方法共用三个参数，它们提供了相当丰富的信息。参数Method m是advice开始后执行的方法。方法名称可以用作判断是否执行代码的条件。Object[] args是传给被调用的public方法的参数数组。当需要记日志时，参数args和被执行方法的名称，都是非常有用的信息。你也可以改变传给m的参数，但要小心使用这个功能；编写最初主程序的程序员并不知道主程序可能会和传入参数的发生冲突。Object target是执行方法m对象的引用。</p>
		<p>    在下面的BeanImpl类中，每个public方法调用前，都会执行advice：</p>
		<p>    代码:<br />package com.company.springaop.test;</p>
		<p>public class BeanImpl implements Bean {</p>
		<p>public void theMethod() {<br />  System.out.println(this.getClass().getName()<br />    + "." + new Exception().getStackTrace()[0].getMethodName()<br />    + "()"<br />    + " says HELLO!");<br />}<br />}</p>
		<p>    类BeanImpl实现了下面的接口Bean：</p>
		<p>    代码:<br />package com.company.springaop.test;</p>
		<p>public interface Bean {<br />public void theMethod();<br />}</p>
		<p>    虽然不是必须使用接口，但面向接口而不是面向实现编程是良好的编程实践，Spring也鼓励这样做。</p>
		<p>    pointcut和advice通过配置文件来实现，因此，接下来你只需编写主方法的Java代码：</p>
		<p>    代码:</p>
		<p>package com.company.springaop.test;</p>
		<p>import org.springframework.context.ApplicationContext;</p>
		<p>import org.springframework.context.support.FileSystemXmlApplicationContext;</p>
		<p>public class Main {</p>
		<p>public static void main(String[] args) {</p>
		<p>  //Read the configuration file<br />  ApplicationContext ctx = new FileSystemXmlApplicationContext("springconfig.xml");</p>
		<p>  //Instantiate an object<br />  Bean x = (Bean) ctx.getBean("bean");</p>
		<p>  //Execute the public method of the bean (the test)<br />  x.theMethod();<br />}<br />}</p>
		<p>    我们从读入和处理配置文件开始，接下来马上要创建它。这个配置文件将作为粘合程序不同部分的“胶水”。读入和处理配置文件后，我们会得到一个创建工厂ctx。任何一个Spring管理的对象都必须通过这个工厂来创建。对象通过工厂创建后便可正常使用。</p>
		<p>    仅仅用配置文件便可把程序的每一部分组装起来。</p>
		<p>    代码:</p>
		<p>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "<a href="http://www.springframework.org/dtd/spring-beans.dtd">http://www.springframework.org/dtd/spring-beans.dtd</a>"&gt;</p>
		<p>&lt;beans&gt;<br />&lt;!--CONFIG--&gt;<br />&lt;bean id="bean" class="org.springframework.aop.framework.ProxyFactoryBean"&gt;<br />  &lt;property name="proxyInterfaces"&gt;<br />    &lt;value&gt;com.company.springaop.test.Bean&lt;/value&gt;<br />  &lt;/property&gt;<br />  &lt;property name="target"&gt;<br />    &lt;ref local="beanTarget"/&gt;<br />  &lt;/property&gt;<br />  &lt;property name="interceptorNames"&gt;<br />    &lt;list&gt;<br />    &lt;value&gt;theAdvisor&lt;/value&gt;<br />    &lt;/list&gt;<br />  &lt;/property&gt;<br />&lt;/bean&gt;</p>
		<p>&lt;!--CLASS--&gt;<br />&lt;bean id="beanTarget" class="com.company.springaop.test.BeanImpl"/&gt;</p>
		<p>&lt;!--ADVISOR--&gt;<br />&lt;!--Note: An advisor assembles pointcut and advice--&gt;<br />&lt;bean id="theAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"&gt;<br />  &lt;property name="advice"&gt;<br />    &lt;ref local="theBeforeAdvice"/&gt;<br />  &lt;/property&gt;<br />  &lt;property name="pattern"&gt;<br />    &lt;value&gt;com\.company\.springaop\.test\.Bean\.theMethod&lt;/value&gt;<br />  &lt;/property&gt;<br />&lt;/bean&gt;</p>
		<p>&lt;!--ADVICE--&gt;<br />&lt;bean id="theBeforeAdvice" class="com.company.springaop.test.TestBeforeAdvice"/&gt;<br />&lt;/beans&gt;</p>
		<p>    四个bean定义的次序并不重要。我们现在有了一个advice，一个包含了正则表达式pointcut的advisor，一个主程序类和一个配置好的接口，通过工厂ctx，这个接口返回自己本身实现的一个引用。</p>
		<p>    BeanImpl和TestBeforeAdvice都是直接配置。我们用一个唯一的ID创建一个bean元素，并指定了一个实现类。这就是全部的工作。</p>
		<p>    advisor通过Spring framework提供的一个RegexMethodPointcutAdvisor类来实现。我们用advisor的一个属性来指定它所需的 advice-bean。第二个属性则用正则表达式定义了pointcut，确保良好的性能和易读性。</p>
		<p>    最后配置的是bean，它可以通过一个工厂来创建。bean的定义看起来比实际上要复杂。bean是ProxyFactoryBean的一个实现，它是Spring framework的一部分。这个bean的行为通过一下的三个属性来定义：</p>
		<p>      * 属性proxyInterface定义了接口类。<br />      * 属性target指向本地配置的一个bean，这个bean返回一个接口的实现。<br />      * 属性interceptorNames是唯一允许定义一个值列表的属性。这个列表包含所有需要在beanTarget上执行的advisor。注意，advisor列表的次序是非常重要的。</p>
		<p>    Spring工具</p>
		<p>    虽然你可以手工修改Ant构建脚本，但使用SpringUI（译注：SpringUI现在是Spring framework的一部分，并改名为spring-ide），使用Spring AOP变得很简单，只要点点鼠标即可。你可以把SpringUI安装成Eclipse的一个plug-in。然后，你只需在你的project上右击鼠标，并选择“add Spring Project Nature”。在project属性中，你可以在“Spring Project”下添加Spring配置文件。在编译前把下面的类库加入project：aopalliance.jar，commons- logging.jar，jakarta-oro-2.0.7.jar和spring.jar。运行程序时你会看到下面的信息：</p>
		<p>    ... (logging information)<br />    Hello world! (by com.company.springaop.test.TestBeforeAdvice)<br />    com.company.springaop.test.BeanImpl.theMethod() says HELLO!</p>
		<p>
				<br />    优点和缺点</p>
		<p>    Spring比起其他的framework更有优势，因为除了AOP以外，它提供了更多别的功能。作为一个轻型framework，它在J2EE 不同的部分都可以发挥作用。因此，即使不想使用Spring AOP，你可能还是想使用Spring。另一个优点是，Spring并不要求开发团队所有的人员都会用它。学习Spring应该从Spring reference的第一页开始。读了本文后，你应该可以更好地理解Spring reference了。Spring唯一的缺点是缺乏更多的文档，但它的mailing list是个很好的补充，而且会不断地出现更多的文档。</p>
<img src ="http://www.blogjava.net/bily/aggbug/81742.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/bily/" target="_blank">礼必风</a> 2006-11-17 12:56 <a href="http://www.blogjava.net/bily/archive/2006/11/17/81742.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Aspectwerkz动态实现AOP</title><link>http://www.blogjava.net/bily/archive/2006/11/15/81313.html</link><dc:creator>礼必风</dc:creator><author>礼必风</author><pubDate>Wed, 15 Nov 2006 09:02:00 GMT</pubDate><guid>http://www.blogjava.net/bily/archive/2006/11/15/81313.html</guid><wfw:comment>http://www.blogjava.net/bily/comments/81313.html</wfw:comment><comments>http://www.blogjava.net/bily/archive/2006/11/15/81313.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/bily/comments/commentRss/81313.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/bily/services/trackbacks/81313.html</trackback:ping><description><![CDATA[
		<font face="Arial">目前最成熟、功能最丰富的<span lang="EN-US">AOP</span>框架当数<span lang="EN-US">AspectJ</span>，<span lang="EN-US">AspectJ</span>已成为大多数其它框架跟从的标准。但是，<span lang="EN-US">AspectJ</span>也走出了非同寻常的一步，它的实现为<span lang="EN-US">Java</span>语言增添了新的关键词。虽然新的语法并不难学，但却意味着我们必须换一个编译器，还要重新配制编辑器，只有这样才能适应新的语法。在规模较大的开发组中，这些要求可能难以办到，因为整个开发小组都会受到影响。由于语言本身的变化，开发小组把<span lang="EN-US">AOP</span>技术引入到现有项目的学习周期随之延长。</font>
		<font face="宋体">
		</font>
		<span lang="EN-US">
				<br />
				<br />
				<font face="Arial">    </font>
		</span>现在我们需要的是这样一个框架，它可以方便地引入，且不会对原来的开发和构造过程产生任何影响。满足这些要求的框架不止一个，例如<span lang="EN-US">JBoss AOP</span>、<span lang="EN-US">Nanning</span>、<span lang="EN-US">Aspectwerkz</span>（<span lang="EN-US">AW</span>）。本文选用的是<span lang="EN-US">Aspectwerkz</span>，因为它可能是最容易学习的框架，也是最容易集成到现有项目的框架。<font face="宋体"></font><span lang="EN-US"><br /><br /><font face="Arial">    Aspectwerkz</font></span>由<span lang="EN-US">Jonas Boner</span>和<span lang="EN-US">Alexandre Vasseur</span>创建，它是目前最快速、功能最丰富的框架之一。虽然它还缺乏<span lang="EN-US">AspectJ</span>的某些功能，但己足以满足大多数开发者在许多情形下的需要。<font face="宋体"></font><span lang="EN-US"><br /><br /><font face="Arial">    Aspectwerkz</font></span>最令人感兴趣的特性之一是它能够以两种不同的模式运行：联机模式和脱机模式。在联机模式下，<span lang="EN-US">AW</span>直接干预属于<span lang="EN-US">JVM</span>的底层类装入机制，截取所有的类装入请求，对字节码实施即时转换。<span lang="EN-US">AW</span>提供了干预类装入过程的许多选项，另外还有一个替代<span lang="EN-US">bin/java</span>命令的封装脚本，这个脚本能够根据<span lang="EN-US">Java</span>版本和<span lang="EN-US">JVM</span>能力自动生成一组可运行的配制。对于开发者，联机模式有许多优点，它能插入到任何类装入器并在类装入期间生成新的类。也就是说，我们不必手工修改应用程序的类，只要按通常的方式部署即可。不过，联机模式要求对应用服务器进行额外的配制，有时这一要求可能很难满足。<font face="宋体"></font><span lang="EN-US"><br /><br /><font face="Arial">    </font></span>在脱机模式下，生成类需要二个步骤。第一步是用标准的编译器编译，第二步是重点<span lang="EN-US">——</span>以脱机模式运行<span lang="EN-US">AWcompiler</span>编译器，让它处理新生成的类。编译器将修改这些类的字节码，根据一个<span lang="EN-US">XML</span>文件的定义，在适当的<span lang="EN-US">point-cut</span>插入<span lang="EN-US">advice</span>。脱机模式的优点是<span lang="EN-US">AWcompiler</span>生成的类能够在任何<span lang="EN-US">JVM 1.3</span>以上的虚拟机运行，本文下面要用的就是这种模式，因为它不需要对<span lang="EN-US">Tomcat</span>作任何修改，只要对构造过程稍作修改就可以照搬到大多数现有的项目。<span lang="EN-US"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?><o:p></o:p></span><font face="宋体"></font><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 21.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">AspectWerkz </span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">主要特性：<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    1</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．运行时和加载时字节码修正：你可以在运行时或编译时轻松的改造任何（旧）应用程序或除了<span lang="EN-US">rt.jar</span>以外的外部类库<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    2</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．支持<span lang="EN-US">join point</span>模型<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    3</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．支持<span lang="EN-US">Annotation</span>：匹配<span lang="EN-US">JavaDoc</span>和<span lang="EN-US">JSR-175</span>，支持用户自定义<span lang="EN-US">Annotation<o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    4</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．支持部署多个<span lang="EN-US">Aspect</span>定义文件到部署的应用程序（<span lang="EN-US">WEB-INF/aop.xml</span>、<span lang="EN-US">META-INF/aop.xml</span>）<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    5</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．<span lang="EN-US">Introduction/</span>内类型声明（也称<span lang="EN-US">Mixin</span>），也就是具有添加接口和实现到已存在的类中的能力<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    6</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．<span lang="EN-US">Annotation</span>定义：定义<span lang="EN-US">Aspect</span>使用的运行时<span lang="EN-US">Annotation</span>（为<span lang="EN-US">JSR-175</span>准备）<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    7</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．<span lang="EN-US">XML</span>定义：定义<span lang="EN-US">Aspect</span>使用的<span lang="EN-US">XML</span>；<span lang="EN-US">XML</span>可以用来精炼、改写和解析<span lang="EN-US">Annotation</span>定义<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    8</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．插件式<span lang="EN-US">Aspect</span>管理器能够和<span lang="EN-US">IoC</span>框架（如<span lang="EN-US">Spring</span>或<span lang="EN-US">PicoContainer</span>）一起工作<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    9</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．四种不同的<span lang="EN-US">Advice</span>和<span lang="EN-US">Introduction</span>部署模型（范围）：<span lang="EN-US">perJVM</span>（单模式）、<span lang="EN-US"> perClass</span>、<span lang="EN-US">perInstance </span>和<span lang="EN-US">perThread<o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    10</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．<span lang="EN-US">Advice</span>和<span lang="EN-US">Introduction</span>能够动态部署、反部署或重新部署<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    11</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．高性能，使用<span lang="EN-US">JIT</span>编译<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    12</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．使用<span lang="EN-US">Fine-grained</span>模式语言选择<span lang="EN-US">join point<o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    13</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．所有<span lang="EN-US">Advice</span>能够和所有的<span lang="EN-US">join point</span>和各种混合类型的<span lang="EN-US">pointcut<o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    14</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．脱机变换（可以用作后处理器）<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    15</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．<span lang="EN-US">Aspect</span>、<span lang="EN-US">Advice</span>和<span lang="EN-US">Introduction</span>使用<span lang="EN-US">POJO</span>编码<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    16</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．目标类可以是正规的<span lang="EN-US">POJO</span>，也就是不需要接口<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    17</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．支持通过定义传递参数给<span lang="EN-US">Advice</span>和定义可重用的<span lang="EN-US">Advice</span>堆栈<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    18</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．元数据被加到类中<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan; tab-stops: list 42.0pt" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">    19</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">．简单的用法和配置<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><font face="Arial"><u><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">开始<span lang="EN-US">AOP</span></span></u><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><o:p></o:p></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">1</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">）这里我们要在屏幕打印出“<span lang="EN-US">Hello AOP!</span>”，看如下代码：<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: blue; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">//HelloAOP.java</span><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><o:p></o:p></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">public class HelloAOP {<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">    public static void main(String args[]) {<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">        HelloAOP ha = new HelloAOP();<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">        ha.test();<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">    }<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">    public void test() {<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">        System.out.println("Hello AOP!");<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">    }<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">}<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">编译<span lang="EN-US">HelloAOP.java</span>文件：<span lang="EN-US">javac HelloAOP.java<o:p></o:p></span></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">2</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">）现在我要在输出“<span lang="EN-US">Hello AOP!</span>”前后做一些工作，这些工作在运行时会得到调用机会，如果使用<span lang="EN-US">AOP</span>术语，我们可以说我们要编写我们的<span lang="EN-US">aspect</span>，这个<span lang="EN-US">aspect</span>会在运行时被<span lang="EN-US">weave into </span>（织入）<span lang="EN-US">HelloAOP class</span>。<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: blue; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">//MyAspect.java</span><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><o:p></o:p></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">import org.codehaus.aspectwerkz.joinpoint.JoinPoint;<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">public class MyAspect {<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">    public void beforeTesting(JoinPoint joinPoint) {<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">        System.out.println("before testing...");<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">    }<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">    public void afterTesting(JoinPoint joinPoint) {<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">        System.out.println("after testing...");<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">    }<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">}<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">javac MyAspect.java<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">3</span><span style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">）织入过程并不简单，我们需要撰写一个描述文件来将<span lang="EN-US">aspect</span>和其织入的<span lang="EN-US">class</span>中的信息联系起来。<span lang="EN-US"><o:p></o:p></span></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: blue; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">//aop.xml</span><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><o:p></o:p></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">&lt;aspectwerkz&gt;<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">    &lt;system id="AspectWerkzExample"&gt;<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">        &lt;aspect class="MyAspect"&gt;<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">                &lt;pointcut name="testMethod" expression="execution(* HelloAOP.test(..))"/&gt;<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">                &lt;advice name="beforeTesting" type="before" bind-to="testMethod"/&gt;<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">                &lt;advice name="afterTesting" type="after" bind-to="testMethod"/&gt;<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">        &lt;/aspect&gt;<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">    &lt;/system&gt;<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">&lt;/aspectwerkz&gt;<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">4)run it<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">aspectwerkz -Daspectwerkz.definition.file=aop.xml HelloAOP<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><font face="Arial"><span lang="EN-US" style="COLOR: blue; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">//output:</span><span lang="EN-US" style="COLOR: black; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><o:p></o:p></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: blue; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">before testing...<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: blue; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">Hello AOP!<o:p></o:p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; LINE-HEIGHT: 130%; TEXT-ALIGN: left; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="COLOR: blue; LINE-HEIGHT: 130%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><font face="Arial">after testing...<o:p></o:p></font></span></p><span lang="EN-US" style="FONT-SIZE: 10.5pt; COLOR: black; FONT-FAMILY: 宋体; mso-bidi-font-family: Arial; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"><font face="Arial" size="3">Everything is fine!</font></span><img src ="http://www.blogjava.net/bily/aggbug/81313.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/bily/" target="_blank">礼必风</a> 2006-11-15 17:02 <a href="http://www.blogjava.net/bily/archive/2006/11/15/81313.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>