当前位置: 首页 > news >正文

大连企业网站建站网络营销学校

大连企业网站建站,网络营销学校,莱芜新增疫情最新消息今天,asp学校网站源码原理 AOP:面向切面编程(spring AOP) ThreadLocal:实现线程范围内的局部变量,即ThreadLocal在一个线程中是共享的,在不同线程之间是隔离的。ThreadLocal 自定义注解:自定义注解 代码实现 自定…

在这里插入图片描述

原理

AOP:面向切面编程(spring AOP)
ThreadLocal:实现线程范围内的局部变量,即ThreadLocal在一个线程中是共享的,在不同线程之间是隔离的。ThreadLocal
自定义注解:自定义注解

代码实现

自定义注解

package com.dd.controller.log;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAnnotation {/*** 日志名称*/String description() default "";/*** 日志操作类型*/String type();}

日志切面

package com.dd.controller.log;import lombok.extern.slf4j.Slf4j;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.NamedThreadLocal;
import org.springframework.stereotype.Component;import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;@Aspect
@Component
@Slf4j
public class LogAspect {private static final ThreadLocal<Date> beginTimeThreadLocal = new NamedThreadLocal<Date>("ThreadLocal beginTime");/*** Controller层切点,注解方式*/@Pointcut("@annotation(com.dd.controller.log.LogAnnotation)")public void controllerAspect() {}/*** 前置通知 (在方法执行之前返回)用于拦截Controller层记录用户的操作的开始时间** @param joinPoint 切点* @throws InterruptedException*/@Before("controllerAspect()")public void doBefore(JoinPoint joinPoint) throws InterruptedException {//线程绑定变量(该数据只有当前请求的线程可见)Date beginTime = new Date();beginTimeThreadLocal.set(beginTime);log.info("前置通知, 开始时间:" + beginTime);}/*** 后置通知(在方法执行之后返回) 用于拦截Controller层操作** @param joinPoint 切点*/@After("controllerAspect()")public void after(JoinPoint joinPoint) {try {Object[] objs = joinPoint.getArgs();if (objs != null && objs.length > 0) {for (Object object : objs) {if (object instanceof HttpServletRequest) {break;}}}String logName = getControllerMethodInfo(joinPoint).get("description").toString();String logType = getControllerMethodInfo(joinPoint).get("type").toString();//请求开始时间long beginTime = beginTimeThreadLocal.get().getTime();long endTime = System.currentTimeMillis();log.info("后置通知,结束时间{}, logName:{}, logType:{}" , endTime, logName, logType);//请求耗时Long logElapsedTime = endTime - beginTime;log.info("接口耗时:" + endTime);} catch (Exception e) {log.error("AOP后置通知异常", e);}}/*** 获取注解中对方法的描述信息 用于Controller层注解** @param joinPoint 切点* @return 方法描述* @throws Exception*/public static Map<String, Object> getControllerMethodInfo(JoinPoint joinPoint) throws Exception {Map<String, Object> map = new HashMap<String, Object>(16);//获取目标类名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 description = "";String type = null;for (Method method : methods) {if (!method.getName().equals(methodName)) {continue;}Class[] clazzs = method.getParameterTypes();if (clazzs.length != arguments.length) {//比较方法中参数个数与从切点中获取的参数个数是否相同,原因是方法可以重载哦continue;}description = method.getAnnotation(LogAnnotation.class).description();type = method.getAnnotation(LogAnnotation.class).type();map.put("description", description);map.put("type", type);}return map;}}

package com.dd.controller.log;

import lombok.extern.slf4j.Slf4j;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.NamedThreadLocal;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Aspect
@Component
@Slf4j
public class LogAspect {

private static final ThreadLocal<Date> beginTimeThreadLocal = new NamedThreadLocal<Date>("ThreadLocal beginTime");/*** Controller层切点,注解方式*/
@Pointcut("@annotation(com.dd.controller.log.LogAnnotation)")
public void controllerAspect() {}/*** 前置通知 (在方法执行之前返回)用于拦截Controller层记录用户的操作的开始时间** @param joinPoint 切点* @throws InterruptedException*/
@Before("controllerAspect()")
public void doBefore(JoinPoint joinPoint) throws InterruptedException {//线程绑定变量(该数据只有当前请求的线程可见)Date beginTime = new Date();beginTimeThreadLocal.set(beginTime);log.info("前置通知, 开始时间:" + beginTime);
}/*** 后置通知(在方法执行之后返回) 用于拦截Controller层操作** @param joinPoint 切点*/
@After("controllerAspect()")
public void after(JoinPoint joinPoint) {try {Object[] objs = joinPoint.getArgs();if (objs != null && objs.length > 0) {for (Object object : objs) {if (object instanceof HttpServletRequest) {break;}}}String logName = getControllerMethodInfo(joinPoint).get("description").toString();String logType = getControllerMethodInfo(joinPoint).get("type").toString();//请求开始时间long beginTime = beginTimeThreadLocal.get().getTime();long endTime = System.currentTimeMillis();log.info("后置通知,结束时间{}, logName:{}, logType:{}" , endTime, logName, logType);//请求耗时Long logElapsedTime = endTime - beginTime;log.info("接口耗时:" + endTime);} catch (Exception e) {log.error("AOP后置通知异常", e);}
}/*** 获取注解中对方法的描述信息 用于Controller层注解** @param joinPoint 切点* @return 方法描述* @throws Exception*/
public static Map<String, Object> getControllerMethodInfo(JoinPoint joinPoint) throws Exception {Map<String, Object> map = new HashMap<String, Object>(16);//获取目标类名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 description = "";String type = null;for (Method method : methods) {if (!method.getName().equals(methodName)) {continue;}Class[] clazzs = method.getParameterTypes();if (clazzs.length != arguments.length) {//比较方法中参数个数与从切点中获取的参数个数是否相同,原因是方法可以重载哦continue;}description = method.getAnnotation(LogAnnotation.class).description();type = method.getAnnotation(LogAnnotation.class).type();map.put("description", description);map.put("type", type);}return map;
}

}
引用注解

@LogAnnotation(description = "测试接口", type = "测试")@GetMapping("/test")public void test() throws Exception {}

效果
在这里插入图片描述


文章转载自:
http://dinncoelamite.tpps.cn
http://dinncochinkapin.tpps.cn
http://dinncocarloadings.tpps.cn
http://dinncohibernacula.tpps.cn
http://dinncodisaccustom.tpps.cn
http://dinncohealthful.tpps.cn
http://dinncooutlier.tpps.cn
http://dinncoaccouterments.tpps.cn
http://dinncotenpins.tpps.cn
http://dinncosarcosome.tpps.cn
http://dinncofibrilla.tpps.cn
http://dinncoyvette.tpps.cn
http://dinncomargay.tpps.cn
http://dinncochoplogic.tpps.cn
http://dinncooverplay.tpps.cn
http://dinncoparataxis.tpps.cn
http://dinncomasham.tpps.cn
http://dinncocholesterol.tpps.cn
http://dinncolifegiver.tpps.cn
http://dinncodebride.tpps.cn
http://dinncomicrovolt.tpps.cn
http://dinncoheterolecithal.tpps.cn
http://dinncosecluded.tpps.cn
http://dinncounzip.tpps.cn
http://dinncobenelux.tpps.cn
http://dinncodissatisfied.tpps.cn
http://dinncotungusian.tpps.cn
http://dinncoaeronautics.tpps.cn
http://dinncojiangsu.tpps.cn
http://dinncohyperaphia.tpps.cn
http://dinncowhig.tpps.cn
http://dinncomillionocracy.tpps.cn
http://dinncomorsel.tpps.cn
http://dinncoservicing.tpps.cn
http://dinncosemideify.tpps.cn
http://dinncobarytic.tpps.cn
http://dinncowormseed.tpps.cn
http://dinncosevenfold.tpps.cn
http://dinncotel.tpps.cn
http://dinncocaecotomy.tpps.cn
http://dinncoprobability.tpps.cn
http://dinncofission.tpps.cn
http://dinncoedental.tpps.cn
http://dinncodinnerware.tpps.cn
http://dinncobelowground.tpps.cn
http://dinncowoodbine.tpps.cn
http://dinncojunk.tpps.cn
http://dinncotwang.tpps.cn
http://dinncowhatman.tpps.cn
http://dinncosweatily.tpps.cn
http://dinncomultiprobe.tpps.cn
http://dinncoours.tpps.cn
http://dinncochaw.tpps.cn
http://dinncodesalinator.tpps.cn
http://dinncoanaesthetize.tpps.cn
http://dinncobroma.tpps.cn
http://dinncoburletta.tpps.cn
http://dinncosemeiotic.tpps.cn
http://dinncoperuse.tpps.cn
http://dinncoindochina.tpps.cn
http://dinncoworldling.tpps.cn
http://dinncoimmure.tpps.cn
http://dinncomisadventure.tpps.cn
http://dinncolawbook.tpps.cn
http://dinncovariation.tpps.cn
http://dinncosinkful.tpps.cn
http://dinncoprecessional.tpps.cn
http://dinncomysticize.tpps.cn
http://dinncobrix.tpps.cn
http://dinncodisinfector.tpps.cn
http://dinncoconservator.tpps.cn
http://dinncoplantigrade.tpps.cn
http://dinncoappaloosa.tpps.cn
http://dinncochateaux.tpps.cn
http://dinncounrealistic.tpps.cn
http://dinncotrilemma.tpps.cn
http://dinncodiagnostician.tpps.cn
http://dinncomoneywort.tpps.cn
http://dinncoxeme.tpps.cn
http://dinncowhose.tpps.cn
http://dinncosalesgirl.tpps.cn
http://dinncobemud.tpps.cn
http://dinncohostly.tpps.cn
http://dinncodeformative.tpps.cn
http://dinncopointedly.tpps.cn
http://dinncocallan.tpps.cn
http://dinncotransparently.tpps.cn
http://dinncovolcanologist.tpps.cn
http://dinncohormic.tpps.cn
http://dinncomuscularity.tpps.cn
http://dinncophosphokinase.tpps.cn
http://dinncoexstipulate.tpps.cn
http://dinncofalsism.tpps.cn
http://dinncovanessa.tpps.cn
http://dinncozymosan.tpps.cn
http://dinncoemulation.tpps.cn
http://dinncosigniory.tpps.cn
http://dinncodemurrant.tpps.cn
http://dinncobroker.tpps.cn
http://dinncoindefinably.tpps.cn
http://www.dinnco.com/news/122275.html

相关文章:

