日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring aop 记录操作日志 Aspect 自定义注解

發布時間:2024/1/17 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring aop 记录操作日志 Aspect 自定义注解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

時間過的真快,轉眼就一年了,沒想到隨手寫的筆記會被這么多人瀏覽,不想誤人子弟,于是整理了一個優化版,在這里感謝智斌哥提供的建議和幫助,話不多說,進入正題

所需jar包 :spring4.3相關聯以及aspectjweaver-1.8.5.jar,jdk? 1.7,1.8親測可用,源碼下載鏈接放在最后,關鍵代碼如下:

1.Action

package com.opr.controller;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView;import com.opr.service.UserService;@Controller @RequestMapping("user") public class UserController {@Autowired UserService userService;/**** 首頁* @param request* @param response* @return ModelAndView*/@RequestMapping("index")public ModelAndView index(HttpServletRequest request,HttpServletResponse response) {return new ModelAndView("index");}/**** 登錄* @param request* @param response* @return ModelAndView*/@RequestMapping("userLogin")public ModelAndView userLogin(HttpServletRequest request,HttpServletResponse response,String userName,String password) {ModelAndView mv = new ModelAndView();try {boolean result = userService.userLogin(userName,password);if(result) {mv.setViewName("success");}else {mv.setViewName("error");}} catch (Exception e) {// TODO: handle exception e.printStackTrace();}return mv;}}

2.Service

package com.opr.service.impl;import org.springframework.stereotype.Service;import com.opr.annotation.OperLog; import com.opr.service.UserService;@Service("userService") public class UserServiceImpl implements UserService {//設置默認值,模擬登錄private final String userName = "admin";private final String password = "123456";@Override@OperLog(operType="用戶登錄",userIndex = 0 )//0為下標,代表傳入的第一個參數,這里取userName為示例。實際場景可以在session中取操作人!public boolean userLogin(String userName,String password) {boolean flag = false;if(this.userName.equals(userName) && this.password.equals(password)) {flag = true;}return flag;}}

3.自定義注解

package com.opr.annotation;import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;//這里不明白的童鞋可以抽空看看自定義注解 @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface OperLog {//操作類型String operType() default "";//操作人String user() default "";//操作人下標int userIndex() default -1;}

4.攔截器

package com.opr.interceptor;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component;import com.opr.annotation.OperLog;@Aspect @Component public class OperLogInterceptor {//這里寫的為環繞觸發 ,可自行根據業務場景選擇@Before @After//觸發條件為:com.opr包下面所有類且注解為OperLog的@Around("within(com.opr..*) && @annotation(operLog)")public Object doAroundMethod(ProceedingJoinPoint pjd,OperLog operLog) throws Throwable {long startTime=System.currentTimeMillis();//開始時間 Object[] params = pjd.getArgs();//獲取請求參數System.out.println("監聽到傳入參數為:");for(Object param:params) {System.out.println(param);}//###################上面代碼為方法執行前#####################Object result = pjd.proceed();//執行方法,獲取返回參數//###################下面代碼為方法執行后#####################System.out.println("返回參數為:" + result);String user = operLog.userIndex()==-1?operLog.user():(String)params[operLog.userIndex()];//操作人String operType = operLog.operType();//操作類型System.out.println("操作人: " + user +" 操作類型: " + operType);long endTime=System.currentTimeMillis();//結束時間float excTime=(float)(endTime-startTime)/1000;System.out.println("執行時間:"+excTime+"s");System.out.println("#######################分隔符##########################");return result;} }

5.Spring

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"><!-- 設置掃描目錄 --><context:component-scan base-package="com.opr" /><!-- 設置請求映射器 --><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/><!-- 設置適配器處理器 --><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/><!-- 設置視圖處理器 --><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp" /></bean><aop:aspectj-autoproxy proxy-target-class="true" /></beans>

6.運行項目后

?

7.成功

  1).前端效果:

  

  2).后臺打印:

  

?

8.失敗

  1).前端效果:

  

?

  2).后臺打印:

  ?

?

源碼下載地址:http://download.csdn.net/download/qq_16437937/10188600

吐槽一下:CSDN下載最低為2分,分不夠的可以郵箱@我,或者在下面留下你的郵箱,我看到了就會發你

郵箱為:Leifeiwangyi@163.com

有什么問題可以留言咱們討論討論,謝謝大家

?

轉載于:https://www.cnblogs.com/leifei/p/8194644.html

總結

以上是生活随笔為你收集整理的Spring aop 记录操作日志 Aspect 自定义注解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。