ソースを参照

spring 中 AOP切面编程实例

zhangxiaoyu 2 年 前
コミット
731c0219d0

+ 1 - 0
pom.xml

@@ -12,6 +12,7 @@
12
         <module>spring_annotation</module>
12
         <module>spring_annotation</module>
13
         <module>springMvc</module>
13
         <module>springMvc</module>
14
         <module>spring_test</module>
14
         <module>spring_test</module>
15
+        <module>spring_aop</module>
15
     </modules>
16
     </modules>
16
 
17
 
17
     <properties>
18
     <properties>

+ 43 - 0
spring_aop/pom.xml

@@ -0,0 +1,43 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns="http://maven.apache.org/POM/4.0.0"
3
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+    <parent>
6
+        <artifactId>spring_demo</artifactId>
7
+        <groupId>org.example</groupId>
8
+        <version>1.0-SNAPSHOT</version>
9
+    </parent>
10
+    <modelVersion>4.0.0</modelVersion>
11
+
12
+    <artifactId>spring_aop</artifactId>
13
+
14
+    <properties>
15
+        <maven.compiler.source>8</maven.compiler.source>
16
+        <maven.compiler.target>8</maven.compiler.target>
17
+    </properties>
18
+    <dependencies>
19
+        <dependency>
20
+            <groupId>org.aspectj</groupId>
21
+            <artifactId>aspectjweaver</artifactId>
22
+            <version>1.9.6</version>
23
+        </dependency>
24
+        <dependency>
25
+            <groupId>org.springframework</groupId>
26
+            <artifactId>spring-test</artifactId>
27
+            <version>5.3.6</version>
28
+        </dependency>
29
+        <dependency>
30
+            <groupId>junit</groupId>
31
+            <artifactId>junit</artifactId>
32
+            <version>4.12</version>
33
+            <scope>test</scope>
34
+        </dependency>
35
+        <dependency>
36
+            <groupId>junit</groupId>
37
+            <artifactId>junit</artifactId>
38
+            <version>4.13.1</version>
39
+            <scope>compile</scope>
40
+        </dependency>
41
+    </dependencies>
42
+
43
+</project>

+ 21 - 0
spring_aop/src/main/java/com/AopTest.java

@@ -0,0 +1,21 @@
1
+package com;
2
+
3
+
4
+import com.aop.TargetInterface;
5
+import org.junit.Test;
6
+import org.junit.runner.RunWith;
7
+import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.test.context.ContextConfiguration;
9
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10
+
11
+@RunWith(SpringJUnit4ClassRunner.class)
12
+@ContextConfiguration("classpath:context.xml")
13
+public class AopTest {
14
+
15
+    @Autowired
16
+    private TargetInterface targetInterface;
17
+    @Test
18
+    public void test() {
19
+        targetInterface.save2();
20
+    }
21
+}

+ 33 - 0
spring_aop/src/main/java/com/aop/MyAspect.java

@@ -0,0 +1,33 @@
1
+package com.aop;
2
+
3
+import org.aspectj.lang.ProceedingJoinPoint;
4
+
5
+public class MyAspect {
6
+    public void myBefore() {
7
+        System.out.println("aspect before...");
8
+    }
9
+    // 最终增强
10
+    public void after() {
11
+        System.out.println("aspect after...");
12
+    }
13
+
14
+    /**
15
+     * 环绕方法
16
+     * @param proceedingJoinPoint 正在执行的连接点 -》切点
17
+     */
18
+    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
19
+        System.out.println("环绕前增强...");
20
+        // 切点方法
21
+        Object proceed = proceedingJoinPoint.proceed();
22
+        System.out.println("环绕后增强...");
23
+        return proceed;
24
+    }
25
+
26
+    /**
27
+     * 异常增强(在抛出异常时执行)
28
+     */
29
+    public void afterThrowing() {
30
+        System.out.println("异常抛出增强...");
31
+
32
+    }
33
+}

+ 19 - 0
spring_aop/src/main/java/com/aop/TargetImpl.java

@@ -0,0 +1,19 @@
1
+package com.aop;
2
+
3
+public class TargetImpl implements TargetInterface{
4
+    @Override
5
+    public void save() {
6
+        System.out.println("target save");
7
+    }
8
+
9
+    @Override
10
+    public void save2() {
11
+        System.out.println("target save2");
12
+        int i = 1/0;
13
+    }
14
+
15
+    @Override
16
+    public void throwException() {
17
+
18
+    }
19
+}

+ 8 - 0
spring_aop/src/main/java/com/aop/TargetInterface.java

@@ -0,0 +1,8 @@
1
+package com.aop;
2
+
3
+public interface TargetInterface {
4
+    void save();
5
+    void save2();
6
+    void throwException();
7
+
8
+}

+ 35 - 0
spring_aop/src/main/resources/context.xml

@@ -0,0 +1,35 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+
3
+<beans xmlns="http://www.springframework.org/schema/beans"
4
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
+       xmlns:context="http://www.springframework.org/schema/context"
6
+       xmlns:aop="http://www.springframework.org/schema/aop"
7
+       xsi:schemaLocation="http://www.springframework.org/schema/beans
8
+       http://www.springframework.org/schema/beans/spring-beans.xsd
9
+       http://www.springframework.org/schema/aop
10
+       https://www.springframework.org/schema/aop/spring-aop.xsd
11
+       http://www.springframework.org/schema/context
12
+       https://www.springframework.org/schema/context/spring-context.xsd
13
+
14
+    ">
15
+<!--       1、配置目标对象 2、配置切面对象 3、配置织入告诉Spring 哪些方法(切点)需要增强 -->
16
+    <bean id="target" class="com.aop.TargetImpl"></bean>
17
+    <bean id="myaspect" class="com.aop.MyAspect">
18
+    </bean>
19
+    <aop:config>
20
+        <!-- 声明切面-->
21
+        <aop:aspect ref="myaspect">
22
+            <!-- 切点 +通知 前置 后置 -->
23
+<!--            <aop:before method="myBefore"-->
24
+<!--                        pointcut="execution(void com.aop.TargetImpl.*())"-->
25
+<!--            />-->
26
+<!--            <aop:after method="after"-->
27
+<!--                        pointcut="execution(public void com.aop.TargetImpl.save())"-->
28
+<!--            />-->
29
+            <aop:pointcut id="save2" expression="execution(public void com.aop.TargetImpl.save2())"/>
30
+            <aop:around method="around" pointcut-ref="save2" />
31
+            <aop:after-throwing method="afterThrowing"  pointcut-ref="save2" />
32
+            <aop:after method="after" pointcut-ref="save2" />
33
+        </aop:aspect>
34
+    </aop:config>
35
+</beans>