package org.crazyit.app.aspect; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public aspect TxAspect { // 指定执行Hello.sayHello()方法时执行下面代码块 Object around():call(* org.crazyit.app.service.*.*(..)) { System.out.println("模拟开启事务..."); // 回调原来的目标方法 Object rvt = proceed(); System.out.println("模拟结束事务..."); return rvt; } }
package org.crazyit.app.service; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class World { // 定义一个简单方法,模拟应用中的业务逻辑方法 public void bar() { System.out.println("执行World组件的bar()方法"); } }
package lee; import org.crazyit.app.service.Hello; import org.crazyit.app.service.World; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class AspectJTest { public static void main(String[] args) { Hello hello = new Hello(); hello.foo(); hello.addUser("孙悟空" , "7788"); World world = new World(); world.bar(); } }
package org.crazyit.app.aspect; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public aspect AuthAspect { // 指定在执行org.crazyit.app.service包中任意类的、任意方法之前执行下面代码块 // 第一个星号表示返回值不限;第二个星号表示类名不限; // 第三个星号表示方法名不限;圆括号中..代表任意个数、类型不限的形参 before(): execution(* org.crazyit.app.service.*.*(..)) { System.out.println("模拟进行权限检查..."); } }
package org.crazyit.app.service; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class Hello { // 定义一个简单方法,模拟应用中的业务逻辑方法 public void foo() { System.out.println("执行Hello组件的foo()方法"); } // 定义一个addUser()方法,模拟应用中的添加用户的方法 public int addUser(String name , String pass) { System.out.println("执行Hello组件的addUser添加用户:" + name); return 20; } }
package org.crazyit.app.aspect; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public aspect LogAspect { // 定义一个PointCut,其名为logPointcut, // 该Pointcut代表了后面给出的切入点表达式,这样可复用该切入点表达式 pointcut logPointcut() :execution(* org.crazyit.app.service.*.*(..)); after():logPointcut() { System.out.println("模拟记录日志..."); } }
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov. // Jad home page: http://kpdus.tripod.com/jad.html // Decompiler options: packimports(3) fieldsfirst ansi space // Source File Name: TxAspect.javapackage lee;import java.io.PrintStream; import org.aspectj.lang.NoAspectBoundException; import org.aspectj.runtime.internal.AroundClosure; import org.aspectj.runtime.internal.Conversions;public class TxAspect { private static Throwable ajc$initFailureCause; /* synthetic field */ public static final TxAspect ajc$perSingletonInstance; /* synthetic field */ public TxAspect() { } public void ajc$around$lee_TxAspect$1$f54fe983(AroundClosure ajc_aroundClosure) { System.out.println("开始事务..."); ajc$around$lee_TxAspect$1$f54fe983proceed(ajc_aroundClosure); System.out.println("事务结束..."); } static void ajc$around$lee_TxAspect$1$f54fe983proceed(TxAspect this) throws Throwable { Conversions.voidValue(this.run(new Object[0])); } public static TxAspect aspectOf() { if (ajc$perSingletonInstance == null) throw new NoAspectBoundException("lee_TxAspect", ajc$initFailureCause); else return ajc$perSingletonInstance; } public static boolean hasAspect() { return ajc$perSingletonInstance != null; } private static void ajc$postClinit() { ajc$perSingletonInstance = new TxAspect(); } static { try { ajc$postClinit(); } catch (Throwable throwable) { ajc$initFailureCause = throwable; } } }
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov. // Jad home page: http://kpdus.tripod.com/jad.html // Decompiler options: packimports(3) fieldsfirst ansi space // Source File Name: Hello.javapackage lee;import java.io.PrintStream; import org.aspectj.runtime.internal.AroundClosure;// Referenced classes of package lee: // LogAspect, TxAspectpublic class Hello { public Hello() { } public void sayHello() { try { System.out.println("Hello AspectJ!"); } catch (Throwable throwable) { LogAspect.aspectOf().ajc$after$lee_LogAspect$1$9fd5dd97(); throw throwable; } LogAspect.aspectOf().ajc$after$lee_LogAspect$1$9fd5dd97(); } public static void main(String args[]) { Hello h = new Hello(); Hello hello = h; sayHello_aroundBody1$advice(hello, TxAspect.aspectOf(), null); } private static final void sayHello_aroundBody0(Hello hello) { hello.sayHello(); } private static final void sayHello_aroundBody1$advice(TxAspect this, AroundClosure ajc_aroundClosure, AroundClosure aroundclosure) { System.out.println("开始事务..."); AroundClosure aroundclosure1 = aroundclosure; sayHello_aroundBody0(this); System.out.println("事务结束..."); } }