自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

開源框架spring詳解-----AOP的深刻理解

開發(fā) 后端
AOP是一種不同于OOP(面向?qū)ο缶幊?的編程模式,它不是OOP的替代,而是對(duì)OOP的一種有益補(bǔ)充。

AOP的理解

1、AOP的概述

AOP是一種不同于OOP(面向?qū)ο缶幊?的編程模式,它不是OOP的替代,而是對(duì)OOP的一種有益補(bǔ)充。

2、spring AOP的原理

3、spring AOP的實(shí)現(xiàn)

在spring2.5中,常用的AOP實(shí)現(xiàn)方式有兩種。第一種是基于xml配置文件方式的實(shí)現(xiàn),第二種是基于注解方式的實(shí)現(xiàn)。

接下來,以具體的是理智講解這兩種方式的使用。

Java代碼

  1. package com.zxf.service;     
  2.     
  3. /**    
  4.  * 業(yè)務(wù)邏輯接口    
  5.  * @author z_xiaofei168    
  6.  */    
  7. public interface AccountService {     
  8.     public void save(String loginname, String password);     
  9. }     
  10.     
  11. 它的實(shí)現(xiàn)類     
  12.     
  13. package com.zxf.service;     
  14. import com.zxf.dao.AccountDao;     
  15.     
  16. /**    
  17.  * AccountService的實(shí)現(xiàn)類    
  18.  * @author z_xiaofei168    
  19.  */    
  20. public class AccountServiceImpl implements AccountService {     
  21.     private  AccountDao accountDao;     
  22.          
  23.     public AccountServiceImpl() {}     
  24.          
  25.     /** 帶參數(shù)的構(gòu)造方法 */    
  26.     public AccountServiceImpl(AccountDao accountDao){     
  27.         this.accountDao = accountDao;     
  28.     }     
  29.          
  30.     public void save(String loginname, String password) {     
  31.         accountDao.save(loginname, password);     
  32.         throw new RuntimeException("故意拋出一個(gè)異常。。。。");     
  33.     }     
  34.          
  35.     /** set方法 */    
  36.     public void setAccountDao(AccountDao accountDao) {     
  37.         this.accountDao = accountDao;     
  38.     }     
  39. }  

 

對(duì)于業(yè)務(wù)系統(tǒng)來說,AccountServiceImpl類就是目標(biāo)實(shí)現(xiàn)類,它的業(yè)務(wù)方法,如save()方法的前后或代碼會(huì)出現(xiàn)異常的地方都是AOP的連接點(diǎn)。

下面是日志服務(wù)類的代碼:

Java代碼

  1. package com.zxf.aspect;     
  2.     
  3. import org.aspectj.lang.JoinPoint;     
  4. import org.aspectj.lang.ProceedingJoinPoint;     
  5.     
  6. /**    
  7.  * 日志切面類    
  8.  * @author z_xiaofei168    
  9.  */    
  10. public class LogAspect {     
  11.     
  12.     //任何通知方法都可以將第一個(gè)參數(shù)定義為 org.aspectj.lang.JoinPoint類型      
  13.     public void before(JoinPoint call) {     
  14.         //獲取目標(biāo)對(duì)象對(duì)應(yīng)的類名     
  15.         String className = call.getTarget().getClass().getName();     
  16.         //獲取目標(biāo)對(duì)象上正在執(zhí)行的方法名     
  17.         String methodName = call.getSignature().getName();     
  18.              
  19.         System.out.println("前置通知:" + className + "類的" + methodName + "方法開始了");     
  20.     }     
  21.          
  22.     public void afterReturn() {     
  23.         System.out.println("后置通知:方法正常結(jié)束了");     
  24.     }     
  25.          
  26.     public void after(){     
  27.         System.out.println("最終通知:不管方法有沒有正常執(zhí)行完成,一定會(huì)返回的");     
  28.     }     
  29.          
  30.     public void afterThrowing() {     
  31.         System.out.println("異常拋出后通知:方法執(zhí)行時(shí)出異常了");     
  32.     }     
  33.          
  34.     //用來做環(huán)繞通知的方法可以第一個(gè)參數(shù)定義為org.aspectj.lang.ProceedingJoinPoint類型     
  35.     public Object doAround(ProceedingJoinPoint call) throws Throwable {     
  36.         Object result = null;     
  37.         this.before(call);//相當(dāng)于前置通知     
  38.         try {     
  39.             result = call.proceed();     
  40.             this.afterReturn(); //相當(dāng)于后置通知     
  41.         } catch (Throwable e) {     
  42.     
  43.             this.afterThrowing();  //相當(dāng)于異常拋出后通知     
  44.             throw e;     
  45.         }finally{     
  46.             this.after();  //相當(dāng)于最終通知     
  47.         }     
  48.              
  49.         return result;     
  50.     }     
  51. }   

 

