




已閱讀5頁(yè),還剩5頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
Spring MVC AOP通過(guò)注解方式攔截Controller等實(shí)現(xiàn)日志管理開(kāi)始上代碼:注解定義package mon;import java.lang.annotation.*;/* * author jiankunking * Date: 2016/8/15 * Time: 11:09 * annotation OperationLogger */Retention(RetentionPolicy.RUNTIME)/注解會(huì)在class中存在,運(yùn)行時(shí)可通過(guò)反射獲取Target(ElementType.METHOD)/目標(biāo)是方法Documented/文檔生成時(shí),該注解將被包含在javadoc中,可去掉public interface OperationLogger /* * 模塊名字 */ String modelName() default ; /* * 操作類型 */ String option();interface是用來(lái)自定義注釋類型的。 注釋的聲明用符號(hào)后面跟上這個(gè)注釋類型的名字,再后面跟上括號(hào),括號(hào)中列出這個(gè)注釋中元 素/方法的keyvalue對(duì)。值必須是常量。AOP攔截部分package mon;import org.apache.log4j.Logger;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.Signature;import org.aspectj.lang.annotation.*;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.stereotype.Component;import java.lang.reflect.Method;/* * author jiankunking * Date: 2016/8/15 * Time: 11:11 * annotation SysLogAspect */AspectComponentpublic class SysLogAspect private static final Logger logger = Logger.getLogger(SysLogAspect.class); /* * 定義Pointcut,Pointcut的名稱,此方法不能有返回值,該方法只是一個(gè)標(biāo)示 */ Pointcut(annotation(mon.OperationLogger) public void controllerAspect() System.out.println(我是一個(gè)切入點(diǎn)); /* * 前置通知(Before advice) :在某連接點(diǎn)(JoinPoint)之前執(zhí)行的通知,但這個(gè)通知不能阻止連接點(diǎn)前的執(zhí)行。 * param joinPoint */ Before(controllerAspect() public void doBefore(JoinPoint joinPoint) System.out.println(=SysLogAspect前置通知開(kāi)始=); /handleLog(joinPoint, null); /* * 后通知(After advice) :當(dāng)某連接點(diǎn)退出的時(shí)候執(zhí)行的通知(不論是正常返回還是異常退出)。 * param joinPoint */ AfterReturning(pointcut = controllerAspect() public void doAfter(JoinPoint joinPoint) System.out.println(=SysLogAspect后置通知開(kāi)始=); /handleLog(joinPoint, null); /* * 拋出異常后通知(After throwing advice) : 在方法拋出異常退出時(shí)執(zhí)行的通知。 * param joinPoint * param e */ AfterThrowing(value = controllerAspect(), throwing = e) public void doAfter(JoinPoint joinPoint, Exception e) System.out.println(=SysLogAspect異常通知開(kāi)始=); /handleLog(joinPoint, e); /* * 環(huán)繞通知(Around advice) :包圍一個(gè)連接點(diǎn)的通知,類似Web中Servlet規(guī)范中的Filter的doFilter方法??梢栽诜椒ǖ恼{(diào)用前后完成自定義的行為,也可以選擇不執(zhí)行。 * param joinPoint */ Around(controllerAspect() public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable System.out.println(=SysLogAspect 環(huán)繞通知開(kāi)始=); /handleLog(joinPoint, null); Object obj= joinPceed(); System.out.println(=SysLogAspect 環(huán)繞通知結(jié)束=); return obj; /* * 日志處理 * * param joinPoint * param e */ private void handleLog(JoinPoint joinPoint, Exception e) try /獲得注解 OperationLogger logger = giveController(joinPoint); if (logger = null) return; String signature = joinPoint.getSignature().toString(); / 獲取目標(biāo)方法簽名 String methodName = signature.substring(signature.lastIndexOf(.) + 1, signature.indexOf(); String longTemp = joinPoint.getStaticPart().toLongString(); String classType = joinPoint.getTarget().getClass().getName(); Class clazz = Class.forName(classType); Method methods = clazz.getDeclaredMethods(); System.out.println(methodName: + methodName); for (Method method : methods) if (method.isAnnotationPresent(OperationLogger.class) & method.getName().equals(methodName) /OpLogger logger = method.getAnnotation(OpLogger.class); String clazzName = clazz.getName(); System.out.println(clazzName: + clazzName + , methodName: + methodName); catch (Exception exp) logger.error(異常信息:, exp); exp.printStackTrace(); /* * 獲得注解 * param joinPoint * return * throws Exception */ private static OperationLogger giveController(JoinPoint joinPoint) throws Exception Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); if (method != null) return method.getAnnotation(OperationLogger.class); return null; Aspect通常用于將必要的但和業(yè)務(wù)無(wú)關(guān)的邏輯和業(yè)務(wù)邏輯分離。Spring使用的AOP注解分為三個(gè)層次: 前提條件是在xml中放開(kāi)了Aspect放在類頭上,把這個(gè)類作為一個(gè)切面。Pointcut放在方法頭上,定義一個(gè)可被別的方法引用的切入點(diǎn)表達(dá)式。5種通知。 Before,前置通知,放在方法頭上。After,后置【finally】通知,放在方法頭上。AfterReturning,后置【try】通知,放在方法頭上,使用returning來(lái)引用方法返回值。AfterThrowing,后置【catch】通知,放在方法頭上,使用throwing來(lái)引用拋出的異常。Around,環(huán)繞通知,放在方法頭上,這個(gè)方法要決定真實(shí)的方法是否執(zhí)行,而且必須有返回值。在Maven中加入以下以依賴 4.0.0 mon spring-mvc-log4j war 1.0-SNAPSHOT SpringMVC + Log4j 1.7 4.3.2.RELEASE 2.6.2 1.2 3.1.0 1.7.4 3.1 org.springframework spring-webmvc $spring.version org.springframework spring-aop $spring.version org.springframework spring-aspects $spring.version org.apache.logging.log4j log4j-api $log4j.version org.apache.logging.log4j log4j-core $log4j.version jstl jstl $jstl.version javax.servlet javax.servlet-api $servletapi.version provided log4j log4j 1.2.17 org.aspectj aspectjrt $org.aspectj-version javax.inject javax.inject 1 cglib cglib $cglib.version org.apache.maven.plugins maven-compiler-plugin 3.3 $jdk.version $jdk.version 在spring-*.xml中加入spring支持,打開(kāi)aop功能 /WEB-INF/pages/ .jsp 注解也寫(xiě)好了,spring也配置好了,在controller里面怎么用呢?Controller應(yīng)用package com.jiankunking.controller;import mon.OperationLogger;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;ControllerRequestMapping(value = /Welcome, produces = text/html;charset=UTF-8)public class WelcomeController OperationLogger(modelName = WelcomeController, option = getWelcome) RequestMapping(value = /getWelcome, method = RequestMethod.POST) public
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 生命科學(xué)實(shí)驗(yàn)室細(xì)胞凍存技術(shù)與專用儲(chǔ)存盒租賃合同
- 專屬私人直升機(jī)停機(jī)坪年度租賃服務(wù)協(xié)議
- 抖音平臺(tái)內(nèi)容審核標(biāo)準(zhǔn)制定協(xié)議
- 倉(cāng)儲(chǔ)配送一體化海運(yùn)船舶貨物委托運(yùn)輸合同
- 博物館講解員與科普教育項(xiàng)目合作協(xié)議
- 護(hù)理節(jié)力原則
- 護(hù)理科研小組建設(shè)與實(shí)施路徑
- 床邊護(hù)理查房
- 腎上腺素臨床護(hù)理應(yīng)用與安全管理
- XX公司管理體系解析
- 露天礦山三級(jí)安全培訓(xùn)
- 2023水電工程費(fèi)用構(gòu)成及概(估)算費(fèi)用標(biāo)準(zhǔn)
- GB/T 12996-2024電動(dòng)輪椅車
- 2024年學(xué)校臨時(shí)用工合同范例(二篇)
- 2024年全國(guó)高考數(shù)學(xué)試題及解析答案(新課標(biāo)Ⅱ卷)
- 工程造價(jià)咨詢服務(wù)投標(biāo)方案(技術(shù)方案)
- 網(wǎng)絡(luò)傳播概論(第5版)課件 第9、10章 網(wǎng)絡(luò)重塑的文化、網(wǎng)絡(luò)時(shí)代新的社會(huì)特征
- 癌癥患者生活質(zhì)量量表EORTC-QLQ-C30
- GB/T 20290-2024家用電動(dòng)洗碗機(jī)性能測(cè)試方法
- 一般工商貿(mào)(輕工)管理人員安全生產(chǎn)考試題庫(kù)(含答案)
- 醫(yī)院培訓(xùn)課件:《PPD試驗(yàn)》
評(píng)論
0/150
提交評(píng)論