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

aop注解方式實(shí)現(xiàn)全局日志管理方法

 更新時(shí)間:2018年01月22日 14:42:02   作者:荒唐的程序猿  
下面小編就為大家分享一篇aop注解方式實(shí)現(xiàn)全局日志管理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

1:日志實(shí)體類

public class SysLog {
 /** */
 private Integer id;
 /** 日志描述*/
 private String description;
 /** 執(zhí)行的方法*/
 private String method;
 /** 日志類型 0:操作日志;1:異常日志*/
 private Integer logType;
 /** 客戶端請(qǐng)求的ip地址*/
 private String requestIp;
 /** 異常代碼*/
 private String exceptionCode;
 /** 異常詳細(xì)信息*/
 private String exceptionDetail;
 /** 請(qǐng)求參數(shù)*/
 private String params;
 /** 操作人*/
 private String createBy;
 /** 操作時(shí)間*/
 private String createDate;
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getDescription() {
  return description;
 }
 public void setDescription(String description) {
  this.description = description;
 }
 public String getMethod() {
  return method;
 }
 public void setMethod(String method) {
  this.method = method;
 }
 public Integer getLogType() {
  return logType;
 }
 public void setLogType(Integer logType) {
  this.logType = logType;
 }
 public String getRequestIp() {
  return requestIp;
 }
 public void setRequestIp(String requestIp) {
  this.requestIp = requestIp;
 }
 public String getExceptionCode() {
  return exceptionCode;
 }
 public void setExceptionCode(String exceptionCode) {
  this.exceptionCode = exceptionCode;
 }
 public String getExceptionDetail() {
  return exceptionDetail;
 }
 public void setExceptionDetail(String exceptionDetail) {
  this.exceptionDetail = exceptionDetail;
 }
 public String getParams() {
  return params;
 }
 public void setParams(String params) {
  this.params = params;
 }
 public String getCreateBy() {
  return createBy;
 }
 public void setCreateBy(String createBy) {
  this.createBy = createBy;
 }
 public String getCreateDate() {
  return createDate;
 }
 public void setCreateDate(String createDate) {
  this.createDate = createDate;
 }
}

2:maven需要的jar

<dependency> 
   <groupId>org.aspectj</groupId> 
   <artifactId>aspectjrt</artifactId> 
   <version>1.7.4</version> 
  </dependency> 
 <dependency> 
   <groupId>org.aspectj</groupId> 
   <artifactId>aspectjweaver</artifactId> 
   <version>1.7.4</version> 
 </dependency> 
<dependency> 
   <groupId>cglib</groupId> 
   <artifactId>cglib</artifactId> 
   <version>2.1_3</version> 
 </dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>4.2.5.RELEASE</version>
</dependency> 

這里要求項(xiàng)目使用的是jdk1.7

3:springServlet-mvc.xml

<!--proxy-target-class="true"強(qiáng)制使用cglib代理 如果為false則spring會(huì)自動(dòng)選擇--> 
<aop:aspectj-autoproxy proxy-target-class="true"/> 

加上proxy-target-class="true"是為了可以攔截controller里面的方法

4:定義切面,我這里主要寫前置通知和異常通知

下面開始自定義注解

import java.lang.annotation.*;
@Target({ElementType.PARAMETER, ElementType.METHOD}) 
@Retention(RetentionPolicy.RUNTIME) 
@Documented 
public @interface Log {
	/** 要執(zhí)行的操作類型比如:add操作 **/ 
	 public String operationType() default ""; 
	 /** 要執(zhí)行的具體操作比如:添加用戶 **/ 
	 public String operationName() default "";
}

切面類

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.gtcity.user.model.SysLog;
import com.gtcity.user.model.SysUser;
import com.gtcity.user.service.SysLogService;
/**
 * @author panliang
 * @version 創(chuàng)建時(shí)間:2017-3-31 
 * @desc 切點(diǎn)類 
 *
 */
@Aspect
@Component
public class SystemLogAspect {
	//注入Service用于把日志保存數(shù)據(jù)庫(kù) 
	@Resource 
	private SysLogService systemLogService;
	private static final Logger logger = LoggerFactory.getLogger(SystemLogAspect. class); 
	
