成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

AOP的理解

1、AOP的概述

AOP是一種不同于OOP(面向?qū)ο缶幊?的編程模式,它不是OOP的替代,而是對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("故意拋出一個異常。。。。");     
  33.     }     
  34.          
  35.     /** set方法 */    
  36.     public void setAccountDao(AccountDao accountDao) {     
  37.         this.accountDao = accountDao;     
  38.     }     
  39. }  

 

對于業(yè)務(wù)系統(tǒng)來說,AccountServiceImpl類就是目標(biāo)實(shí)現(xiàn)類,它的業(yè)務(wù)方法,如save()方法的前后或代碼會出現(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.     //任何通知方法都可以將第一個參數(shù)定義為 org.aspectj.lang.JoinPoint類型      
  13.     public void before(JoinPoint call) {     
  14.         //獲取目標(biāo)對象對應(yīng)的類名     
  15.         String className = call.getTarget().getClass().getName();     
  16.         //獲取目標(biāo)對象上正在執(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í)行完成,一定會返回的");     
  28.     }     
  29.          
  30.     public void afterThrowing() {     
  31.         System.out.println("異常拋出后通知:方法執(zhí)行時(shí)出異常了");     
  32.     }     
  33.          
  34.     //用來做環(huán)繞通知的方法可以第一個參數(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. }   

 

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

#p#

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

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

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>    

 

 

 

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

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

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

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

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

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

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

在spring2.0以后引入了JDK5.0的注解Annotation的支持,提供了對AspectJ基于注解的切面的支持,從而 更進(jì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>   

 

 

這是那個切面的類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.     //針對指定的切入點(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.                 + "一定會返回的");     
  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簡單討論
  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ù)庫

2020-09-20 22:14:14

編程PythonJava

2010-08-02 10:11:51

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

2016-11-03 08:57:02

javascriptjquerynode.js

2022-12-04 09:19:25

JAVA并發(fā)有序性

2012-06-21 10:00:25

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

2022-06-07 07:58:45

SpringSpring AOP

2009-09-29 10:00:40

Spring AOP框

2022-06-08 08:04:28

Springservicerepository

2025-05-29 04:00:00

2021-05-06 18:17:52

SpringAOP理解

2011-09-15 10:15:30

Spring

2022-04-26 08:41:54

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

2023-07-03 07:39:43

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

2019-05-10 10:50:04

Spring AOPJDK動態(tài)代理CGLIB動態(tài)代理
點(diǎn)贊
收藏

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

主站蜘蛛池模板: a级毛片国产 | 最新国产精品视频 | 99在线观看视频 | 国产日韩一区二区三区 | 亚洲 欧美 日韩 在线 | 狠狠夜夜| 特级特黄特色的免费大片 | www.国产一区 | 日韩av在线一区二区 | 国产乱码精品一区二三赶尸艳谈 | 黑人巨大精品欧美一区二区免费 | 国产一区二区影院 | 国产精品夜间视频香蕉 | 性做久久久久久免费观看欧美 | 看片国产 | 久久精品亚洲 | 一区二区三区亚洲视频 | 免费在线观看av网址 | 成人在线中文 | 亚洲网站在线观看 | 91在线电影 | 久久久精品在线 | 香蕉久久久久久 | 久久精品亚洲一区二区三区浴池 | 精精国产视频 | 欧美三级在线 | 特级丰满少妇一级aaaa爱毛片 | 午夜日韩 | 久久国产精99精产国高潮 | 欧美激情一区二区 | 超碰在线久| 免费看片国产 | 黄片毛片在线观看 | 成人国产精品免费观看 | 欧美成人一区二区三区 | 国产高清在线视频 | 精品欧美一区二区三区久久久 | 久久国产欧美日韩精品 | 国产一二三区在线 | 欧美在线观看一区二区 | 日韩激情免费 |