From cc02d545ff523ed188c0c779c469795eb922c210 Mon Sep 17 00:00:00 2001 From: bingor Date: Mon, 17 Oct 2022 15:42:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9D=A6=E5=85=8B=E5=A4=A7=E6=88=98=EF=BC=88?= =?UTF-8?q?=E4=B8=80=E6=9C=9F=EF=BC=89-=E8=AE=BE=E8=AE=A1=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F-SpringAop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++++ .../com/msb/proxy/springAop/v1/LogProxy.java | 23 ++++++++++++++ .../java/com/msb/proxy/springAop/v1/Main.java | 24 ++++++++++++++ .../java/com/msb/proxy/springAop/v1/Tank.java | 29 +++++++++++++++++ .../com/msb/proxy/springAop/v2/LogProxy.java | 31 +++++++++++++++++++ .../java/com/msb/proxy/springAop/v2/Main.java | 24 ++++++++++++++ .../java/com/msb/proxy/springAop/v2/Tank.java | 26 ++++++++++++++++ src/main/resources/application-auto.xml | 11 +++++++ src/main/resources/applicationContext.xml | 17 +++++++++- 9 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/msb/proxy/springAop/v1/LogProxy.java create mode 100644 src/main/java/com/msb/proxy/springAop/v1/Main.java create mode 100644 src/main/java/com/msb/proxy/springAop/v1/Tank.java create mode 100644 src/main/java/com/msb/proxy/springAop/v2/LogProxy.java create mode 100644 src/main/java/com/msb/proxy/springAop/v2/Main.java create mode 100644 src/main/java/com/msb/proxy/springAop/v2/Tank.java create mode 100644 src/main/resources/application-auto.xml diff --git a/pom.xml b/pom.xml index 25aad6e..fb41eef 100644 --- a/pom.xml +++ b/pom.xml @@ -16,6 +16,12 @@ 5.3.23 + + org.aspectj + aspectjweaver + 1.9.6 + + junit diff --git a/src/main/java/com/msb/proxy/springAop/v1/LogProxy.java b/src/main/java/com/msb/proxy/springAop/v1/LogProxy.java new file mode 100644 index 0000000..fd2bd55 --- /dev/null +++ b/src/main/java/com/msb/proxy/springAop/v1/LogProxy.java @@ -0,0 +1,23 @@ +package com.msb.proxy.springAop.v1;/** + * @Author bingor + * @Date 2022/10/17 14:45 + * @Description: com.msb.proxy.springAop.v1 + * @Version: 1.0 + */ + +/** + *@ClassName LogProxy + *@Description TODO + *@Author bingor + *@Date 2022/10/17 14:45 + *@Version 3.0 + */ +public class LogProxy { + public void before() { + System.out.println("method for before execute..."); + } + + public void after() { + System.out.println("method for after execute..."); + } +} diff --git a/src/main/java/com/msb/proxy/springAop/v1/Main.java b/src/main/java/com/msb/proxy/springAop/v1/Main.java new file mode 100644 index 0000000..b4828ef --- /dev/null +++ b/src/main/java/com/msb/proxy/springAop/v1/Main.java @@ -0,0 +1,24 @@ +package com.msb.proxy.springAop.v1;/** + * @Author bingor + * @Date 2022/10/17 14:54 + * @Description: com.msb.proxy.springAop.v1 + * @Version: 1.0 + */ + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + *@ClassName Main + *@Description TODO + *@Author bingor + *@Date 2022/10/17 14:54 + *@Version 3.0 + */ +public class Main { + public static void main(String[] args) { + ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); + Tank tank = (Tank) context.getBean("tank"); + tank.move(); + } +} diff --git a/src/main/java/com/msb/proxy/springAop/v1/Tank.java b/src/main/java/com/msb/proxy/springAop/v1/Tank.java new file mode 100644 index 0000000..8e33d69 --- /dev/null +++ b/src/main/java/com/msb/proxy/springAop/v1/Tank.java @@ -0,0 +1,29 @@ +package com.msb.proxy.springAop.v1;/** + * @Author bingor + * @Date 2022/10/17 9:41 + * @Description: com.msb.proxy.v5 + * @Version: 1.0 + */ + +import com.msb.proxy.cglib.LogMethodInterceptor; +import org.springframework.cglib.proxy.Enhancer; + +import java.util.Random; + +/** + *@ClassName Tank + *@Description TODO + *@Author bingor + *@Date 2022/10/17 9:41 + *@Version 3.0 + */ +public class Tank { + public void move() { + System.out.println("tank move……"); + try { + Thread.sleep(new Random().nextInt(10000)); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/msb/proxy/springAop/v2/LogProxy.java b/src/main/java/com/msb/proxy/springAop/v2/LogProxy.java new file mode 100644 index 0000000..e4ae2a1 --- /dev/null +++ b/src/main/java/com/msb/proxy/springAop/v2/LogProxy.java @@ -0,0 +1,31 @@ +package com.msb.proxy.springAop.v2;/** + * @Author bingor + * @Date 2022/10/17 14:45 + * @Description: com.msb.proxy.springAop.v1 + * @Version: 1.0 + */ + +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; + +/** + *@ClassName LogProxy + *@Description TODO + *@Author bingor + *@Date 2022/10/17 14:45 + *@Version 3.0 + */ +@Aspect +public class LogProxy { + @Before("execution(void com.msb.proxy.springAop.v2.Tank.move())") + public void before() { + System.out.println("auto:method for before execute..."); + } + + @After("execution(void com.msb.proxy.springAop.v2.Tank.move())") + public void after() { + System.out.println("auto:method for after execute..."); + } +} diff --git a/src/main/java/com/msb/proxy/springAop/v2/Main.java b/src/main/java/com/msb/proxy/springAop/v2/Main.java new file mode 100644 index 0000000..4c6af61 --- /dev/null +++ b/src/main/java/com/msb/proxy/springAop/v2/Main.java @@ -0,0 +1,24 @@ +package com.msb.proxy.springAop.v2;/** + * @Author bingor + * @Date 2022/10/17 14:54 + * @Description: com.msb.proxy.springAop.v1 + * @Version: 1.0 + */ + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + *@ClassName Main + *@Description TODO + *@Author bingor + *@Date 2022/10/17 14:54 + *@Version 3.0 + */ +public class Main { + public static void main(String[] args) { + ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"application-auto.xml"}); + Tank tank = (Tank) context.getBean("tank"); + tank.move(); + } +} diff --git a/src/main/java/com/msb/proxy/springAop/v2/Tank.java b/src/main/java/com/msb/proxy/springAop/v2/Tank.java new file mode 100644 index 0000000..ab9b641 --- /dev/null +++ b/src/main/java/com/msb/proxy/springAop/v2/Tank.java @@ -0,0 +1,26 @@ +package com.msb.proxy.springAop.v2;/** + * @Author bingor + * @Date 2022/10/17 9:41 + * @Description: com.msb.proxy.v5 + * @Version: 1.0 + */ + +import java.util.Random; + +/** + *@ClassName Tank + *@Description TODO + *@Author bingor + *@Date 2022/10/17 9:41 + *@Version 3.0 + */ +public class Tank { + public void move() { + System.out.println("auto: tank move……"); + try { + Thread.sleep(new Random().nextInt(10000)); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/resources/application-auto.xml b/src/main/resources/application-auto.xml new file mode 100644 index 0000000..9dc1ca5 --- /dev/null +++ b/src/main/resources/application-auto.xml @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file diff --git a/src/main/resources/applicationContext.xml b/src/main/resources/applicationContext.xml index f94c065..f4ef295 100644 --- a/src/main/resources/applicationContext.xml +++ b/src/main/resources/applicationContext.xml @@ -1,7 +1,22 @@ + xmlns:aop="http://www.springframework.org/schema/aop" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> + + + + + + + + + + + + + \ No newline at end of file