	//Controller層切點(diǎn) 
	//第一個(gè)*代表所有的返回值類型
	//第二個(gè)*代表所有的類
	//第三個(gè)*代表類所有方法
	//最后一個(gè)..代表所有的參數(shù)。
	 @Pointcut("execution (* com.gtcity.web.controller..*.*(..))") 
	 public void controllerAspect() { 
	 } 
	 
	 /**
	 * 
	 * @author: panliang
	 * @time:2017-3-31 下午2:22:16
	 * @param joinPoint 切點(diǎn)
	 * @describtion:前置通知 用于攔截Controller層記錄用戶的操作 
	 */
	 @Before("controllerAspect()")
	 public void doBefore(JoinPoint joinPoint) {
		/* System.out.println("==========執(zhí)行controller前置通知===============");
		 if(logger.isInfoEnabled()){
			 logger.info("before " + joinPoint);
		 }*/
		 
		 HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); 
   HttpSession session = request.getSession(); 
   //讀取session中的用戶 
   SysUser user = (SysUser) session.getAttribute("user"); 
   if(user==null){
  	 user=new SysUser();
  	 user.setUserName("非注冊(cè)用戶");
   }
   //請(qǐng)求的IP 
   String ip = request.getRemoteAddr();
   try { 
    
    String targetName = joinPoint.getTarget().getClass().getName(); 
    String methodName = joinPoint.getSignature().getName(); 
    Object[] arguments = joinPoint.getArgs(); 
    Class targetClass = Class.forName(targetName); 
    Method[] methods = targetClass.getMethods();
    String operationType = "";
    String operationName = "";
    for (Method method : methods) { 
     if (method.getName().equals(methodName)) { 
      Class[] clazzs = method.getParameterTypes(); 
      if (clazzs.length == arguments.length) { 
       operationType = method.getAnnotation(Log.class).operationType();
       operationName = method.getAnnotation(Log.class).operationName();
       break; 
      } 
     } 
    }
    //*========控制臺(tái)輸出=========*// 
    System.out.println("=====controller前置通知開始====="); 
    System.out.println("請(qǐng)求方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()")+"."+operationType); 
    System.out.println("方法描述:" + operationName); 
    System.out.println("請(qǐng)求人:" + user.getUserName()); 
    System.out.println("請(qǐng)求IP:" + ip); 
    //*========數(shù)據(jù)庫(kù)日志=========*// 
    SysLog log = new SysLog(); 
    log.setDescription(operationName); 
    log.setMethod((joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()")+"."+operationType); 
    log.setLogType(0); 
    log.setRequestIp(ip); 
    log.setExceptionCode(null); 
    log.setExceptionDetail( null); 
    log.setParams( null); 
    log.setCreateBy(user.getUserName());
    log.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); 
    log.setRequestIp(ip);
    //保存數(shù)據(jù)庫(kù) 
    systemLogService.insert(log); 
    System.out.println("=====controller前置通知結(jié)束====="); 
   } catch (Exception e) { 
    //記錄本地異常日志 
    logger.error("==前置通知異常=="); 
    logger.error("異常信息:{}", e.getMessage()); 
   } 
		 
		 
	 } 
	 
	 
 
  /**
	 * 
	 * @author: panliang
	 * @time:2017-3-31 下午2:24:36
	 * @param joinPoint 切點(diǎn) 
	 * @describtion:異常通知 用于攔截記錄異常日志 
	 */
  @AfterThrowing(pointcut = "controllerAspect()", throwing="e") 
  public void doAfterThrowing(JoinPoint joinPoint, Throwable e) { 
 	 
 	 HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); 
   HttpSession session = request.getSession(); 
   //讀取session中的用戶 
   SysUser user = (SysUser) session.getAttribute("user"); 
   if(user==null){
   	 user=new SysUser();
   	 user.setUserName("非注冊(cè)用戶");
   }
   //請(qǐng)求的IP 
   String ip = request.getRemoteAddr();
   
   String params = ""; 
   if (joinPoint.getArgs() != null && joinPoint.getArgs().length > 0) { 
   
  	 params=Arrays.toString(joinPoint.getArgs());
   } 
   try { 
    
    String targetName = joinPoint.getTarget().getClass().getName(); 
    String methodName = joinPoint.getSignature().getName(); 
    Object[] arguments = joinPoint.getArgs(); 
    Class targetClass = Class.forName(targetName); 
    Method[] methods = targetClass.getMethods();
    String operationType = "";
    String operationName = "";
    for (Method method : methods) { 
     if (method.getName().equals(methodName)) { 
      Class[] clazzs = method.getParameterTypes(); 
      if (clazzs.length == arguments.length) { 
       operationType = method.getAnnotation(Log.class).operationType();
       operationName = method.getAnnotation(Log.class).operationName();
       break; 
      } 
     } 
    }
    /*========控制臺(tái)輸出=========*/ 
    System.out.println("=====異常通知開始====="); 
    System.out.println("異常代碼:" + e.getClass().getName()); 
    System.out.println("異常信息:" + e.getMessage()); 
    System.out.println("異常方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()")+"."+operationType); 
    System.out.println("方法描述:" + operationName); 
    System.out.println("請(qǐng)求人:" + user.getUserName()); 
    System.out.println("請(qǐng)求IP:" + ip); 
    System.out.println("請(qǐng)求參數(shù):" + params); 
    //==========數(shù)據(jù)庫(kù)日志========= 
    SysLog log = new SysLog();
    log.setDescription(operationName); 
    log.setExceptionCode(e.getClass().getName()); 
    log.setLogType(1); 
    log.setExceptionDetail(e.getMessage()); 
    log.setMethod((joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()")); 
    log.setParams(params); 
    log.setCreateBy(user.getUserName()); 
    log.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); 
    log.setRequestIp(ip); 
    //保存數(shù)據(jù)庫(kù) 
    systemLogService.insert(log); 
    System.out.println("=====異常通知結(jié)束====="); 
   } catch (Exception ex) { 
    //記錄本地異常日志 
    logger.error("==異常通知異常=="); 
    logger.error("異常信息:{}", ex.getMessage()); 
   } 
   //==========記錄本地異常日志========== 
   logger.error("異常方法:{}異常代碼:{}異常信息:{}參數(shù):{}", joinPoint.getTarget().getClass().getName() + joinPoint.getSignature().getName(), e.getClass().getName(), e.getMessage(), params); 
 
  } 
	 
}

