锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
濡傛灉鐩稿悓綾誨瀷鐨勯氱煡浣嶄簬鍚屼竴涓柟闈腑錛岄偅涔堝湪鎶婇氱煡搴旂敤浜庡叡浜殑榪炴帴鐐規椂錛屽彲浠ヤ嬌鐢ㄩ氱煡澹版槑鐨勪綅緗潵鎸囩ず鍏朵紭鍏堢駭銆傛寜鍦ㄦ柟闈腑澹版槑before()鍜宎round()閫氱煡綾誨瀷鐨勬搴忥紝鎸夊厛鍚庨『搴忔墽琛屻傝宎fter()鐩稿弽錛屽0鏄庡湪鍚庨潰鐨勬墽琛屼紭鍏堛?img src ="http://www.aygfsteel.com/rain1102/aggbug/128334.html" width = "1" height = "1" />
]]>
閬靛驚鐗瑰畾鐨勮娉曠敤浜庢崟鑾鋒瘡涓涓綾葷殑鍙嬌鐢ㄨ繛鎺ョ偣銆?br> 涓昏鐨勭綾伙細
鍩轟簬鎺у埗嫻佺殑pointcuts
涓昏鍖呮嫭涓ょ綾誨瀷鐨勬帶鍒舵祦錛?br> cflow(Pointcut)錛屾崟鑾鋒墍鏈夌殑榪炴帴鐐瑰湪鎸囧畾鐨勬柟娉曟墽琛屼腑錛屽寘鎷墽琛屾柟娉曟湰韜?br> cflowbelow(Pointcut)錛屾崟鑾鋒墍鏈夌殑榪炴帴鐐瑰湪鎸囧畾鐨勬柟娉曟墽琛屼腑錛岄櫎浜嗘墽琛屾柟娉曟湰韜?br>
濡備互涓嬬殑渚嬪瓙錛?br> cflow(call(* Account.debit(..))
鎵鏈夌殑debit鏂規硶涓殑榪炴帴鐐癸紝鍖呮嫭debit鏂規硶鏈韓
cflowbelow(call(* Account.debit(..))
鎵鏈塪ebit鏂規硶涓殑榪炴帴鐐癸紝闄や簡debit鏂規硶鏈韓
cflow(transactedOperations())
鎵鏈夌敱transactedOperations鎹曡幏鐨勮繛鎺ョ偣
cflowbelow(execution(Account.new(..))
鎵鏈夊湪Account 鏋勯犲櫒涓墽琛岀殑榪炴帴鐐?br>
鍩轟簬璇嶆眹緇撴瀯鐨勮繛鎺ョ偣
婧愪唬鐮佺墖鏂傦紝濡倃ithin()鍜寃ithincode()
within 錛氭崟鑾峰湪鎸囧畾綾繪垨鑰呮柟闈腑鐨勭▼搴忎綋涓殑鎵鏈夎繛鎺ョ偣錛屽寘鎷唴閮ㄧ被銆?br> Withincode錛氱敤浜庢崟鑾峰湪鏋勯犲櫒鎴栬呮柟娉曚腑鐨勬墍鏈夎繛鎺ョ偣錛屽寘鎷湪鍏朵腑鐨勬湰鍦扮被
鎵ц瀵硅薄榪炴帴鐐?/strong>
鍖歸厤this,鍜宼arget瀵硅薄錛屼綔涓烘柟娉曡璋冪敤鐨勫璞°?br> this(Account)錛屾墍鏈堿ccount鐨勫疄渚嬬殑鎵ц鐐癸紝鍖歸厤鎵鏈夌殑榪炴帴鐐癸紝濡傛柟娉曡皟鐢紝灞炴ц緗紝褰撳墠鐨勬墽琛屽璞′負Account錛屾垨鑰呭叾瀛愮被銆?br> target(Account)錛氬尮閰嶆墍鏈夌殑榪炴帴鐐癸紝鐩爣瀵硅薄涓篈ccount鎴栧叾瀛愮被銆?br>
蹇呴』鎵ц鐩稿簲鐨勭被鍨嬶紝涓嶈兘浣跨敤*,鎴栬?.閫氶厤絎︺傚綋鍓嶉潤鎬佹柟娉曪紝涓嶈兘琚尮閰嶃?br>
鍦╳ithin()鍜宼his()涓殑鍖哄埆錛?br> 涓涓槸紼嬪簭浣擄紝鑰屽彟涓涓負瀵硅薄鎵ц銆?br>
鍙傛暟pointcuts
鐢ㄤ簬鎹曡幏鍙傛暟綾誨瀷鐨勮繛鎺ョ偣銆?br> args(String,..,int)
args(RemoteException)
鏉′歡媯嫻媝ointcuts
if(System.currentTimeMillis()>triggerTime)
]]>
pointcut <pointcut name>(<any values to be picked up>) : within(<class>);
2. 鎹曡幏鐗瑰畾鏂規硶鍐呯殑鎵鏈夎繛鎺ョ偣
浣跨敤withincode(Signature)鍒囧叆鐐廣傝娉曪細
pointcut <pointcut name>(<any values to be picked up>) : withincode(<modifier> <class>.<method>(<parameter types>));
]]>
pointcut <pointcut name>(<any values to be picked up>) : get(<optional modifier> <type> <class>.<field>);
get(Signature)鍒囧叆鐐逛笉鑳芥崟鑾峰闈欐佸睘鎬х殑璁塊棶錛屽敖綆′粠AspectJ鐨勮娉曡搴﹁浠ヨ繖縐嶆柟寮忓畾涔夊垏鍏ョ偣鏄畬鍏ㄥ悎娉曠殑銆?br> 浣跨敤after() returning(<ReturnValue>)褰㈠紡鐨勯氱煡錛屽畠鍦ㄥ0鏄庣殑returning()閮ㄥ垎涓甫鏈変竴涓爣璇嗙錛岀敤浜庡寘鍚闂繃鐨勫箋?br>鎹曡幏浣曟椂淇敼瀵硅薄鐨勫瓧孌?/strong>
pointcut <pointcut name>(<any values to be picked up>) : set(<optional modifier> <type> <class>.<field>);
package com.eric.aspectj;
public aspect GetRecipe {
pointcut getNamePointcut() : get(String MyClass.name);
before() : getNamePointcut() {
System.out.println("-------------- Aspect Advice Logic ---------------");
System.out.println("In the advice picked by " + "getNamePointcut()");
System.out.println("Signature: " + thisJoinPoint.getStaticPart().getSignature());
System.out.println("Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
System.out.println("--------------------------------------------------");
}
after() returning(String value) : getNamePointcut() {
System.out.println("Value being accessed is " + value);
}
}
package com.eric.aspectj;
public class MyClass {
public static final String CONSTANT = "CONSTANT STRING";
private int number;
private String name;
public String getName()
{
System.out.println("In the getName() method");
return this.name;
}
public void setName(String name)
{
System.out.println("In the setName(String) method");
this.name = name;
System.out.println("The name field has been set");
}
public int getNumber()
{
System.out.println("In the getNumber() method");
return this.number;
}
public void setNumber(int number)
{
System.out.println("In the setNumber(int) method");
this.number = number;
System.out.println("The number field has been set");
}
public static final void main(String args[])
{
MyClass myObject = new MyClass();
myObject.setName("Eric Chau");
System.out.println("The name has been set");
myObject.setNumber(88888888);
System.out.println("The number has been set");
System.out.println("Name Stored = " + myObject.getName());
System.out.println("Number Stored = " + myObject.getNumber());
System.out.println("The constant value is " + MyClass.CONSTANT);
}
}
榪愯緇撴灉錛?br>In the setName(String) method
The name field has been set
The name has been set
In the setNumber(int) method
The number field has been set
The number has been set
In the getName() method
-------------- Aspect Advice Logic ---------------
In the advice picked by getNamePointcut()
Signature: String com.eric.aspectj.MyClass.name
Source Line: MyClass.java:11
--------------------------------------------------
Value being accessed is Eric Chau
Name Stored = Eric Chau
In the getNumber() method
Number Stored = 88888888
The constant value is CONSTANT STRING
public aspect CallNewRecipe {
pointcut myClassConstructorWithIntAndStringPointcutStaticinitialization() : staticinitialization(MyClass);
pointcut myClassConstructorWithIntAndStringPointcutCall() : call(MyClass.new(int, String));
pointcut myClassConstructorWithIntAndStringPointcutExecution() : execution(MyClass.new(int, String));
pointcut myClassConstructorWithIntAndStringPointcutPreinitialization() : preinitialization(MyClass.new(int, String));
pointcut myClassConstructorWithIntAndStringPointcutInitialization() : initialization(MyClass.new(int, String));
before() : myClassConstructorWithIntAndStringPointcutExecution() {
System.out.println("-------------- Aspect Advice Logic Execution ---------------");
System.out.println("In the advice picked by " + "myClassConstructorWithIntAndOthersPointcut()");
System.out.println("The current type of object under construction is: ");
System.out.println("getThis: " + thisJoinPoint.getThis());
System.out.println("getTarget: " + thisJoinPoint.getTarget());
System.out.println("getKind: " + thisJoinPoint.getKind());
System.out.println("Signature: " + thisJoinPoint.getSignature());
System.out.println("getName: " + thisJoinPoint.getSignature().getName());
System.out.println("getDeclaringTypeName: " + thisJoinPoint.getSignature().getDeclaringTypeName());
System.out.println("getDeclaringType: " + thisJoinPoint.getSignature().getDeclaringType());
System.out.println("getModifiers: " + thisJoinPoint.getSignature().getModifiers());
System.out.println("Source Line: " + thisJoinPoint.getSourceLocation());
System.out.println("toString: " + thisJoinPoint.toString());
System.out.println("--------------------------------------------------");
}
before() : myClassConstructorWithIntAndStringPointcutStaticinitialization() {
System.out.println("--------------- Staticinitialization ------------------");
}
before() : myClassConstructorWithIntAndStringPointcutCall() {
System.out.println("--------------- Call ------------------");
}
before() : myClassConstructorWithIntAndStringPointcutPreinitialization() {
System.out.println("--------------- Preinitialization ------------------");
}
before() : myClassConstructorWithIntAndStringPointcutInitialization() {
System.out.println("--------------- Initialization ------------------");
}
}
package com.eric.aspectj;
public class MyClass {
private int number;
private String name;
public MyClass(int number, String name) {
this.number = number;
this.name = name;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyClass myObject = new MyClass(123, "Eric Chau");
}
}
榪愯緇撴灉錛?br>--------------- Staticinitialization ------------------
--------------- Call ------------------
--------------- Preinitialization ------------------
--------------- Initialization ------------------
-------------- Aspect Advice Logic Execution ---------------
In the advice picked by myClassConstructorWithIntAndOthersPointcut()
The current type of object under construction is:
getThis: com.eric.aspectj.MyClass@10d448
getTarget: com.eric.aspectj.MyClass@10d448
getKind: constructor-execution
Signature: com.eric.aspectj.MyClass(int, String)
getName: <init>
getDeclaringTypeName: com.eric.aspectj.MyClass
getDeclaringType: class com.eric.aspectj.MyClass
getModifiers: 1
Source Line: MyClass.java:8
toString: execution(com.eric.aspectj.MyClass(int, String))
--------------------------------------------------
org.aspectj.lang鈥?/span>鎻愪緵浜嗘帴鍙e拰鏀寔綾伙紝鐢ㄤ簬鍙戠幇鍏充簬榪炴帴鐐圭殑榪愯鏃朵俊鎭?/span>
org.aspectj.lang.reflect鈥?/span>鍖呭惈涓浜涙帴鍙o紝瀹冧滑鎻愪緵浜嗗叧浜庤繛鎺ョ偣絳懼悕鐨勬洿鐗規畩鍖栫殑淇℃伅銆?/span>
org.aspectj.weaver.tools鈥?/span>鎻愪緵浜嗕竴浜涙帴鍙o紝鐢ㄤ簬鏀寔JVM綾誨姞杞藉櫒鎵ц鍔犺澆鏃剁粐鍏ャ?/span>
/**
* Create a new ServiceException with the specified message.
* @param msg the detail message
*/
public ServiceException(String msg) {
super(msg);
}
/**
* Create a new ServiceException with the specified message
* and root cause.
* @param msg the detail message
* @param ex the root cause
*/
public ServiceException(String msg, Throwable ex) {
super(msg, ex);
}
}
public class ExceptionClass {
public void triggerMyException() throws ServiceException
{
ServiceException myException = new ServiceException("A service exception has occured");
System.out.println("About to throw a ServiceException");
throw myException;
}
/**
* @param args
*/
public static void main(String[] args) {
ExceptionClass myObject = new ExceptionClass();
try
{
myObject.triggerMyException();
} catch (ServiceException me)
{
System.out.println("A ServiceException has been caught");
}
}
}
榪愯緇撴灉錛?br>About to throw a ServiceException
------------ Aspect Advice Logic --------------
Signature: catch(ServiceException)
Source Line: ExceptionClass.java:19
-----------------------------------------------
A ServiceException has been caught
鑾峰彇鎶涘嚭鐨勫紓甯?br>public aspect HandlerRecipe {
pointcut myExceptionHandlerPointcut(ServiceException exception) : handler(ServiceException) && args(exception);
before(ServiceException exception) : myExceptionHandlerPointcut(exception) {
System.out.println("------------ Aspect Advice Logic --------------");
System.out.println("Signature: " + thisJoinPoint.getStaticPart().getSignature());
System.out.println("Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
System.out.println("Exception caught:");
exception.printStackTrace();
System.out.println("-----------------------------------------------");
}
}
public aspect MyAspectj {
pointcut callPointCut(My myClass, int age, String name) : execution(void com.eric.aspectj.My.foo(int, String)) && this(myClass) && args(age, name);
before(My myClass, int age, String name) : callPointCut(myClass, age, name) {
System.out.println("signature: " + thisJoinPoint.getStaticPart().getSignature());
System.out.println("Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
System.out.println("before...name : " + name + " age : " + age);
}
after(My myClass, int age, String name) returning: callPointCut(myClass, age, name) {
System.out.println("after...");
}
}
public aspect MyAspectj {
pointcut callPointCut(MyClass myClass, int age, String name) : call(void com.eric.aspectj.MyClass.foo(int, String)) && target(myClass) && args(age, name);
before(MyClass myClass, int age, String name) : callPointCut(myClass, age, name) {
System.out.println("before...name : " + name + " age : " + age);
}
after(MyClass myClass, int age, String name) returning: callPointCut(myClass, age, name) {
System.out.println("after...");
}
}
package com.eric.aspectj;
public class MyClass {
public void foo(int age, String name) {
System.out.println("foo " + name + " age is " + age);
}
public static void main(String[] args) {
MyClass object = new MyClass();
object.foo(24, "Eric Chau");
}
}
榪愯緇撴灉錛?br>before...name : Eric Chau age : 24
foo Eric Chau age is 24
after...