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前置通知
package?org.guangsoft.utils;import?java.lang.reflect.Method;
import?java.util.Arrays;
import?java.util.Date;
import?org.springframework.aop.MethodBeforeAdvice;
/****
*?前置增強: *?MethodBeforeAdvice?接口表示重寫的方法為前置advice *?***/public?class?BeforeLog?implements?MethodBeforeAdvice
{
@Override
public?void?before(Method?method,
Object[]?args,?Object?obj)
throws?Throwable
{
System.out.println(method);
System.out.println(Arrays.toString(args));
System.out.println(obj);
System.out.println("BeforeLog-------------"?+?new?Date());
}
}
AOP配置:
<?xml?version="1.0"?encoding="UTF-8"?><!--?到入xml文件的約束?-->
<beans?xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"?xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"><!--?實例化BeforeLog對象?-->
<bean?id="bf"?class="org.guangsoft.utils.BeforeLog"></bean>
<!--?實例化service對象?-->
<bean?id="us"?class="org.guangsoft.service.impl.UsersServiceImpl"?/>
<!--?進行aop的配置,產生代理對象?-->
<aop:config>
<!--?聲明切入點?-->
<aop:pointcut?expression="execution(*?org.guansoft.service.impl.*.*(..))"
id="pc"?/>
<!--?織入?將通知和切入點進行合並(切面+核心業務對象)?-->
<aop:advisor?advice-ref="bf"?pointcut-ref="pc"?/>
</aop:config>
</beans>
3.1.2後置通知
對業務對象的方法進行後增強。
package?org.guangsoft.utils;import?java.lang.reflect.Method;
import?java.util.Date;
import?org.springframework.aop.AfterReturningAdvice;
/***
*?後置通知 *?***/public?class?AfterLog?implements?AfterReturningAdvice
{
@Override
public?void?afterReturning(Object?obj1,//?obj1?接收目標方法的返回值
Method?method,
Object[]?args,
Object?obj2)?throws?Throwable
{
//?System.out.println(obj1+"----------------------"+obj2);
System.out.println("AfterLog-------------------"?+?new?Date());
}
}
AOP配置:
<?xml?version="1.0"?encoding="UTF-8"?><!--?到入xml文件的約束?-->
<beans?xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"?xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"><!--?實例化BeforeLog對象?-->
<bean?id="bf"?class="org.guangsoft.utils.BeforeLog"></bean>
<bean?id="af"?class="org.guangsoft.utils.AfterLog"></bean>
<!--?實例化service對象?-->
<bean?id="us"?class="org.guangsoft.service.impl.UsersServiceImpl"?/>
<!--?進行aop的配置,產生代理對象?-->
<aop:config>
<!--?聲明切入點?-->
<aop:pointcut?expression="execution(*?org.guangsoft.service.impl.*.*(..))"
id="pc"?/>
<!--?織入?將通知和切入點進行合並(切面+核心業務對象)?-->
<aop:advisor?advice-ref="bf"?pointcut-ref="pc"?/>
<aop:advisor?advice-ref="af"?pointcut-ref="pc"?/>
</aop:config>
</beans>
3.1.3環繞通知
package?org.guangsoft.utils;import?java.lang.reflect.Method;
import?java.util.Arrays;
import?java.util.Date;
import?org.aopalliance.intercept.MethodInterceptor;
import?org.aopalliance.intercept.MethodInvocation;
/***
*?環繞通知 *?***/public?class?AoundLog?implements?MethodInterceptor
{
/**
*?MethodInvocation中封裝了目標對象,調用的方法,方法需要的參數 *?***/@Override
public?Object?invoke(MethodInvocation?mi)?throws?Throwable
{
Method?method?=?mi.getMethod();
Object[]?args?=?mi.getArguments();
Object?obj?=?mi.getThis();
System.out.println(method);
System.out.println(Arrays.toString(args));
System.out.println(obj);
System.out.println("around------before--------"?+?new?Date());
Object?rv?=?method.invoke(obj,?args);//?調用目標對象的方法,放行
System.out.println("around------after--------"?+?new?Date());
return?rv;
}
}
AOP配置:同上
3.1.4 異常通知
package?org.guangsoft.utils;import?java.util.Date;
import?org.springframework.aop.ThrowsAdvice;
/****
*?異常通知 *?**/public?class?ExceptionLog?implements?ThrowsAdvice
{
/***
*?該類中的方法參考AfterReturningAdvice寫 *?該參數是用來接收異常信息的 *?***/public?void?afterThrowing(Throwable?ex)?throws?Throwable
{
//?System.out.println(obj1+"----------------------"+obj2);
System.out.println("ExceptionLog-----------"?+?ex.getMessage()
+?"--------"?+?new?Date());
}
}
Pointcut:核心業務對象
Advice:通知