Aspject 分析之一

本文详细介绍了AspjectJ的单例模式、配置、切入点形式、对象初始化注入及关键语法,包括类方法、字段、异常处理和静态方法的区别,并展示了如何在不同场景下应用AspjectJ进行代码增强。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、Aspject是单例模式的,不用手动去实例化

2、创建aop.xml文件:

         <aspectj>
    <aspects>
        <aspect name="ajia.security.SecurityAspect"/>
    </aspects>
</aspectj>

使用如下命令:java –classpath classes;aspects
   -javaagent:%ASPECTJ_HOME%\lib\aspectjweaver.jar ajia.main.Main
 该方式是加载时植入Aspject -------------Load-time weaving

3、AspjectJ的切入点形式:

     方法级别: 执行时:使用java反射机制  ,能起作用            调用时:java反射机制不能起作用

 

    字段级别: 执行时:使用java反射机制  ,不能起作用            调用时:java反射机制不能起作用    

 

   异常处理级别:

   类初始化级别:加载一个类的时候,在静态块内植入

   对象初始化级别:Such a join
point, unlike a constructor-execution join point, occurs only in the first called con-
structor for each type in the hierarchy. Unlike class-initialization that occurs when a
class loader loads a class, object initialization occurs when an object is created.

只发生在继承树中每个类型的初始化。当对象创建的时候植入

   public class SavingsAccount extends Account {
    ...
    public SavingsAccount(int accountNumber, boolean isOverdraft) {
        super(accountNumber);                             
        this.isOverdraft = isOverdraft;         
    }
Object initialization
    public SavingsAccount(int accountNumber) {
join point
        this(accountNumber, false);            
        this.minimumBalance = 25;
    }
    ...
}

In this code snippet, for the first constructor, the object-initialization join point
encompasses the assignment to the
isOverdraft instance member and not super().
For the second constructor, the join point encompasses the assignments in the first
and second constructor.

4、AspjectJ中this语法:Any join point where this instanceof Account evaluates to true,不会选择静态方法

     target语法:Any join point where the object on which the method is being called is instanceof Account.,不会选择静态方法

 

 

public class Account {
    ...                                     
                                               
    (1)、public void debit(double amount)        
        throws InsufficientBalanceException {
    }              

  
    (2)、  private static class Helper {            
                          
    }                                       
}

    (3)、public class SavingsAccount extends Account {
   
}

(1)和(2)属于within语法范围  (1)\(2)\(3)属于this语法范围

 

5、 Also note that the two pointcuts execution(* Account.*(..)) and execution(* *(..))&& this(Account) don’t select the same set of join points. The first selects

all the instance and static methods defined in the Account class, whereas the latter picks up the instance methods in the class hierarchy of the Account class but none of
the static methods.

6、

   declare @method: public * Account.*(..): @Secured(role="ROLE_TELLER"); 将符合的所有类的方法追加@Secured注解

   declare @type: @PrivacyControlled * : @Tracked; 将持有PrivacyControlled注解的类或者接口或者注解类追加Tracked注解

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值