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

公司网站的专题策划企业网络营销策划方案范文

公司网站的专题策划,企业网络营销策划方案范文,苏州优秀网站设计,网站的ab测试怎么做原理 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://www.zhongyajixie.com/news/24856.html

相关文章:

  • 网站建设业务介绍第一设计
  • web动态网站开发网站定制开发
  • 域名服务商网站seo优化的方法有哪些
  • 西安响应式网站设计站长工具seo优化建议
  • wordpress themes free黑帽seo工具
  • 建设四川网站seo品牌优化百度资源网站推广关键词排名
  • 企业网站程序百度云登录首页
  • 广州网站建设报价百度搜索链接入口
  • 江苏省城乡和建设厅网站网络营销有哪些形式
  • 如何提高网站的知名度合作seo公司
  • 厦门网站建设_整合营销的最高阶段是
  • 招聘网站怎么做介绍近期出现的病毒叫什么
  • 咸阳做网站公司深圳seo论坛
  • 关于我们网站设计佛山百度推广电话
  • 网站建设价格gxjzdrj互联网营销怎么赚钱
  • 域名与网站区别搜索引擎网站推广如何优化
  • 苹果电脑如何做网站百姓网推广怎么收费标准
  • 企业网站建设遵循的原则最新新闻热点事件2024
  • 自己做视频网站广告seo是什么意思
  • 怎样在政府采购网站做备案网络建站优化科技
  • 谁可以做网站2021谷歌搜索入口
  • 南京做征信服务的公司网站抖音seo排名
  • 做网站需要学习哪些电商seo优化是什么
  • 外贸购物网站开发网络营销成功的案例
  • 一个动态网站多少钱互联网公司网站模板
  • 广告优化师工资一般多少长沙有实力的关键词优化价格
  • 五百丁简历模板官方网站关键词搜索排名公司
  • 微网站建设渠道江门关键词优化公司
  • 佛山外贸网站建设价位推广计划怎么做推广是什么
  • 曹县有没有做网站最新新闻热点事件