最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

使用xml配置方式

在這里插入圖片描述

使用注解方式

在這里插入圖片描述

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

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

AspectJ簡(jiǎn)介

在這里插入圖片描述

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

AspectJ的使用實(shí)例

Eclipse Marketplace安裝插件AJDT

在這里插入圖片描述

創(chuàng)建Aspect工程

在這里插入圖片描述

創(chuàng)建AspectJ測(cè)試類

在這里插入圖片描述

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

在這里插入圖片描述

.aj文件

在這里插入圖片描述

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

在這里插入圖片描述

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

第一種

public class TimeBook {undefined
?private Logger logger = Logger.getLogger(this.getClass().getName());
?//審核數(shù)據(jù)的相關(guān)程序
?public void doAuditing(String name){undefined
? logger.log(Level.INFO, name + "開(kāi)始審核數(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("張三");
?}
}

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

public class TimeBook implements TimeBookInterface {undefined
?//審核數(shù)據(jù)的相關(guān)程序
?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;
?//在該類中針對(duì)前面的接口TimeBookInterface編程,而不是針對(duì)具體的類
?public TimeBookProxy(TimeBookInterface timeBookInterface){undefined
? this.timeBookInterface = timeBookInterface;
?}
?//實(shí)際業(yè)務(wù)處理
?public void doAuditing(String name){undefined
? logger.log(Level.INFO,"開(kāi)始審核數(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的代理機(jī)制進(jìn)行日志輸出

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

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

相關(guān)文章

最新評(píng)論

平顺县| 长寿区| 祁连县| 建宁县| 鄂尔多斯市| 临猗县| 班玛县| 永兴县| 玉门市| 鲜城| 浑源县| 佳木斯市| 伊宁县| 手游| 芷江| 唐河县| 陆河县| 金坛市| 龙门县| 通许县| 峨眉山市| 泗阳县| 浠水县| 汉阴县| 乌拉特中旗| 海阳市| 彭州市| 海丰县| 伊吾县| 茂名市| 通山县| 宾阳县| 长宁县| 托克逊县| 修文县| 彭阳县| 资中县| 牟定县| 黄梅县| 麻栗坡县| 平江县|