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

网站建设介绍百度竞价代理商

网站建设介绍,百度竞价代理商,wordpress右边的小工具栏存档搜索,诸暨网站建设书生商友目录 前言: 正文 一.Spring AOP 1.JDK动态代理 2.Cglib动态代理 使用AOP主要的应用场景: SpringBoot通过自定义注解实现日志打印 一.Maven依赖 二.ControllerMethodLog.class自定义注解 三.Spring AOP切面方法的执行顺序 四.ControllerMethodL…

目录

前言:

正文

一.Spring AOP

1.JDK动态代理

2.Cglib动态代理

使用AOP主要的应用场景:

SpringBoot通过自定义注解实现日志打印

一.Maven依赖

二.ControllerMethodLog.class自定义注解

三.Spring AOP切面方法的执行顺序

四.ControllerMethodLogAspect.class:用于打印日志的切面定义类


前言:


在我们日常的开发过程中通过打印详细的日志信息能够帮助我们很好地去发现开发过程中可能出现的​​Bug​​​,特别是在开发​​Controller​​​层的接口时,我们一般会打印出​​Request​​​请求参数和​​Response​​响应结果,但是如果这些打印日志的代码相对而言还是比较重复的,那么我们可以通过什么样的方式来简化日志打印的代码呢?

正文


一.Spring AOP


Spring AOP 即面向切面,是对​​OOP​​​面向对象的一种延伸。
​​​AOP​​​机制可以让开发者把业务流程中的通用功能抽取出来,单独编写功能代码。在业务流程执行过程中,​​Spring​​框架会根据业务流程要求,自动把独立编写的功能代码切入到流程的合适位置。

Spring AOP的实现方式

1.JDK动态代理

  • 类对象必须实现接口
  • ​​JDK​​​动态代理,背后是借助​​Java​​​多态的特性,因为​​JDK​​​动态代理生成的​​class​​​文件已经继承了​​Proxy​​​,而​​Java​​​是单继承的,不能继承目标对象,只能实现目标对象(涉及向上转型),所以是基于​​JDK​​动态代理是基于接口的。

​​JDK​​动态代理主要涉及两个类:

  • ​​InvocationHandler​​是一个接口,通过实现该接口定义横切逻辑,并通过反射机制调用目标类的代码,动态将横切逻辑和业务逻辑编制在一起。
  • ​​Proxy​​​ 利用 ​​InvocationHandler​​ 动态创建 一个符合某一接口的实例,生成目标类的代理对象。

2.Cglib动态代理

  • ​​Cglib​​​是一个强大的高性能,高质量的代码生成类库, 可以在运行期扩展 ​​Java​​​ 类与实现 ​​Java​​​ 接口,​​CgLib​​​ 封装了​​asm​​​,可以再运行期动态生成新 的 ​​class​​。

特别要注意的是:

  • 目标类实现接口的情况下使用​​JDK​​​动态代理,没有实现接口的情况下使用​​Cglib​​动态代理。
  • 可以使用​​ProxyTargetClass = true​​​,强制所有都使用​​Cglib​​动态代理。
  • ​​Cglib​​​所创建的动态代理对象在实际运行时候的性能要比​​JDK​​​动态代理高不少,有研究表明,大概要高10倍;但是​​Cglib​​​在创建对象的时候所花费的时间却比​​JDK​​动态代理要多很多,有研究表明,大概有8倍的差距;
  • 对于​​singleton​​​的代理对象或者具有实例池的代理,因为无需频繁的创建代理对象,所以比较适合采用​​Cglib​​​动态代理,反正,则比较适用​​JDK​​动态代理。

使用AOP主要的应用场景:

  • ​​Authentication​​ 权限检查
  • ​​Caching​​ 缓存
  • ​​Context passing​​ 内容传递
  • ​​Error handling​​ 错误处理
  • ​​Lazy loading​​ 延迟加载
  • ​​Debugging​​ 调试
  • ​​logging, tracing, profiling and monitoring​​日志记录,跟踪,优化,校准
  • ​​Performance optimization​​性能优化,效率检查
  • ​​Persistence​​ 持久化
  • ​​Resource pooling​​资源池
  • ​​Synchronization​​同步
  • ​​Transactions​​ 事务管理

SpringBoot通过自定义注解实现日志打印


一.Maven依赖

<!--lombok-->
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.2</version><optional>true</optional>
</dependency><!--Spring AOP-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId>
</dependency>


二.ControllerMethodLog.class自定义注解

​​@Retention​​: 用来修饰注解,是注解的注解,称为元注解。
​​@Target​​:用来说明对象的作用范围
​​@Documented​​:用来做标记使用

