亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

如何在不使用spring框架中使用aop的功能

 更新時間:2022年01月03日 11:20:26   作者:Brrby  
這篇文章主要介紹了如何在不使用spring框架中使用aop的功能,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Spring框架的AOP機制可以讓開發(fā)者把業(yè)務流程中的通用功能抽取出來,單獨編寫功能代碼。在業(yè)務流程執(zhí)行過程中,Spring框架會根據(jù)業(yè)務流程要求,自動把獨立編寫的功能代碼切入到流程的合適位置。

spring提供了兩種方式的AOP使用

使用xml配置方式

在這里插入圖片描述

使用注解方式

在這里插入圖片描述

這里需要注意的是Spring AOP目前僅僅支持方法級別的切面,成員的interception并沒有實現(xiàn)。另外,spring aop僅僅是集成框架,并沒有參與aop的具體開發(fā)。

如果想利用aop的更多功能,或者在不使用spring的框架中使用aop的功能,該怎么辦呢?

AspectJ簡介

在這里插入圖片描述

spring aop集成了AspectJ(可以和java編程語言無縫結(jié)合的一個面向切面編程的可擴展框架)

AspectJ的使用實例

Eclipse Marketplace安裝插件AJDT

在這里插入圖片描述

創(chuàng)建Aspect工程

在這里插入圖片描述

創(chuàng)建AspectJ測試類

在這里插入圖片描述

創(chuàng)建一個切面Aspect文件

在這里插入圖片描述

.aj文件

在這里插入圖片描述

運行HelloAspectJDemo的java程序,結(jié)果為:

在這里插入圖片描述

不使用spring的aop功能實現(xiàn)日志輸出

第一種

public class TimeBook {undefined
?private Logger logger = Logger.getLogger(this.getClass().getName());
?//審核數(shù)據(jù)的相關程序
?public void doAuditing(String name){undefined
? logger.log(Level.INFO, name + "開始審核數(shù)據(jù)...");
? System.out.println("審核程序");
? logger.log(Level.INFO, name + "審核數(shù)據(jù)結(jié)束...");
?}
}
//TestHelloWorld.java
package com.gc.test;
import com.gc.action.TimeBook;
public class TestHelloWorld {undefined
?public static void main(String[] args){undefined
? TimeBook timeBook = new TimeBook();
? timeBook.doAuditing("張三");
?}
}

第二種:通過面向接口編程實現(xiàn)日志輸出

public class TimeBook implements TimeBookInterface {undefined
?//審核數(shù)據(jù)的相關程序
?public void doAuditing(String name){undefined
? System.out.println("審核程序");
?}
}
//TimeBookProxy.java
package com.gc.action;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import com.gc.impl.TimeBookInterface;
public class TimeBookProxy {undefined
?private Logger logger = Logger.getLogger(this.getClass().getName());
?private TimeBookInterface timeBookInterface;
?//在該類中針對前面的接口TimeBookInterface編程,而不是針對具體的類
?public TimeBookProxy(TimeBookInterface timeBookInterface){undefined
? this.timeBookInterface = timeBookInterface;
?}
?//實際業(yè)務處理
?public void doAuditing(String name){undefined
? logger.log(Level.INFO,"開始審核數(shù)據(jù) "+name);
? timeBookInterface.doAuditing(name);
? logger.log(Level.INFO,"審核數(shù)據(jù)結(jié)束 "+name);
?}
}
public class TestHelloWorld {undefined
?public static void main(String[] args){undefined
? TimeBookProxy timeBookProxy = new TimeBookProxy(new TimeBook());
? timeBookProxy.doAuditing("張三");
?}
}

第三種:使用java的代理機制進行日志輸出

public class LogProxy implements InvocationHandler{undefined
?private Logger logger = Logger.getLogger(this.getClass().getName());
?private Object delegate;
?//綁定代理對象
?public Object bind(Object delegate){undefined
? this.delegate = delegate;
? return Proxy.newProxyInstance(delegate.getClass().getClassLoader(),
? ? delegate.getClass().getInterfaces(),this);
?}
?//針對接口編程
?public Object invoke(Object proxy,Method method,Object[] args) throws Throwable {undefined
? Object result = null;
? try{undefined
? ?//在方法調(diào)用前后進行日志輸出
? ?logger.log(Level.INFO,args[0]+" 開始審核數(shù)據(jù)...");
? ?result = method.invoke(delegate, args);
? ?logger.log(Level.INFO,args[0]+" 審核數(shù)據(jù)結(jié)束...");
? }catch(Exception e){undefined
? ?logger.log(Level.INFO,e.toString());
? }
? return result;
?}
}
//TimeBookInterface.java
package com.gc.impl;
//針對接口編程
public interface TimeBookInterface {undefined
?public void doAuditing(String name);
}
//TimeBook.java
public class TimeBook implements TimeBookInterface {undefined
?//審核數(shù)據(jù)的相關程序
?public void doAuditing(String name){undefined
? System.out.println("審核程序");
?}
}
//TestHelloWorld.java
public class TestHelloWorld {undefined
?public static void main(String[] args){undefined
? //實現(xiàn)了對日志類的重用
? LogProxy logProxy = new LogProxy();
? TimeBookInterface timeBookProxy = (TimeBookInterface)logProxy.bind(new TimeBook());
? timeBookProxy.doAuditing("張三");
?}
}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論