Spring面向切面編程(AOP)


1 spring容器中bean特性

Spring容器的javabean對象默認是單例的。

通過在xml文件中,配置可以使用某些對象為多列。

Spring容器中的javabean對象默認是立即加載(立即實例化:spring加載完成,立即創建對象)

scope:屬性

        singleton:默認值為單例,默認也是立即加載,在加載完成spring容器的時候,bean對象已經創建完成   

        prototype:多例的,默認懶加載,spring容器加載完成的時候,不會創建bean的對象,只有從容器獲得bean對象的時候,才進行bean對象的實例化

        request: 將創建的javabean對象,封裝到request范圍

        session:將創建的javabean對象,封裝到session范圍

Spring容器bean的對象生命周期:

Bean對象的創建一直到銷毀為bean的生命周期。

         生命周期的開始:

         如果為單例,由加載完spring容器開始

         如果為多例,由從容器獲得bean對象開始

         實例化

         初始化

         服務

         銷毀(單例:關閉容器的時候,多例由jvm自動回收)

2 spring的AOP面向切面編程

2.1 模擬銀行轉賬業務

需求:實現銀行的轉賬功能,在轉賬的時候需要完成

1 身份認證(登陸)

2 權限的驗證

3 轉賬實現

4 歷史交易記錄,

分析:1,2,4三個功能對於銀行的業務,屬於公共的功能(共性的功能)

在功能實現的時候,需要將1,2,4抽取出來,單獨實現,

做到了將共性的功能和核心的業務功能進行了分離

通過動態代理實現:共性的功能和核心業務功能的合並,產生核心業務對象的

 在代碼實現的時候,進行了功能實現的分離:

 代碼開發的進行分離,程序在運行的時候進行合並。

2.2 springAOP的思想

在系統開發中,將系統的共性的公共的功能獨立實現,在程序運行的過程中,將共性功能和核心的業務功能,進行整合。

好處:

1 完成共性功能和核心業務功能的解耦合

2 提供共性功能的復用性。

2.3springAOP的概念 

Aspect切面:封裝共性功能的(增強功能的)類

Advice通過:切面類中封裝的增強功能的方法。

PointCut:切入點,是一個集合的概念,該集合的表達使用一個正則表達式表達

      所有核心業務對象的所有方法的前后(事務處理AOP典型的應用)

JoinPoint:連接點,程序中需要加入advice的地方,而且正在執行的ponitCut

織入(Weaving):將aspect和核心業務對象,進行整合的過程。

3 springAOP的實現

3.1通過特定接口實現

Aop通知的類型:

      Before:前置通知

      After:后置通知

      Around:環繞通知

      Throwing:異常通知

需求:實現在業務對象中的方法執行的時候,記錄日志功能

3.1.1前置通知

 1 package org.guangsoft.utils;  2 import java.lang.reflect.Method;  3 import java.util.Arrays;  4 import java.util.Date;  5 import org.springframework.aop.MethodBeforeAdvice;  6 /****  7  * 前置增強:  8  * MethodBeforeAdvice 接口表示重寫的方法為前置advice  9  * ***/
10 public class BeforeLog implements MethodBeforeAdvice 11 { 12  @Override 13     public void before(Method method, 14  Object[] args, Object obj) 15     throws Throwable 16  { 17  System.out.println(method); 18  System.out.println(Arrays.toString(args)); 19  System.out.println(obj); 20         System.out.println("BeforeLog-------------" + new Date()); 21  } 22 }

AOP配置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!-- 到入xml文件的約束 -->
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4  xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
 5  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6  xsi:schemaLocation="http://www.springframework.org/schema/beans  7  http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  8  http://www.springframework.org/schema/aop  9  http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