這個(gè)類屬于業(yè)務(wù)服務(wù)類,如果用AOP的術(shù)語(yǔ)來說,它就是一個(gè)切面類,它定義了許多通知。Before()、afterReturn()、after()和afterThrowing()這些方法都是通知。

#p#

<1>.基于xml配置文件的AOP實(shí)現(xiàn)

這種方式在實(shí)現(xiàn)AOP時(shí),有4個(gè)步驟。

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:aop="http://www.springframework.org/schema/aop"    
  5.         xsi:schemaLocation="     
  6.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd     
  7.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd>    
  8.     
  9.     <bean id="accountDaoImpl" class="com.zxf.dao.AccountDaoImpl"/>    
  10.          
  11.     <bean id="accountService" class="com.zxf.service.AccountServiceImpl">    
  12.         <property name=" accountDaoImpl " ref=" accountDaoImpl "/>    
  13.     bean>    
  14.     
  15.         
  16.     <bean id="logAspectBean" class="com.zxf.aspect.LogAspect"/>    
  17.          
  18.         
  19.     <aop:config>    
  20.             
  21.         <aop:aspect id="logAspect" ref="logAspectBean">    
  22.                 
  23.             <aop:pointcut id="allMethod"      
  24.                 expression="execution(* com.zxf.service.*.*(..))"/>    
  25.                      
  26.                 
  27.             <aop:before method="before" pointcut-ref="allMethod" />    
  28.                 
  29.             <aop:after-returning method="afterReturn" pointcut-ref="allMethod"/>    
  30.                 
  31.             <aop:after method="after" pointcut-ref="allMethod"/>    
  32.                 
  33.             <aop:after-throwing method="afterThrowing" pointcut-ref="allMethod"/>    
  34.                  
  35.                 
  36.                 
  37.         aop:aspect>    
  38.     aop:config>    
  39. beans>    

 

 

 

上述配置針對(duì)切入點(diǎn)應(yīng)用了前置、后置、最終,以及拋出異常后通知。這樣在測(cè)試執(zhí)行AccountServiceImpl類的save()方法時(shí),控制臺(tái)會(huì)有如下結(jié)果輸出。

前置通知:com.zxf.service.AccountServiceImpl類的save方法開始了。

針對(duì)MySQL的AccountDao實(shí)現(xiàn)中的save()方法。

后置通知:方法正常結(jié)束了。

最終通知:不管方法有沒有正常執(zhí)行完成,一定會(huì)返回的。

<2>基于注解的AOP的實(shí)現(xiàn)

首先創(chuàng)建一個(gè)用來作為切面的類LogAnnotationAspect,同時(shí)把這個(gè)類配置在spring的配置文件中。

在spring2.0以后引入了JDK5.0的注解Annotation的支持,提供了對(duì)AspectJ基于注解的切面的支持,從而 更進(jìn)一步地簡(jiǎn)化AOP的配置。具體的步驟有兩步。

Spring的配置文件是如下的配置:

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:aop="http://www.springframework.org/schema/aop"    
  5.         xsi:schemaLocation="     
  6.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd     
  7.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd>    
  8.     
  9.     <bean id="accountDao" class="com.zxf.dao.AccountDaoImpl"/>    
  10.     <bean id="accountService" class="com.zxf.service.AccountServiceImpl">    
  11.         <property name="accountDao" ref="accountDao"/>    
  12.     bean>    
  13.         
  14.     <bean id="logAspectBean" class="com.zxf.aspect.LogAnnotationAspect"/>    
  15.         
  16.     <aop:aspectj-autoproxy/>    
  17. beans>   

 

 

這是那個(gè)切面的類LogAnnotationAspect

