通過注解方式攔截Controller等實現(xiàn)日志管理分解_第1頁
通過注解方式攔截Controller等實現(xiàn)日志管理分解_第2頁
通過注解方式攔截Controller等實現(xiàn)日志管理分解_第3頁
通過注解方式攔截Controller等實現(xiàn)日志管理分解_第4頁
通過注解方式攔截Controller等實現(xiàn)日志管理分解_第5頁
已閱讀5頁,還剩5頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、Spring MVC AOP通過注解方式攔截Controller等實現(xiàn)日志管理開始上代碼:注解定義package mon;import java.lang.annotation.*;/* * author jiankunking * Date: 2016/8/15 * Time: 11:09 * annotation OperationLogger */Retention(RetentionPolicy.RUNTIME)/注解會在class中存在,運行時可通過反射獲取Target(ElementType.METHOD)/目標是方法Documented/文檔生成時,該注解將被包含在javadoc

2、中,可去掉public interface OperationLogger /* * 模塊名字 */ String modelName() default ; /* * 操作類型 */ String option();interface是用來自定義注釋類型的。 注釋的聲明用符號后面跟上這個注釋類型的名字,再后面跟上括號,括號中列出這個注釋中元 素/方法的keyvalue對。值必須是常量。AOP攔截部分package mon;import org.apache.log4j.Logger;import org.aspectj.lang.JoinPoint;import org.aspectj.la

3、ng.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 SysLogAsp

4、ect */AspectComponentpublic class SysLogAspect private static final Logger logger = Logger.getLogger(SysLogAspect.class); /* * 定義Pointcut,Pointcut的名稱,此方法不能有返回值,該方法只是一個標示 */ Pointcut(annotation(mon.OperationLogger) public void controllerAspect() System.out.println(我是一個切入點); /* * 前置通知(Before advice) :

5、在某連接點(JoinPoint)之前執(zhí)行的通知,但這個通知不能阻止連接點前的執(zhí)行。 * param joinPoint */ Before(controllerAspect() public void doBefore(JoinPoint joinPoint) System.out.println(=SysLogAspect前置通知開始=); /handleLog(joinPoint, null); /* * 后通知(After advice) :當某連接點退出的時候執(zhí)行的通知(不論是正常返回還是異常退出)。 * param joinPoint */ AfterReturning(pointc

6、ut = controllerAspect() public void doAfter(JoinPoint joinPoint) System.out.println(=SysLogAspect后置通知開始=); /handleLog(joinPoint, null); /* * 拋出異常后通知(After throwing advice) : 在方法拋出異常退出時執(zhí)行的通知。 * param joinPoint * param e */ AfterThrowing(value = controllerAspect(), throwing = e) public void doAfter(Jo

7、inPoint joinPoint, Exception e) System.out.println(=SysLogAspect異常通知開始=); /handleLog(joinPoint, e); /* * 環(huán)繞通知(Around advice) :包圍一個連接點的通知,類似Web中Servlet規(guī)范中的Filter的doFilter方法。可以在方法的調用前后完成自定義的行為,也可以選擇不執(zhí)行。 * param joinPoint */ Around(controllerAspect() public Object doAround(ProceedingJoinPoint joinPoint

8、) throws Throwable System.out.println(=SysLogAspect 環(huán)繞通知開始=); /handleLog(joinPoint, null); Object obj= joinPceed(); System.out.println(=SysLogAspect 環(huán)繞通知結束=); return obj; /* * 日志處理 * * param joinPoint * param e */ private void handleLog(JoinPoint joinPoint, Exception e) try /獲得注解 OperationLo

9、gger logger = giveController(joinPoint); if (logger = null) return; String signature = joinPoint.getSignature().toString(); / 獲取目標方法簽名 String methodName = signature.substring(signature.lastIndexOf(.) + 1, signature.indexOf(); String longTemp = joinPoint.getStaticPart().toLongString(); String classTy

10、pe = 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

11、) /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 */ pr

12、ivate 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(OperationLogge

13、r.class); return null; Aspect通常用于將必要的但和業(yè)務無關的邏輯和業(yè)務邏輯分離。Spring使用的AOP注解分為三個層次: 前提條件是在xml中放開了Aspect放在類頭上,把這個類作為一個切面。Pointcut放在方法頭上,定義一個可被別的方法引用的切入點表達式。5種通知。 Before,前置通知,放在方法頭上。After,后置【finally】通知,放在方法頭上。AfterReturning,后置【try】通知,放在方法頭上,使用returning來引用方法返回值。AfterThrowing,后置【catch】通知,放在方法頭上,使用throwing來引用拋出的

14、異常。Around,環(huán)繞通知,放在方法頭上,這個方法要決定真實的方法是否執(zhí)行,而且必須有返回值。在Maven中加入以下以依賴 xmlns:xsi=/2001/XMLSchema-instance /maven-v4_0_0.xsd mon spring-mvc-log4j war 1.0-SNAPSHOT SpringMVC + Log4j 1.7 .RELEASE 1.2 3.1 org.springframework spring-webmvc $spring.version org.springframework

15、 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 org.aspectj aspectjrt $

16、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支持,打開aop功能 /WEB-INF/pages/ .jsp 注解也寫好了,spring也配置好了,在controller里面怎么用呢?Controller應用package com.jiankunking.controller;import mon

17、.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 v

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論