| |
本文讲解的主要内容,按照概念的重要程度,排列如下: aspectj是一个代码生成工具(code generator)。 aspectj语法就是用来定义代码生成规则的语法。您如果使用过java compiler compiler (javacc),您会发现,两者的代码生成规则的理念惊人相似。 aspectj有自己的语法编译工具,编译的结果是java class文件,运行的时候,classpath需要包含aspectj的一个jar文件(runtime lib)。 aspectj和xdoclet的比较。aspectj和ejb descriptor的比较。 本文的原则是,只细讲其他资料没有讲到的东西,其他资料讲过的东西,不讲或略讲。以节省网络资源,更为了节省大家宝贵的时间。j 2.aspect oriented programming (aop) 本节简单介绍aop的概念,解释我们为什么需要aop。 aop是object oriented programming(oop)的补充。 oop能够很好地解决对象的数据和封装的问题,却不能很好的解决aspect("方面")分离的问题。下面举例具体说明。 比如,我们有一个bank(银行)类。bank有两个方法,deposit(存钱)和withdraw(取钱)。 类和方法的定义如下: code 2.1 bank.java class bank{ public float deposit(accountinfo account, float money){ // 增加account账户的钱数,返回账户里当前的钱数 } public float withdraw(accountinfo account, float money){ // 减少account账户的钱数,返回取出的钱数 } }; 这两个方法涉及到用户的账户资金等重要信息,必须要非常小心,所以编写完上面的商业逻辑之后,项目负责人又提出了新的要求--给bank类的每个重要方法加上安全认证特性。 于是,我们不得不分别在上面的两个方法中加入安全认证的代码。 类和方法的定义如下:(新增加的代码用不同的背景标出) code 2.2 bank.java class bank{ public float deposit(accountinfo account, float money){ // 验证account是否为合法用户 // 增加account账户的钱数,返回账户里当前的钱数 } public float withdraw(accountinfo account, float money){ // 验证account是否为合法用户 // 减少account账户的钱数,返回取出的钱数 } }; 这两个方法都需要操作数据库,为了保持数据完整性,项目负责人又提出了新的要求--给bank类的每个操作数据库的方法加上事务控制。 于是,我们不得不分别在上面的两个方法中加入安全认证的代码。 类和方法的定义如下:(新增加的代码用不同的背景标出) code 2.3 bank.java class bank{ public float deposit(accountinfo account, float money){ // 验证account是否为合法用户 // begin transaction // 增加account账户的钱数,返回账户里当前的钱数 // end transaction } public float withdraw(accountinfo account, float money){ // 验证account是否为合法用户 // begin transaction // 减少account账户的钱数,返回取出的钱数 // end transaction } }; 我们看到,这些与商业逻辑无关的重复代码遍布在整个程序中。实际的工程项目中涉及到的类和函数,远远不止两个。如何解决这种问题? 我们首先来看看oop能否解决这个问题。 我们利用design pattern的template pattern,可以抽出一个框架,改变上面的例子的整个设计结构。 类和方法的定义如下: code 2.4 base.java abstract class base{ public float importantmethod(accountinfo account, float money){ // 验证account是否为合法用户 // begin transaction float result = yourbusiness(account, money) // end transaction return result; } protected abstract float yourbusiness(accountinfo account, float money); }; code 2.5 bankdeposit.java class bankdeposit extends base{ protected float yourbusiness(accountinfo account, float money){ // 增加account账户的钱数,返回账户里当前的钱数 } }; code 2.6 bankwithdraw.java class bankwithdraw extends base{ protected float yourbusiness(accountinfo account, float money){ // 减少account账户的钱数,返回取出的钱数 } }; 这里我们用一种很勉强的方法实现了认证和事务代码的重用。而且,有心的读者可能会注意到,这种方法的前提是,强制所有的方法都遵守同样的signature。 如果有一个转账方法transfer(accountinfo giver, accountinfo receiver, float money),由于transfer方法的signature不同于yourbusiness的signature,这个方法无法使用上面的框架。 这个例子中提到的认证,事务等方面,就是aop所关心的aspect。 aop就是为了解决这种问题而出现的。aop的目的就是--separation of aspects (or separation of concerns). 下面的章节,解释ejb descriptor,aspectj,xdoclet等工具如何解决separation of aspects的问题。 3.ejb descriptor 如果我们使用ejb实现上面的例子,bank类可以作为一个stateless session bean实现。 在bank的代码中只用考虑商业逻辑,不用考虑认证和事务等方面。 认证和事务等方面在ejb descriptor中定义,由ejb container提供这些方面的实现。 我们来看一下,如何使用ejb descriptor描述上面的例子。 ejb descriptor包括一个ejb-jar.xml文件。ejb-jar.xml文件包含两大部分,enterprise-beans和assembly-descriptor部分。enterprise-beans部分包含ejb的定义--jndi name,ejb home, interface, bean class path等;assembly-descriptor部分包括配置信息的定义--安全角色,事务控制等等。 下面给出上面例子对应的模拟ejb descriptor。 <ejb-jar> <enterprise-beans> <session> <ejb-name>bank</ejb-name> … <ejb-class>example.bank</ejb-class> <session-type>stateless</session-type> <transaction-type>container</transaction-type> <security-role-ref> <role-name>bank-account</role-name> </security-role-ref> </session> </enterprise-beans> <assembly-descriptor> <security-role> <role-name>bank-account</role-name> </security-role> <method-permission> <role-name>employee</role-name> <method> <ejb-name>bank</ejb-name> <method-name>deposit</method-name> </method> <method> <ejb-name>bank</ejb-name> <method-name>withdraw</method-name> </method> </method-permission> <container-transaction> <method> <ejb-name>bank</ejb-name> <method-name>deposit</method-name> </method> <method> <ejb-name>bank</ejb-name> <method-name>withdraw</method-name> </method> <trans-attribute>required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar> 本文后面会讲到如何用aspectj实现上例中的separation of aspects。 读者可以比较一下aspectj语法和ejb descriptor定义之间的对应关系。 两者都提供了类名、方法名的匹配规则,能够把类的方法映射到认证,事务等aspect(方面)。 4.aspectj 这一节我们来看看aspectj如何实现上例中的separation of aspects。 使用aspectj,我们不用对原有的代码做任何修改,就可以为代码提供不同的aspect(方面)--比如,认证,事务等。 我们只需要提供两个不同的aspect--认证aspect和事务aspect。 code 4.1 authaspect.java aspect authaspect{ pointcut bankmethods() : execution (* bank.deposit(…)) || execution (* bank. withdraw (…)); object around(): bankmethods(){ // 验证account是否为合法用户 return proceed(); } }; code 4.2 transactionaspect.java aspect transactionaspect{ pointcut bankmethods() : execution(* bank.deposit(…)) || execution (* bank. withdraw (…)); object around(): bankmethods(){ // begin transaction o
|
|