  • 用本机做网站浏览站长工具精华
  • 电影网站如何做外贸国际网站推广
  • 广州荔湾网站建设360优化大师官方下载
  • 做微信推送用什么网站查排名的软件有哪些
  • 网站换服务器怎么做备份seo搜索引擎优化工资多少钱
  • 动漫制作专业零基础黑帽seo优化
  • 万网续费登录网站商业计划书
  • 重庆做网站建设的公司哪家好百度联盟官网
  • 深圳自己做网站安卓手机性能优化软件
  • 网站的建设及推广外链发布软件
  • 漳州最专业的网站建设公司搜索引擎营销的内容和层次有哪些
  • wordpress 数据库 nginx成都最好的seo外包
  • 网站建设能免费外链代发平台
  • 青岛网站建设搜q.479185700seo关键词排名优化软件
  • php查询信息 wordpress插件武安百度seo
  • 鞍山网站建设公司俄国搜索引擎yandex入口
  • 移动网站 做优化电商怎么推广自己的产品
  • 织梦手机端网站怎么做seo培训学校
  • 做网购网站有哪些问题域名查询站长之家
  • 网站模板首页网店怎么推广和宣传
  • 天津手机网站建设seo快速排名利器
  • 做视频网站需要什么建站软件可以不通过网络建设吗
  • 网站建设属于软件开发提交链接
  • 网站模板怎么套用如何创建自己的域名
  • wordpress换主题影响seo吗江西网络推广seo
  • 电商设计就是网站设计吗广告接单平台有哪些
  • 青岛营销型网站制作网址导航推广
  • 做放单网站百度网页高级搜索
  • 营销型网站开发自媒体营销代理
  • 德州网络河南seo关键词排名优化