10     <!-- 實例化BeforeLog對象 -->
11     <bean id="bf" class="org.guangsoft.utils.BeforeLog"></bean>
12     <!-- 實例化service對象 -->
13     <bean id="us" class="org.guangsoft.service.impl.UsersServiceImpl" />
14     <!-- 進行aop的配置,產生代理對象 -->
15     <aop:config>
16         <!-- 聲明切入點 -->
17         <aop:pointcut expression="execution(* org.guansoft.service.impl.*.*(..))"
18  id="pc" />
19         <!-- 織入 將通知和切入點進行合並(切面+核心業務對象) -->
20         <aop:advisor advice-ref="bf" pointcut-ref="pc" />
21     </aop:config>
22 </beans>
23  

3.1.2后置通知

對業務對象的方法進行后增強。

 1 package org.guangsoft.utils;  2 import java.lang.reflect.Method;  3 import java.util.Date;  4 import org.springframework.aop.AfterReturningAdvice;  5 /***  6  * 后置通知  7  * ***/
 8 public class AfterLog implements AfterReturningAdvice  9 { 10  @Override 11     public void afterReturning(Object obj1,// obj1 接收目標方法的返回值
12  Method method, 13  Object[] args, 14             Object obj2) throws Throwable 15  { 16         // System.out.println(obj1+"----------------------"+obj2);
17         System.out.println("AfterLog-------------------" + new Date()); 18  } 19

AOP配置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!-- 到入xml文件的約束 -->
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4  xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
 5  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6  xsi:schemaLocation="http://www.springframework.org/schema/beans  7  http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  8  http://www.springframework.org/schema/aop  9  http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
10     <!-- 實例化BeforeLog對象 -->
11     <bean id="bf" class="org.guangsoft.utils.BeforeLog"></bean>
12     <bean id="af" class="org.guangsoft.utils.AfterLog"></bean>
13     <!-- 實例化service對象 -->
14     <bean id="us" class="org.guangsoft.service.impl.UsersServiceImpl" />
15     <!-- 進行aop的配置,產生代理對象 -->
16     <aop:config>
17         <!-- 聲明切入點 -->
18         <aop:pointcut expression="execution(* org.guangsoft.service.impl.*.*(..))"
19  id="pc" />
20         <!-- 織入 將通知和切入點進行合並(切面+核心業務對象) -->
21         <aop:advisor advice-ref="bf" pointcut-ref="pc" />
22         <aop:advisor advice-ref="af" pointcut-ref="pc" />
23     </aop:config>
24 </beans>
25  

3.1.3環繞通知

 1 package org.guangsoft.utils;  2 import java.lang.reflect.Method;  3 import java.util.Arrays;  4 import java.util.Date;  5 import org.aopalliance.intercept.MethodInterceptor;  6 import org.aopalliance.intercept.MethodInvocation;  7 /***  8  * 環繞通知  9  * ***/
10 public class AoundLog implements MethodInterceptor 11 { 12     /**
13  * MethodInvocation中封裝了目標對象,調用的方法,方法需要的參數 14  * ***/
15  @Override 16     public Object invoke(MethodInvocation mi) throws Throwable 17  { 18         Method method = mi.getMethod(); 19         Object[] args = mi.getArguments(); 20         Object obj = mi.getThis(); 21  System.out.println(method); 22  System.out.println(Arrays.toString(args)); 23  System.out.println(obj); 24         System.out.println("around------before--------" + new Date()); 25         Object rv = method.invoke(obj, args);// 調用目標對象的方法,放行
26         System.out.println("around------after--------" + new Date()); 27         return rv; 28  } 29 }

AOP配置:同上

3.1.4 異常通知

 1 package org.guangsoft.utils;  2 import java.util.Date;  3 import org.springframework.aop.ThrowsAdvice;  4 /****  5  * 異常通知  6  * **/
 7 public class ExceptionLog implements ThrowsAdvice  8 {  9     /*** 10  * 該類中的方法參考AfterReturningAdvice寫 11  * 該參數是用來接收異常信息的 12  * ***/
13     public void afterThrowing(Throwable ex) throws Throwable 14  { 15         // System.out.println(obj1+"----------------------"+obj2);
16         System.out.println("ExceptionLog-----------" + ex.getMessage() 17                 + "--------" + new Date()); 18  } 19 }

Pointcut:核心業務對象

Advice:通知

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM