免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
Java AOP 日志記錄

關(guān)于Spring AOP的一些術(shù)語(yǔ)

  1. 切面(Aspect):在Spring AOP中,切面可以使用通用類或者在普通類中以@Aspect 注解(@AspectJ風(fēng)格)來(lái)實(shí)現(xiàn)
  2. 連接點(diǎn)(Joinpoint):在Spring AOP中一個(gè)連接點(diǎn)代表一個(gè)方法的執(zhí)行
  3. 通知(Advice):在切面的某個(gè)特定的連接點(diǎn)(Joinpoint)上執(zhí)行的動(dòng)作。通知有各種類型,其中包括"around"、"before”和"after"等通知。許多AOP框架,包括Spring,都是以攔截器做通知模型,?并維護(hù)一個(gè)以連接點(diǎn)為中心的攔截器鏈
  4. 切入點(diǎn)(Pointcut):定義出一個(gè)或一組方法,當(dāng)執(zhí)行這些方法時(shí)可產(chǎn)生通知,Spring缺省使用AspectJ切入點(diǎn)語(yǔ)法。

通知類型

  1. 前置通知(@Before):在某連接點(diǎn)(join point)之前執(zhí)行的通知,但這個(gè)通知不能阻止連接點(diǎn)前的執(zhí)行(除非它拋出一個(gè)異常)
  2. 返回后通知(@AfterReturning):在某連接點(diǎn)(join point)正常完成后執(zhí)行的通知:例如,一個(gè)方法沒(méi)有拋出任何異常,正常返回
  3. 拋出異常后通知(@AfterThrowing):方法拋出異常退出時(shí)執(zhí)行的通知
  4. 后通知(@After):當(dāng)某連接點(diǎn)退出的時(shí)候執(zhí)行的通知(不論是正常返回還是異常退出)
  5. 環(huán)繞通知(@Around):包圍一個(gè)連接點(diǎn)(join point)的通知,如方法調(diào)用。這是最強(qiáng)大的一種通知類型,環(huán)繞通知可以在方法調(diào)用前后完成自定義的行為,它也會(huì)選擇是否繼續(xù)執(zhí)行連接點(diǎn)或直接返回它們自己的返回值或拋出異常來(lái)結(jié)束執(zhí)行

?以上來(lái)自博客:http://tiankong6622.iteye.com/blog/2092733

通過(guò)注解 加 AOP實(shí)現(xiàn)日志記錄

日志實(shí)體類

import lombok.Data;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;/** * @Author: jj * @Date: 2019/7/30 13:58 * @Version 1.0 */@Datapublic class Logs {	private String methodName;	private String className;	private List<Params> params = new ArrayList<>();	private Object returnData;	private String description;	private String comments;	private Date actionTime;	private String userName;	private String ip;	@Data	class Params {		private String paramsName;		private String typeName;		private String paramsDescription;		private Object value;	}	public String printLogs() {		String par = "";		for (Params param : params) {			par = par   " ;{"   param.paramsName   " ( "   param.typeName   " / "   param.paramsDescription   " ) : "   param.value   "}";		}		return "\t\t\t Time: "   new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(actionTime)   "============="  				className   " { "   methodName   " ( "   description   " ) ---> < "   par.substring(2)   " > return: "   returnData   " } "  				"\t\t\t ****用戶:"   userName   "   IP:"   ip   " 操作:"   comments   "****";	}}

?日志注解

import java.lang.annotation.*;/** * @Author: jj * @Date: 2019/7/30 13:21 * @Version 1.0 */@Documented@Target(ElementType.METHOD)@Inherited@Retention(RetentionPolicy.RUNTIME)public @interface LoggingInfo {	String methodDescription() default "";	Param[] paramsDescription();	String comments();}

參數(shù)注釋注解

import java.lang.annotation.*;/** * @Author: jj * @Date: 2019/7/30 15:13 * @Version 1.0 */@Documented@Target({ElementType.METHOD,ElementType.ANNOTATION_TYPE})@Inherited@Retention(RetentionPolicy.RUNTIME)public @interface Param {	String param();	String description() default "";}

切面類

import com.agrant.biz.commons.exception.ServiceException;import com.agrant.biz.commons.utils.ShiroSessionUtils;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.*;import org.aspectj.lang.reflect.MethodSignature;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 javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import java.lang.reflect.Method;import java.lang.reflect.Parameter;import java.util.Date;/** * @Author: jj * @Date: 2019/7/30 13:19 * @Version 1.0 */@Aspect@Componentpublic class LoggingAspect {	//本地異常日志記錄對(duì)象	private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);	Logs log = null;	//Controller層切點(diǎn)	@Pointcut("@annotation(com.agrant.biz.service.aspect.log.LoggingInfo)")	public void controllerAspect() {	}	/**	 *	 * @param joinPoint 切點(diǎn)	 */	@Before("controllerAspect()")	public void doBefore(JoinPoint joinPoint) {		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder				.getRequestAttributes()).getRequest();		HttpSession session = request.getSession();		//讀取session中的用戶		String currentUser = ShiroSessionUtils.getUser().get("name").toString();		//請(qǐng)求的IP		String ip = request.getRemoteAddr();		//獲取類名		String targetName = joinPoint.getTarget().getClass().getName();		//獲取方法名		String methodName = joinPoint.getSignature().getName();		Method currentMethod = ((MethodSignature) joinPoint.getSignature()).getMethod();		//*========數(shù)據(jù)庫(kù)日志=========*//		log = new Logs();		log.setActionTime(new Date(System.currentTimeMillis()));		log.setUserName(currentUser);		log.setIp(ip);		log.setClassName(targetName);		log.setMethodName(methodName);		try {			Object[] arguments = joinPoint.getArgs();			Class targetClass = Class.forName(targetName);			Method[] methods = targetClass.getMethods();			for (Method method : methods) {				if (method.getName().equals(methodName)) {					Class[] clazzs = method.getParameterTypes();					if (clazzs.length == arguments.length) {						method.getGenericReturnType();						Parameter[] parameters = method.getParameters();						log.setComments(method.getAnnotation(LoggingInfo.class).comments());						log.setDescription(method.getAnnotation(LoggingInfo.class).methodDescription());						Param[] parametersDescription = method.getAnnotation(LoggingInfo.class).paramsDescription();						if (parameters.length != parametersDescription.length) {							throw new ServiceException("LoggingAspect注解參數(shù)長(zhǎng)度有誤??!");						}						for (int i = 0; i < parameters.length; i  ) {							Parameter parameter = parameters[i];							Param param = parametersDescription[i];							System.out.println("param"   param.param()   "description"   param.description());							Logs.Params paramLog = log.new Params();							paramLog.setParamsName(param.param());							paramLog.setTypeName(parameter.getType().getTypeName());							paramLog.setParamsDescription(param.description());							paramLog.setValue(arguments[i]);							log.getParams().add(paramLog);						}						break;					}				}			}		} catch (ClassNotFoundException e) {			logger.error(ServiceException.collectExceptionStackMsg(e));		}	}	@AfterReturning(pointcut = "controllerAspect()", returning = "ret")	public Object doAfter(JoinPoint joinPoint, Object ret) {		log.setReturnData(ret);		logger.info(log.printLogs());//		loggingMapper.insert();		return ret;	}}

使用實(shí)例

日志打印結(jié)果:

Time: 2019-07-30 19:28:28=============com.agrant.biz.service.imp.BiReportServiceImpl { enteredListing ( 消耗信息輸入頁(yè) ) ---> < {mediaId ( java.lang.Integer / 媒體id ) : 1} ;{agentMonth ( java.lang.String / 代理商數(shù)據(jù) 消耗日期 ) : 2019-07} ;{mediaYear ( java.lang.String / 媒體數(shù)據(jù)消耗月份 ) : null} ;{vo ( com.agrant.biz.model.vo.ResultVo / view Object ) : ResultVo(code=200, message=success!, data={medias={consumed=[], pending=[{date=2019-06, id=1}, {date=2019-05, id=2}, {date=2019-07, id=3}, {date=2019-02, id=4}, {date=2019-01, id=5}, {date=2019-04, id=6}, {date=2019-03, id=7}]}, agents={consumed=[{date=2019-07-06, id=1}], pending=[{date=2019-07-01, id=2012}, {date=2019-07-01, id=3651}, {date=2019-07-02, id=3661}]}, timestamp=1564486108687}, clazz=null, currentPage=null, pageSize=null, totalPage=null, totalSize=null, orderBy=null)} > return: ResultVo(code=200, message=success!, data={medias={consumed=[], pending=[{date=2019-06, id=1}, {date=2019-05, id=2}]}, agents={consumed=[{date=2019-07-06, id=1}], pending=[{date=2019-07-01, id=2012}, {date=2019-07-01, id=3651}]}, timestamp=1564486108687}, clazz=null, currentPage=null, pageSize=null, totalPage=null, totalSize=null, orderBy=null) } 			 ****用戶:張三   IP:127.0.0.1 操作:請(qǐng)求消耗信息輸入頁(yè)****

?

來(lái)源:https://www.icode9.com/content-1-371051.html
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
java中base64
HTTP/2 服務(wù)端推送
【Java】Converter(數(shù)據(jù)類型轉(zhuǎn)換工具類)
利用spring AOP實(shí)現(xiàn)每個(gè)請(qǐng)求的日志輸出
Spring MVC通過(guò)AOP切面編程 來(lái)攔截controller 實(shí)現(xiàn)日志的寫入
java注解日志記錄到數(shù)據(jù)庫(kù)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服