Java代碼

  1. package com.zxf.aspect;     
  2.     
  3. import org.aspectj.lang.JoinPoint;     
  4. import org.aspectj.lang.ProceedingJoinPoint;     
  5. import org.aspectj.lang.annotation.After;     
  6. import org.aspectj.lang.annotation.AfterReturning;     
  7. import org.aspectj.lang.annotation.AfterThrowing;     
  8. import org.aspectj.lang.annotation.Aspect;     
  9. import org.aspectj.lang.annotation.Before;     
  10. import org.aspectj.lang.annotation.Pointcut;     
  11.     
  12. /**    
  13.  * 日志切面類    
  14.  */    
  15. @Aspect  //定義切面類     
  16. public class LogAnnotationAspect {     
  17.     @SuppressWarnings("unused")     
  18.     //定義切入點(diǎn)     
  19.     @Pointcut("execution(* com.zxf.service.*.*(..))")     
  20.     private void allMethod(){}     
  21.          
  22.     //針對(duì)指定的切入點(diǎn)表達(dá)式選擇的切入點(diǎn)應(yīng)用前置通知     
  23.     @Before("execution(* com. zxf.service.*.*(..))")     
  24.     public void before(JoinPoint call) {     
  25.              
  26.         String className = call.getTarget().getClass().getName();     
  27.         String methodName = call.getSignature().getName();     
  28.              
  29.         System.out.println("【注解-前置通知】:" + className + "類的"      
  30.                 + methodName + "方法開始了");     
  31.     }     
  32.     //訪問命名切入點(diǎn)來應(yīng)用后置通知     
  33.     @AfterReturning("allMethod()")     
  34.     public void afterReturn() {     
  35.         System.out.println("【注解-后置通知】:方法正常結(jié)束了");     
  36.     }     
  37.          
  38.     //應(yīng)用最終通知     
  39.     @After("allMethod()")     
  40.     public void after(){     
  41.         System.out.println("【注解-最終通知】:不管方法有沒有正常執(zhí)行完成,"      
  42.                 + "一定會(huì)返回的");     
  43.     }     
  44.          
  45.     //應(yīng)用異常拋出后通知     
  46.     @AfterThrowing("allMethod()")     
  47.     public void afterThrowing() {     
  48.         System.out.println("【注解-異常拋出后通知】:方法執(zhí)行時(shí)出異常了");     
  49.     }     
  50.          
  51.     //應(yīng)用周圍通知     
  52.     //@Around("allMethod()")     
  53.     public Object doAround(ProceedingJoinPoint call) throws Throwable{     
  54.         Object result = null;     
  55.         this.before(call);//相當(dāng)于前置通知     
  56.         try {     
  57.             result = call.proceed();     
  58.             this.afterReturn(); //相當(dāng)于后置通知     
  59.         } catch (Throwable e) {     
  60.             this.afterThrowing();  //相當(dāng)于異常拋出后通知     
  61.             throw e;     
  62.         }finally{     
  63.             this.after();  //相當(dāng)于最終通知     
  64.         }     
  65.              
  66.         return result;     
  67.     }     
  68. }    

 

備注:輸出結(jié)果和前面的一樣。

【編輯推薦】

  1. Spring Hibernate簡(jiǎn)單討論
  2. OSGi與Spring:設(shè)置Spring DM開發(fā)環(huán)境
  3. 使用Spring DM創(chuàng)建Hello World,以及OSGi服務(wù)
  4. Spring MVC總結(jié):善用注解,生活更輕松
  5. 概括spring hibernate集成

責(zé)任編輯:金賀 來源: ITEYE博客
相關(guān)推薦

2017-01-13 08:52:46

HDFS機(jī)制Then

2024-06-24 08:31:42

2012-12-31 14:59:58

Android開發(fā)Layout_weig

2024-05-21 08:44:43

MySQLB+Tree內(nèi)存

2011-04-18 19:36:10

HSRP協(xié)議

2011-03-14 13:11:07

Oracle數(shù)據(jù)庫(kù)

2020-09-20 22:14:14

編程PythonJava

2010-08-02 10:11:51

DB2數(shù)據(jù)庫(kù)編目

2016-11-03 08:57:02

javascriptjquerynode.js

2022-12-04 09:19:25

JAVA并發(fā)有序性

2022-06-07 07:58:45

SpringSpring AOP

2012-06-21 10:00:25

團(tuán)隊(duì)合作程序員

2009-09-29 10:00:40

Spring AOP框

2022-06-08 08:04:28

Springservicerepository

2021-05-06 18:17:52

SpringAOP理解

2011-09-15 10:15:30

Spring

2022-04-26 08:41:54

JDK動(dòng)態(tài)代理方法

2023-07-03 07:39:43

Spring框架設(shè)計(jì)模式

2019-05-10 10:50:04

Spring AOPJDK動(dòng)態(tài)代理CGLIB動(dòng)態(tài)代理

2015-05-06 10:05:22

javajava框架spring aop
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)