5:在controller里面

/**
	 * 根據(jù)用戶名去找密碼 判斷用戶名和密碼是否正確
	 * @author panliang
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("/skipPage.do")
	@Log(operationType="select操作:",operationName="用戶登錄")//注意:這個(gè)不加的話,這個(gè)方法的日志記錄不會(huì)被插入
	public ModelAndView skipPage(HttpServletRequest request,HttpServletResponse response) throws IOException{
		
		ModelAndView result=null;
		String username = request.getParameter("email");
		String password = request.getParameter("password");
		int flag = sysUserService.login(request, username, password);
		if(flag==1){//登錄成功
			result=new ModelAndView("redirect:/login/dispacher_main.do");
		}else if(flag==2){//用戶名不存在		
			result=new ModelAndView("redirect:/login/login.do?errorCode=1");			
		} else{//密碼不正確	
			result=new ModelAndView("redirect:/login/login.do?errorCode=2");			
		}
		return result;
	}

對(duì)于想要了解其他三種通知的可以參考這篇博文:點(diǎn)擊打開鏈接

這樣用戶在訪問后臺(tái)時(shí),不管是正常訪問還是出現(xiàn)bug數(shù)據(jù)庫(kù)都有記錄

以上這篇aop注解方式實(shí)現(xiàn)全局日志管理方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot配置shiro安全框架的實(shí)現(xiàn)

    SpringBoot配置shiro安全框架的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot配置shiro安全框架的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 帶你入門Java的類與對(duì)象

    帶你入門Java的類與對(duì)象

    下面小編就為大家?guī)硪黄钊肜斫釰ava 對(duì)象和類。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望能給你帶來幫助
    2021-07-07
  • 解決Spring AOP 同類調(diào)用失效問題

    解決Spring AOP 同類調(diào)用失效問題

    這篇文章主要介紹了解決Spring AOP 同類調(diào)用失效問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java獲取本機(jī)IP地址的方法代碼示例(內(nèi)網(wǎng)、公網(wǎng))

    Java獲取本機(jī)IP地址的方法代碼示例(內(nèi)網(wǎng)、公網(wǎng))

    在IT領(lǐng)域獲取本機(jī)IP地址是一項(xiàng)基礎(chǔ)但重要的任務(wù),特別是在網(wǎng)絡(luò)編程、遠(yuǎn)程協(xié)作和設(shè)備通信中,這篇文章主要給大家介紹了關(guān)于Java獲取本機(jī)IP地址的方法(內(nèi)網(wǎng)、公網(wǎng)),需要的朋友可以參考下
    2024-07-07
  • 詳解Java生成PDF文檔方法

    詳解Java生成PDF文檔方法

    這篇文章主要介紹了Java生成PDF文檔方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Mybatis批量修改聯(lián)合主鍵數(shù)據(jù)的兩種方法

    Mybatis批量修改聯(lián)合主鍵數(shù)據(jù)的兩種方法

    最近遇上需要批量修改有聯(lián)合主鍵的表數(shù)據(jù),找很多資料都不是太合適,最終自己摸索總結(jié)了兩種方式可以批量修改數(shù)據(jù),對(duì)Mybatis批量修改數(shù)據(jù)相關(guān)知識(shí)感興趣的朋友一起看看吧
    2022-04-04
  • ???????Spring多租戶數(shù)據(jù)源管理 AbstractRoutingDataSource

    ???????Spring多租戶數(shù)據(jù)源管理 AbstractRoutingDataSource

    本文技術(shù)了???????Spring多租戶數(shù)據(jù)源管理 AbstractRoutingDataSource,下文詳細(xì)內(nèi)容介紹,需要的小伙伴可以參考一下
    2022-05-05
  • Java并發(fā)編程加鎖導(dǎo)致的活躍性問題詳解方案

    Java并發(fā)編程加鎖導(dǎo)致的活躍性問題詳解方案

    所謂并發(fā)編程是指在一臺(tái)處理器上"同時(shí)"處理多個(gè)任務(wù)。并發(fā)是在同一實(shí)體上的多個(gè)事件。多個(gè)事件在同一時(shí)間間隔發(fā)生,所以編寫正確的程序很難,而編寫正確的并發(fā)程序則難上加難
    2021-10-10
  • Java使用Hutool執(zhí)行日期的加法和減法操作方法

    Java使用Hutool執(zhí)行日期的加法和減法操作方法

    使用Hutool進(jìn)行日期的加法和減法操作,可以使用`DateUtil.offsetXXX()`方法來實(shí)現(xiàn),這些方法會(huì)返回一個(gè)新的日期,而不是在原日期上進(jìn)行修改,本文給大家介紹Java使用Hutool執(zhí)行日期的加法和減法操作方法,感興趣的朋友一起看看吧
    2023-11-11
  • Java?Handler同步屏障淺析講解

    Java?Handler同步屏障淺析講解

    同步屏障機(jī)制是什么?Handler發(fā)送的消息分為普通消息、屏障消息、異步消息,一旦Looper在處理消息時(shí)遇到屏障消息,那么就不再處理普通的消息,而僅僅處理異步的消息。不再使用屏障后,需要撤銷屏障,不然就再也執(zhí)行不到普通消息了
    2022-08-08

最新評(píng)論

呈贡县| 连城县| 栖霞市| 浮山县| 绥棱县| 进贤县| 青州市| 安多县| 上饶县| 石首市| 丰宁| 黄山市| 合江县| 油尖旺区| 周宁县| 遂昌县| 泰宁县| 扎鲁特旗| 全南县| 齐河县| 新津县| 万盛区| 平和县| 喀喇沁旗| 双柏县| 莱州市| 景谷| 连山| 都江堰市| 清流县| 柯坪县| 惠州市| 山阳县| 弥渡县| 深圳市| 休宁县| 彭水| 海城市| 聂拉木县| 那曲县| 股票|