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注解