1. 程式人生 > >AOP基礎:繼承接口方式實現:前置通知、後置通知、異常通知、環繞通知 (demo)

AOP基礎:繼承接口方式實現:前置通知、後置通知、異常通知、環繞通知 (demo)

widget pointcut 1.0 add contex 分享圖片 lns stat ado

運行環境

    ide: Spring Tool Suite 3 Version: 3.9.8.RELEASE

    jdk:   1.8

    系統:  windows10

主要內容:

  • 前置通知:  在方法執行之前通知。
  • 後置通知:  在方法執行之後通知。
  • 異常通知:  在方法出現異常時通知。
  • 環繞通知:  全方位控制,可以在其中實現前三種通知。

它們需要繼承的接口如下圖:

?技術分享圖片技術分享圖片??

a:需要導入的jar包:

aopaliance.jar
aspectjweaver.jar

spring-aop.jar 開發AOP特性時需要的JAR

spring-beans.jar 處理Bean的jar <bean>
spring-context.jar 處理spring上下文的jar <context>
spring-core.jar spring核心jar
spring-expression.jar spring表達式

commons-logging.jar

b.配置xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans "

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx "
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

c:編寫

  分為四個文件:

    student.java:        其中的add()方法是切入點。

    applicationContext.xml:   配置

    test.java:           測試。

    四個通知類型各新建一個對應的繼承接口的類。(依照上圖來繼承)

前置通知:

student.java:

1 package springProject;
2  
3 public class Student {
4     public void add() {
5         System.out.println("  add");
6     }
7 }

applicationContext.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:aop="http://www.springframework.org/schema/aop"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
 9         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
10     
11     <bean id="student" class="springProject.Student">
12     </bean>
13     
14     <bean id="logBefore" class="aop.before.loginBefore">
15     </bean>
16     
17     <aop:config>
18         <!-- 配置切入點  (在哪裏執行通知 ) -->
19         <!-- =========連接線的另一方========= -->
20         <aop:pointcut expression="execution(public * springProject.Student.add(..))"   id="poioncut"/>
21         <!-- advisor:相當於 鏈接切入點 和切面的線  -->                
22         <!-- =========連接線========= -->
23         <aop:advisor advice-ref="logBefore" pointcut-ref="poioncut"/>
24     </aop:config>
25 </beans>

test.java:

 1 package springProject;
 2  
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5  
 6 public class test {
 7     public static void main(String[] args) {
 8         ApplicationContext conext = new ClassPathXmlApplicationContext("applicationContext.xml");
 9         Student student=(Student)conext.getBean("student");
10         //System.out.println(student);
11         student.add();
12     }
13 }

loginBefore.java:

package aop.before;
 
import java.lang.reflect.Method;
 
import org.springframework.aop.MethodBeforeAdvice;
 
public class loginBefore implements MethodBeforeAdvice{
    @Override
    public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
        System.out.println("前置通知");
    }
}

執行結果:

技術分享圖片

後置通知:

與前置通知幾乎一樣。

在xml中增加如下部分:

1 <bean id="loginAfter" class="aop.after.loginAfter"></bean>
3 <aop:config>
4     <aop:pointcut expression="execution(public * springProject.Student.add(..))" id="pointcut"/>
5     <aop:advisor advice-ref="loginAfter" pointcut-ref="pointcut"/>
6 </aop:config> 

loginAfter.java

 1 package aop.after;
 2 
 3 import java.lang.reflect.Method;
 5 import org.springframework.aop.AfterReturningAdvice;
7 public class loginAfter implements AfterReturningAdvice{ 9 @Override 10 public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { 11 System.out.println("後置通知"); 12 } 13 }

其他兩個文件不用變。

執行結果:

技術分享圖片

異常通知:

xml中增加如下:

1 <bean id="loginThrow" class="aop.throwA.logThrow"></bean>
2 <aop:config>
3     <aop:pointcut expression="execution(public * springProject.Student.add(..))" id="pointcut"/>
4     <aop:advisor advice-ref="loginThrow" pointcut-ref="pointcut"/>
5 </aop:config> 

student.java修改一下:

 1  package springProject;
 2 2  
 3  public class Student {
 4     public void add() {
 5        String aString =null;//增加了一個異常
 6       aString.charAt(10);
 7 
 8         System.out.println("  add");
 9     }
10  }

執行結果:

技術分享圖片

環繞通知:

loginInvoker.java:

 1 package aop.invoke;
 2 
 3 import org.aopalliance.intercept.MethodInterceptor;
 4 import org.aopalliance.intercept.MethodInvocation;
 5 
 6 public class loginInvoke  implements MethodInterceptor{
 7 
 8     @Override
 9     public Object invoke(MethodInvocation invocation) throws Throwable {
10         Object result  = null ;
11         //方法體1...
12         try {
13             //方法體2...
14             System.out.println("用環繞通知實現的[前置通知]...");
15             
16             // invocation.proceed() 之前的代碼:前置通知
17              result  = invocation.proceed() ;//控制著目標方法的執行  ,add()
18             //result 就是目標方法addStudent()方法的返回值
19 //             invocation.proceed() 之後的代碼:後置通知
20             System.out.println("用環繞通知實現的[後置通知]...:");
21             System.out.println("-----------------目標對象target"+invocation.getThis()+",調用的方法名:"+invocation.getMethod().getName()+",方法的參數個數:"+invocation.getArguments().length+",返回值:"+result);
22         }catch(Exception e) {
23             //方法體3...
24             //異常通知
25             System.out.println("用環繞通知實現的[異常通知]...");
26         }
27         
28         return result;//目標方法的返回值(這裏可以設置為任何你想設置的值。不推薦這樣做。)
29     }
30 }

xml增加如下部分:

<bean id="loginInvoke" class="aop.invoke.loginInvoke"></bean>
<aop:config>
    <aop:pointcut expression="execution(public * springProject.Student.add(..))" id="pointcut"/>
    <aop:advisor advice-ref="loginInvoke" pointcut-ref="pointcut"/>
</aop:config>

AOP基礎:繼承接口方式實現:前置通知、後置通知、異常通知、環繞通知 (demo)