spring之AOP基本概念和AOP的三種實現方式

AOP基本概念


 關於AOP的一些概念性詞語的解釋:

切面(Aspect):一個關注點的模塊化,這個關注點可能會橫切多個對象。事務管理是J2EE應用中一個關於橫切關注點的很好的例子。在Spring AOP中,切面可以使用基於模式)或者基於Aspect註解方式來實現。通俗點說就是我們加入的切面類(比如log類),可以這麼理解。

連接點(Joinpoint:在程序執行過程中某個特定的點,比如某方法調用的時候或者處理異常的時候。在Spring AOP中,一個連接點總是表示一個方法的執行。通俗的說就是加入切點的那個點

通知(Advice:在切面的某個特定的連接點上執行的動作。其中包括了“around”“before”“after”等不同類型的通知(通知的類型將在後面部分進行討論)。許多AOP框架(包括Spring)都是以攔截器做通知模型,並維護一個以連接點爲中心的攔截器鏈。

切入點(Pointcut:匹配連接點的斷言。通知和一個切入點表達式關聯,並在滿足這個切入點的連接點上運行(例如,當執行某個特定名稱的方法時)。切入點表達式如何和連接點匹配是AOP的核心:Spring缺省使用AspectJ切入點語法。

引入(Introduction:用來給一個類型聲明額外的方法或屬性(也被稱爲連接類型聲明(inter-type declaration))。Spring允許引入新的接口(以及一個對應的實現)到任何被代理的對象。例如,你可以使用引入來使一個bean實現IsModified接口,以便簡化緩存機制。

目標對象(Target Object: 被一個或者多個切面所通知的對象。也被稱做被通知(advised)對象。 既然Spring AOP是通過運行時代理實現的,這個對象永遠是一個被代理(proxied)對象。

AOP代理(AOP ProxyAOP框架創建的對象,用來實現切面契約(例如通知方法執行等等)。在Spring中,AOP代理可以是JDK動態代理或者CGLIB代理。

織入(Weaving:把切面連接到其它的應用程序類型或者對象上,並創建一個被通知的對象。這些可以在編譯時(例如使用AspectJ編譯器),類加載時和運行時完成。Spring和其他純Java AOP框架一樣,在運行時完成織入。

AOP的三種實現方式

<!-- 
1、配置形式:最容易理解
2、註解形式:代碼量最少
3、正則形式:	功能最強
 -->

例子:


一、AOP註解實現

@Aspect
public class LoggerAdvice {
@Pointcut("execution(* com.javalxj.spring.*.service..*mpl.*(String,*))")
public void method(){

}
@After("method()")
public void after(){
System.out.println("AOP:After");

}---

---------改進------

public class LoggerAdvice {
private static final String EXPRESSION="execution(* com.javalxj.spring.*.service..*mpl.*(String,*))";
@After(EXPRESSION)
public void after(){
System.out.println("AOP:After");
}

------註解實現五種通知方式----

public class LoggerAdvice {
private static final String EXPRESSION="execution(* com.javalxj.spring.*.service..*mpl.*(String,*))";
@After(EXPRESSION)
public void after(){
System.out.println("AOP:After");
}
@Before(EXPRESSION)
public void before(){
System.out.println("AOP:Before");
}
@AfterReturning(EXPRESSION)
public void afterReturning(){
System.out.println("AOP:afterReturning");
}
@AfterThrowing(EXPRESSION)
public void afterThrowing(){
System.out.println("AOP:afterThrowing");
}
/**
* around總是最先執行
* 配置文件報錯找錯輕鬆註解方式的文件找錯不容易
* @param pjp

*/
@Around(EXPRESSION)
public void around(ProceedingJoinPoint pjp){
System.out.println("AOP:around");
try {
pjp.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

--------------------------------------------帶參數的註解-----------------------------------

@Pointcut("execution(* com.javalxj.spring.*.service..*mpl.*(String,*))")
public void method(){

}
/**帶參數的攔截:註釋參數名稱必須與方法中的參數名稱相同
* 問題提出:參數多個 ,並且類型不同怎麼辦
* 提示:參數個數相同的時候可以用OBject 參數類,然後進方法內轉化執行判斷強轉:
*/
@After("method() and args(name,age)")
public void after(String name,int age){

System.out.println("AOP:After----"+name);
}

----------------------------------配置形式-------------------------

<?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"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd>
    <bean id="registerDaoImpl" class="com.zxf.dao.RegisterDaoImpl"/>
    <bean id="registerService" class="com.zxf.service.RegisterServiceImpl">
        <property name=" registerDaoImpl " ref=" RegisterDaoImpl "/>
    </bean>
    <!-- 日誌切面類 -->
    <bean id="logAspectBean" class="com.zxf.aspect.LogAspect"/>
    <!-- 第1步: AOP的配置 -->
    <aop:config>
        <!-- 第2步:配置一個切面 -->
        <aop:aspect id="logAspect" ref="logAspectBean">
            <!-- 第3步:定義切入點,指定切入點表達式 -->
            <aop:pointcut id="allMethod" 
                expression="execution(* com.zxf.service.*.*(..))"/> 
            <!-- 第4步:應用前置通知 -->
            <aop:before method="before" pointcut-ref="allMethod" />
            <!-- 第4步:應用後置通知 -->
            <aop:after-returning method="afterReturn" pointcut-ref="allMethod"/>
            <!-- 第4步:應用最終通知 -->
            <aop:after method="after" pointcut-ref="allMethod"/>
            <!-- 第4步:應用拋出異常後通知 -->
            <aop:after-throwing method="afterThrowing" pointcut-ref="allMethod"/>
            <!-- 第4步:應用環繞通知 -->
            <!-- 
            <aop:around method="doAround" pointcut-ref="allMethod" />
             -->
        </aop:aspect>
    </aop:config>
</beans>


---------------------------------------正則形式-----------------------------

<?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"
xsi:schemaLocation=
" http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<!-- 
1、配置形式:最容易理解
2、註解形式:代碼量最少
3、正則形式:	功能最強
 -->
<!-- 邏輯層實現類 -->
<bean id="aopService" class="com.javalxj.spring.aop.service.impl.AopServiceImpl">
	<property name="aopDao" ref="aopDao"></property>
</bean>
<!-- 數據層實現類 -->
<bean id="aopDao" class="com.javalxj.spring.aop.dao.impl.AopDaoImpl">
</bean>
<!-- 日誌通知實現類 -->
<bean id="loggerAdvice" class="com.javalxj.spring.aop.advice.LogAdvice">
</bean>

<!-- 基於正則的配置表達式 
1、定義一個切入點注入.(所有類中的)*create//以create結尾的方法==爲了攔截符合要求的方法
-->
<bean id="pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
	<property name="pattern" value=".*create"></property>
</bean>
<!-- 基於正則的配置表達式 
2、定義一個通知管理器
-->
<bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
	<property name="advice" ref="loggerAdvice"></property>
	<property name="pointcut" ref="pointcut"></property>
</bean>
<!-- 基於正則的配置表達式 
3、定義代理實現對象
	target目標對象               有攔截對象的
	interceptorNames     攔截器
	proxyInterfaces      接口
-->
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
	<property name="target" ref="aopService"/> 
	<property name="interceptorNames" value="advisor" />
	<property name="proxyInterfaces"  value="com.javalxj.spring.aop.service.AopService"></property>
</bean>
<!-- AOP代理 配置
<aop:config>
	<aop:aspect ref="loggerAdvice">
		<aop:pointcut expression="execution(* com.javalxj.spring.*.service..*mpl.*(String,*))" id="method"/>
		<aop:around pointcut-ref="method" method="around"/>
	</aop:aspect>
	
</aop:config>
-->



<!--啓用AOP註解支持
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
-->


</beans>


發佈了46 篇原創文章 · 獲贊 17 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章