Browse Source

aop 基于注解模式

zhang 2 years ago
parent
commit
2fbd1f0ecd

+ 10 - 0
springMvc/src/main/java/com/aop_proxy_jdk/Advice.java

@@ -0,0 +1,10 @@
1
+package com.aop_proxy_jdk;
2
+
3
+public class Advice {
4
+    public void before() {
5
+        System.out.println("before");
6
+    }
7
+    public void after() {
8
+        System.out.println("after");
9
+    }
10
+}

+ 1 - 1
springMvc/src/main/java/com/aop_proxy/TargetImpl.java

@@ -1,4 +1,4 @@
1
-package com.aop_proxy;
1
+package com.aop_proxy_jdk;
2
 
2
 
3
 public class TargetImpl implements TargetInterface{
3
 public class TargetImpl implements TargetInterface{
4
     @Override
4
     @Override

+ 1 - 1
springMvc/src/main/java/com/aop_proxy/TargetInterface.java

@@ -1,4 +1,4 @@
1
-package com.aop_proxy;
1
+package com.aop_proxy_jdk;
2
 
2
 
3
 public interface TargetInterface {
3
 public interface TargetInterface {
4
     public void save();
4
     public void save();

+ 29 - 0
springMvc/src/main/java/com/controller/ExceptionTestController.java

@@ -1,10 +1,16 @@
1
 package com.controller;
1
 package com.controller;
2
 
2
 
3
+import com.aop_proxy_jdk.Advice;
4
+import com.aop_proxy_jdk.TargetImpl;
5
+import com.aop_proxy_jdk.TargetInterface;
3
 import com.exception.MyException;
6
 import com.exception.MyException;
4
 import org.springframework.stereotype.Controller;
7
 import org.springframework.stereotype.Controller;
5
 import org.springframework.web.bind.annotation.RequestMapping;
8
 import org.springframework.web.bind.annotation.RequestMapping;
6
 import org.springframework.web.bind.annotation.ResponseBody;
9
 import org.springframework.web.bind.annotation.ResponseBody;
7
 
10
 
11
+import java.lang.reflect.InvocationHandler;
12
+import java.lang.reflect.Method;
13
+import java.lang.reflect.Proxy;
8
 import java.util.HashMap;
14
 import java.util.HashMap;
9
 import java.util.Map;
15
 import java.util.Map;
10
 
16
 
@@ -18,4 +24,27 @@ public class ExceptionTestController {
18
         throw new MyException();
24
         throw new MyException();
19
 //        return stringStringHashMap;
25
 //        return stringStringHashMap;
20
     }
26
     }
27
+    @RequestMapping("/exception2")
28
+    @ResponseBody //
29
+    public Map exception2() {
30
+        TargetImpl target = new TargetImpl();
31
+        Advice advice = new Advice();
32
+        TargetInterface targetInterface = (TargetInterface) Proxy.newProxyInstance(
33
+                target.getClass().getClassLoader(),
34
+                target.getClass().getInterfaces(),
35
+                new InvocationHandler() {
36
+                    @Override
37
+                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
38
+                        advice.before();
39
+                        method.invoke(target,args);
40
+                        advice.after();
41
+                        return null;
42
+                    }
43
+                }
44
+        );
45
+        targetInterface.save();
46
+        return null;
47
+//        return stringStringHashMap;
48
+    }
49
+
21
 }
50
 }

+ 3 - 1
spring_aop/src/main/java/com/AopTest.java

@@ -14,8 +14,10 @@ public class AopTest {
14
 
14
 
15
     @Autowired
15
     @Autowired
16
     private TargetInterface targetInterface;
16
     private TargetInterface targetInterface;
17
+    @Autowired
18
+    private com.anno.TargetInterface targetInterface1;
17
     @Test
19
     @Test
18
     public void test() {
20
     public void test() {
19
-        targetInterface.save2();
21
+        targetInterface1.save2();
20
     }
22
     }
21
 }
23
 }

+ 45 - 0
spring_aop/src/main/java/com/anno/MyAspect.java

@@ -0,0 +1,45 @@
1
+package com.anno;
2
+
3
+import org.aspectj.lang.ProceedingJoinPoint;
4
+import org.aspectj.lang.annotation.*;
5
+import org.springframework.stereotype.Component;
6
+
7
+@Component("myAs")
8
+@Aspect
9
+public class MyAspect {
10
+    @Before("execution(void com.anno.TargetImpl.save2())")
11
+    public void myBefore() {
12
+        System.out.println("annotation. aspect before...");
13
+    }
14
+    // 最终增强
15
+    @After("MyAspect.myPoint()")
16
+    public void after() {
17
+        System.out.println("annotation.aspect after...");
18
+    }
19
+
20
+    /**
21
+     * 环绕方法
22
+     * @param proceedingJoinPoint 正在执行的连接点 -》切点
23
+     */
24
+//    @Around("execution(void com.anno.TargetImpl.save2())")
25
+    @Around("myPoint()")
26
+    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
27
+        System.out.println("环绕前增强...");
28
+        // 切点方法
29
+        Object proceed = proceedingJoinPoint.proceed();
30
+        System.out.println("环绕后增强...");
31
+        return proceed;
32
+    }
33
+    // 抽取切点。
34
+    @Pointcut("execution(void com.anno.TargetImpl.save2())")
35
+    public void myPoint() {
36
+
37
+    }
38
+    /**
39
+     * 异常增强(在抛出异常时执行)
40
+     */
41
+    public void afterThrowing() {
42
+        System.out.println("异常抛出增强...");
43
+
44
+    }
45
+}

+ 21 - 0
spring_aop/src/main/java/com/anno/TargetImpl.java

@@ -0,0 +1,21 @@
1
+package com.anno;
2
+
3
+import org.springframework.stereotype.Component;
4
+
5
+@Component("ttaget")
6
+public class TargetImpl implements TargetInterface {
7
+    @Override
8
+    public void save() {
9
+        System.out.println("target save");
10
+    }
11
+
12
+    @Override
13
+    public void save2() {
14
+        System.out.println("target save2");
15
+    }
16
+
17
+    @Override
18
+    public void throwException() {
19
+
20
+    }
21
+}

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

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

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

@@ -9,7 +9,6 @@ public class TargetImpl implements TargetInterface{
9
     @Override
9
     @Override
10
     public void save2() {
10
     public void save2() {
11
         System.out.println("target save2");
11
         System.out.println("target save2");
12
-        int i = 1/0;
13
     }
12
     }
14
 
13
 
15
     @Override
14
     @Override

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

@@ -32,4 +32,7 @@
32
             <aop:after method="after" pointcut-ref="save2" />
32
             <aop:after method="after" pointcut-ref="save2" />
33
         </aop:aspect>
33
         </aop:aspect>
34
     </aop:config>
34
     </aop:config>
35
+    <context:component-scan base-package="com.anno"></context:component-scan>
36
+<!--    aop自动代理-->
37
+    <aop:aspectj-autoproxy />
35
 </beans>
38
 </beans>