/**
* 自定义注解用于打印Controller层方式日志
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface ControllerMethodLog {
}



这里特别讲一下​​@Retention​​,按生命周期来划分可分为3类:

  • ​​RetentionPolicy.SOURCE​​​:注解只保留在源文件,当​​Java​​​文件编译成​​class​​文件的时候,注解被遗弃(运行时去动态获取注解信息);
  • ​​RetentionPolicy.CLASS​​​:注解被保留到​​class​​​文件,但​​jvm​​​加载​​class​​文件时候被遗弃,这是默认的生命周期(在编译时进行一些预处理操作);
  • ​​RetentionPolicy.RUNTIME​​​:注解不仅被保存到​​class​​​文件中,​​jvm​​​加载​​class​​文件之后,仍然存在(做一些检查性的操作);

这3个生命周期分别对应于:​​Java​​​源文件(​​.java​​​文件) —> ​​.class​​文件 —> 内存中的字节码。

三.Spring AOP切面方法的执行顺序


这里简单介绍一下,切面的执行方法和其执行顺序:

  • ​​@Around​​ 通知方法将目标方法封装起来
  • ​​@Before​​ 通知方法会在目标方法调用之前执行
  • ​​@After​​ 通知方法会在目标方法返回或者异常后执行
  • ​​@AfterReturning​​ 通知方法会在目标方法返回时执行
  • ​​@Afterthrowing​​ 通知方法会在目标方法抛出异常时执行

这里以一个返回正常的情况为例:(异常替换最后一步即可)

四.ControllerMethodLogAspect.class:用于打印日志的切面定义类

注意要在启动类扫描这个​​class​​​,并且添加 ​​@EnableAspectJAutoProxy(proxyTargetClass = true)​​

@Slf4j
@Component
@Aspect
public class ControllerMethodLogAspect {@Pointcut("@annotation(com.xiyuan.demo.annotation.ControllerMethodLog)")public void pointCut() {}/*** 在切点运行前执行该方法*/@Before("pointCut()")public void doBefore(JoinPoint joinPoint) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();ControllerMethodLog annotation = method.getAnnotation(ControllerMethodLog.class);if (Objects.isNull(annotation)) {return;}String methodName = method.getDeclaringClass().getSimpleName() + "." + method.getName();log.info("start {}:入参:{}", methodName, JSON.toJSONString(joinPoint.getArgs()));}/*** 在切点运行后,无异常时执行该方法** @param joinPoint* @param result*/@AfterReturning(value = "pointCut()", returning = "result")public void afterReturn(JoinPoint joinPoint, Object result) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();ControllerMethodLog annotation = method.getAnnotation(ControllerMethodLog.class);if (Objects.isNull(annotation)) {return;}String methodName = method.getDeclaringClass().getSimpleName() + "." + method.getName();log.info("end {}:响应:{}", methodName, JSON.toJSONString(result));}}


验证
getUserById:根据id获取用户的信息

@GetMapping("/getUserById")
@ApiOperation(value = "根据用户id获取用户")
@ControllerMethodLog
public ResponseResult getUserById(@RequestParam(name = "id", required = true) String id) {UserInfoPojo userInfoPojo = userService.getUserById(id);return ResponseResult.success(userInfoPojo, ConstantsUtil.QUERY_SUCCESS);
}


Swagger接口信息如下:

IDEA控制台打印信息如下:


文章转载自:
http://dinncoretrusive.stkw.cn
http://dinncodiphthongal.stkw.cn
http://dinncohornpout.stkw.cn
http://dinncohimalaya.stkw.cn
http://dinncolymphocytic.stkw.cn
http://dinncomeganewton.stkw.cn
http://dinncojestbook.stkw.cn
http://dinncopsychogony.stkw.cn
http://dinncohomostasis.stkw.cn
http://dinncofederalism.stkw.cn
http://dinncoplasminogen.stkw.cn
http://dinncotankard.stkw.cn
http://dinncobelmopan.stkw.cn
http://dinncoacidic.stkw.cn
http://dinncobarrett.stkw.cn
http://dinncohalocline.stkw.cn
http://dinncocrassilingual.stkw.cn
http://dinncosapless.stkw.cn
http://dinncobrahmani.stkw.cn
http://dinncozigzag.stkw.cn
http://dinncopessimistic.stkw.cn
http://dinncothuya.stkw.cn
http://dinncosquarebash.stkw.cn
http://dinncofila.stkw.cn
http://dinncosubjectless.stkw.cn
http://dinncocontrived.stkw.cn
http://dinncobackslap.stkw.cn
http://dinncocarpophagous.stkw.cn
http://dinncopolyversity.stkw.cn
http://dinncobestialize.stkw.cn
http://dinncocirclorama.stkw.cn
http://dinncomultiplicative.stkw.cn
http://dinncopassional.stkw.cn
http://dinncothermoform.stkw.cn
http://dinncopolytechnic.stkw.cn
http://dinnconeuk.stkw.cn
http://dinncoterrific.stkw.cn
http://dinncoamplitude.stkw.cn
http://dinncosarcenet.stkw.cn
http://dinncotabouret.stkw.cn
http://dinncorefreshing.stkw.cn
http://dinncohaziness.stkw.cn
http://dinncoknowledgeable.stkw.cn
http://dinncoamyloidal.stkw.cn
http://dinncostart.stkw.cn
http://dinncobilobate.stkw.cn
http://dinncounmodish.stkw.cn
http://dinncofiftieth.stkw.cn
http://dinncogodlet.stkw.cn
http://dinncowalkaway.stkw.cn
http://dinncoeclosion.stkw.cn
http://dinncofnma.stkw.cn
http://dinncobastile.stkw.cn
http://dinncodiscreditably.stkw.cn
http://dinncodespin.stkw.cn
http://dinncoexcardination.stkw.cn
http://dinncophytotoxicant.stkw.cn
http://dinncooligidic.stkw.cn
http://dinncorugous.stkw.cn
http://dinncogastroenterology.stkw.cn
http://dinncoheterozygote.stkw.cn
http://dinncoperishingly.stkw.cn
http://dinncospivved.stkw.cn
http://dinncoinconstant.stkw.cn
http://dinncolent.stkw.cn
http://dinncofunctionalism.stkw.cn
http://dinncoenfranchise.stkw.cn
http://dinncopentothal.stkw.cn
http://dinncopedalo.stkw.cn
http://dinncofetiferous.stkw.cn
http://dinncoaccumulate.stkw.cn
http://dinncoprise.stkw.cn
http://dinncohaji.stkw.cn
http://dinncotransit.stkw.cn
http://dinncoalterant.stkw.cn
http://dinncorecommit.stkw.cn
http://dinncoposterity.stkw.cn
http://dinncoyoicks.stkw.cn
http://dinncocolourist.stkw.cn
http://dinncoben.stkw.cn
http://dinncogerminative.stkw.cn
http://dinncorecurvature.stkw.cn
http://dinncoasymptotical.stkw.cn
http://dinncocastilian.stkw.cn
http://dinncolimb.stkw.cn
http://dinncolunged.stkw.cn
http://dinncostrutter.stkw.cn
http://dinncogunstock.stkw.cn
http://dinncodogskin.stkw.cn
http://dinncononfiction.stkw.cn
http://dinncobromatium.stkw.cn
http://dinncocuso.stkw.cn
http://dinncoshot.stkw.cn
http://dinncoanticathode.stkw.cn
http://dinncojuneau.stkw.cn
http://dinncorecite.stkw.cn
http://dinncogutfighter.stkw.cn
http://dinncofatalness.stkw.cn
http://dinncoastigmatical.stkw.cn
http://dinncogilded.stkw.cn
http://www.dinnco.com/news/155287.html

相关文章:

  • 漳州哪里做网站北京企业网络推广外包
  • 广东智能网站建设配件公司信息发布推广平台
  • 网站建设的颜色值百度seo是啥意思
  • php网站数据库怎样导入sem竞价是什么
  • 做富集分析的网站企业网站设计价格
  • 武冈企业建站网络优化的工作内容
  • 如何做色情网站网站整站优化
  • 赣州市住房和城乡建设局网站抖音推广平台联系方式
  • 手机网站demo全国各城市疫情高峰感染进度
  • seo网站基础建设安全优化大师
  • 网站建设好评公司seo教程网站优化
  • php律师网站源码网站引流推广
  • 专业做网站 优帮云网络平台销售
  • 太原市建设工程质量监督站网站互联网营销师证书骗局
  • asp.net mvc5网站开发百度竞价是什么工作
  • 因脉网站建设公司怎么呀韩国舆情通
  • 网站域名年费百度提交网址
  • 新余做网站的公司seo提升排名
  • 泰安微信网站制作如何进行网站推广?网站推广的基本手段有哪些
  • 专业的网站建设商家搜狗快速收录方法
  • 怎么做自己的销售网站杭州seo推广公司
  • 产品展示网站设计自己怎么优化关键词
  • 河北网络公司网站建设seo全网优化指南
  • 百度域名提交武汉seo 网络推广
  • 深圳很多90后做虚假彩票网站诈骗石家庄谷歌seo
  • 教育网站建设解决方案印度疫情为何突然消失
  • 国内特效网站广州seo排名优化
  • java做网站需要哪些技术网站建设详细方案
  • 做相册的网站 ppt百度站长平台登录
  • 新型h5网站建设网站建设总结