关于Spring AOP使用时的一些问题汇总

在使用AOP的时候遇到了一些问题,特此记录一下

首先写一个常用的AOP切片

切片类AopLog

package com.mantis.aop.aspect;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mantis.aop.common.util.DataUtil;
import eu.bitwalker.useragentutils.UserAgent;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.validation.BeanPropertyBindingResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:执行顺序 正常顺序 Around Before Method.invoke Around After AfterReturning
 * 异常顺序 Around Before Method.invoke After AfterThrowing
 * @author: wei.wang
 * @since: 2020/4/4 13:47
 * @history: 1.2020/4/4 created by wei.wang
 */
@Aspect
@Component
public class AopLog {

 private static Logger logger = LoggerFactory.getLogger(AopLog.class);

 /**
  * 定义切点,切点为com.smec.fin.controller包和子包里任意方法的执行和service层所有方法的执行
  */
 @Pointcut("execution(public * com.mantis.aop.controller..*.*(..))")
 public void log() {
  // 定义切点
 }

 /**
  * 定义切点,切点为com.smec.fin.controller包和子包里任意方法的执行和service层所有方法的执行
  */
 @Pointcut("execution(public * com.mantis.aop.service.impl..*.*(..))")
 public void log2() {
  // 定义切点
 }

 /**
  * 环绕通知
  *
  * @param point
  * @return
  * @throws Throwable
  */
 @Around("log2()")
 public Object aroundLog(ProceedingJoinPoint point) throws Throwable {
  logger.info("开始执行环绕操作");
  Object result = point.proceed();
  logger.info("执行环绕操作结束,返回值:{}", result);
  return result;
 }
}

服务类AopServiceImpl

package com.mantis.aop.service.impl;

import com.mantis.aop.service.AopService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @author: wei.wang
 * @since: 2020/10/24 12:45
 * @history: 1.2020/10/24 created by wei.wang
 */
@Service
public class AopServiceImpl implements AopService {

 @Override
 public void testAop(String str) {
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl.testAop" + str);
  this.testAop2("testFinalMethod");
 }

 @Override
 public void testAop2(String str) {
  //this.testFinalMethod("testFinalMethod");
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl." + str);
 }

 public final void testFinalMethod(String str) {
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod" + str);
 }

 public void testFinalMethod2(String str) {
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod" + str);
 }
}

1.同方法运行问题

在使用AOP时我们发现存在同类中调用时切点失效问题,在执行时我们发现只执行了testAop的切点,testAop2的切点没有执行,这是因为经过AOP代理后的对象都已经不是原来的对象了,而是加入了增强方法的代理对象,使用代理对象调用时可以执行增强方法,但是这里是使用this调用的,也就是AopServiceImpl,因为不是代理对象就没有增强方法。

2020-10-24 13:31:06.261 INFO 13344 --- [nio-9000-exec-1] com.mantis.aop.controller.AopController : 方法开始执行
2020-10-24 13:31:06.264 INFO 13344 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 开始执行环绕操作
com.mantis.aop.service.AopService.AopServiceImpl.testAoptest2
com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod

2020-10-24 13:31:06.274 INFO 13344 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 执行环绕操作结束,返回值:null

用@Autowired或Resource引入自身依赖

使用注解方法引入自身代理依赖,注意使用构造的方式会有循环依赖问题

 @Autowired
 AopServiceImpl aopService;

 @Override
 public void testAop(String str) {
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl.testAop" + str);
  aopService.testAop2("testFinalMethod");
  System.out.println();
 }
2020-10-24 13:36:33.477 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.controller.AopController : 方法开始执行
2020-10-24 13:36:33.480 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 开始执行环绕操作
com.mantis.aop.service.AopService.AopServiceImpl.testAoptest2
2020-10-24 13:36:33.488 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 开始执行环绕操作
com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod
2020-10-24 13:36:33.488 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 执行环绕操作结束,返回值:null

2020-10-24 13:36:33.490 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 执行环绕操作结束,返回值:null

开启暴露代理类,AopContext.currentProxy()方式获取代理类

通过暴露代理类方式,实际原理是把代理类添加到当前请求的ThreadLocal里面,然后在使用时从ThreadLocal中获取代理类,再调用对应的方法

在主方法上加入@EnableAspectJAutoProxy(exposeProxy=true),然后从AOP上下文中获取代理

 @Override
 public void testAop(String str) {
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl.testAop" + str);
  AopServiceImpl service = AopContext.currentProxy() != null ? (AopServiceImpl) AopContext.currentProxy() : this;
  service.testAop2("testFinalMethod");
  System.out.println();
 }
2020-10-24 13:38:31.031 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.controller.AopController : 方法开始执行
2020-10-24 13:38:31.035 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 开始执行环绕操作
com.mantis.aop.service.AopService.AopServiceImpl.testAoptest2
2020-10-24 13:38:31.047 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 开始执行环绕操作
com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod
2020-10-24 13:38:31.048 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 执行环绕操作结束,返回值:null

2020-10-24 13:38:31.050 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 执行环绕操作结束,返回值:null

2.final关键字问题

Spring AOP默认使用cglib,会生成目标对象的子类代理对象。调用目标对象的方法,实际上是调用代理对象的方法。由于子类能够继承父类的方法,因此一般情况下目标类的方法,代理对象都会有。但是当目标类中某个方法带有final关键字时,这个方法不能被重写,因此代理对象中没有这个方法,因此会调用目标对象的方法。

带final关键字

因为testFinalMethod方法中存在final关键字,导致无法重写,结果代理对象就无法生成这个方法,因此调用目标对象方法,导致没有被增强

 @Override
 public void testAop(String str) {
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl.testAop" + str);
  AopServiceImpl service = AopContext.currentProxy() != null ? (AopServiceImpl) AopContext.currentProxy() : this;
  service.testFinalMethod("testFinalMethod");
  System.out.println();
 }

 public final void testFinalMethod(String str) {
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod" + str);
 }
2020-10-24 13:47:46.907 INFO 15204 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 开始执行环绕操作
com.mantis.aop.service.AopService.AopServiceImpl.testAoptest2
com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethodtestFinalMethod

2020-10-24 13:47:46.916 INFO 15204 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 执行环绕操作结束,返回值:null

不带final关键字

因为testFinalMethod方法中不存在final关键字,所以正常执行增强。

 @Override
 public void testAop(String str) {
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl.testAop" + str);
  AopServiceImpl service = AopContext.currentProxy() != null ? (AopServiceImpl) AopContext.currentProxy() : this;
  service.testFinalMethod2("testFinalMethod");
  System.out.println();
 }

 public final void testFinalMethod2(String str) {
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod" + str);
 }
2020-10-24 13:50:51.018 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.controller.AopController : 方法开始执行
2020-10-24 13:50:51.021 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.aspect.AopLog    : 开始执行环绕操作
com.mantis.aop.service.AopService.AopServiceImpl.testAoptest2
2020-10-24 13:50:51.029 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.aspect.AopLog    : 开始执行环绕操作
com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethodtestFinalMethod
2020-10-24 13:50:51.030 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.aspect.AopLog    : 执行环绕操作结束,返回值:null

2020-10-24 13:50:51.031 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.aspect.AopLog    : 执行环绕操作结束,返回值:null

总结

到此这篇关于Spring AOP使用时的一些问题汇总的文章就介绍到这了,更多相关Spring AOP使用时的问题内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Spring Boot中自定义注解结合AOP实现主备库切换问题

    摘要:本篇文章的场景是做调度中心和监控中心时的需求,后端使用TDDL实现分表分库,需求:实现关键业务的查询监控,当用Mybatis查询数据时需要从主库切换到备库或者直接连到备库上查询,从而减小主库的压力,在本篇文章中主要记录在Spring Boot中通过自定义注解结合AOP实现直接连接备库查询. 一.通过AOP 自定义注解实现主库到备库的切换 1.1 自定义注解 自定义注解如下代码所示 import java.lang.annotation.ElementType; import java.la

  • 解决springboot的aop切面不起作用问题(失效的排查)

    检查下springboot的启动类是否开启扫描 @SpringBootApplication @ComponentScan(basePackages = {"com.zhangpu.springboot"}) 另外springboot默认开启的EnableAspectJAutoProxy为true 如果不放心可以增加: @EnableAspectJAutoProxy(proxyTargetClass=true) 第二种可能: 没有导入 相关的jar <dependency>

  • spring boot 使用Aop通知打印控制器请求报文和返回报文问题

    一.简介 开发过程中我们往往需要写许多例如: @GetMapping("/id/get") public Result getById( String id) throws Exception{ log.info("请求参数为:"+id); verify(new VerifyParam("部门id", id)); Result result = new Result("通过id获取部门信息成功!", service.query

  • Springboot使用@Valid 和AOP做参数校验及日志输出问题

    项目背景 最近在项目上对接前端的的时候遇到了几个问题 1.经常要问前端要请求参数 2.要根据请求参数写大量if...else,代码散步在 Controller 中,影响代码质量 3.为了解决问题1,到处记日志,导致到处改代码 解决方案 为了解决这类问题,我使用了@Valid 做参数校验,并使用AOP记录前端请求日志 1.Bean实体类增加注解 对要校验的实体类增加注解,如果实体类中有List结构,就在List上加@Valid @Valid注解 注解 备注 @Null 只能为null @NotNu

  • 关于Spring AOP使用时的一些问题汇总

    在使用AOP的时候遇到了一些问题,特此记录一下 首先写一个常用的AOP切片 切片类AopLog package com.mantis.aop.aspect; import com.fasterxml.jackson.databind.ObjectMapper; import com.mantis.aop.common.util.DataUtil; import eu.bitwalker.useragentutils.UserAgent; import org.aspectj.lang.JoinP

  • Spring AOP源码深入分析

    目录 1. 前言 2. 术语 3. 示例 4. @EnableAspectJAutoProxy 5. AbstractAutoProxyCreator 6. 构建Advisor 7. 创建代理对象 8. DynamicAdvisedInterceptor 9. CglibMethodInvocation 10. Advice子类 1. 前言 Spring除了IOC和DI,还有另一个杀手锏功能——Spring AOP.AOP是一种面向切面的编程思想,它的关注点是横向的,不同于OOP的纵向.面向对象

  • Spring中异步注解@Async的使用、原理及使用时可能导致的问题及解决方法

    前言 其实最近都在研究事务相关的内容,之所以写这么一篇文章是因为前面写了一篇关于循环依赖的文章: <Spring循环依赖的解决办法,你真的懂了吗> 然后,很多同学碰到了下面这个问题,添加了Spring提供的一个异步注解@Async循环依赖无法被解决了,下面是一些读者的留言跟群里同学碰到的问题: 本着讲一个知识点就要讲明白.讲透彻的原则,我决定单独写一篇这样的文章对@Async这个注解做一下详细的介绍,这个注解带来的问题远远不止循环依赖这么简单,如果对它不够熟悉的话建议慎用. 文章要点 @Asy

  • Spring AOP日志框架实现过程图解

    AOP日志框架实现 JDK动态代理实现日志框架 首先,在项目包com.ay.test 下创建业务接口类BusinessClassService,具体代码如下: BusinessC lassService 业务接口类可以理解为日常开发业务创建的接口类, 接口中有一个简 单的方法doSomeThing .然后,开发业务类的实现类BusinessClassServiceImpl,具体代码如下: 实现类BusinessClassServicelmpl 实现了BusinessClassServ ice 接

  • 解决Spring AOP 同类调用失效问题

    目录 背景 一 目标 二 概述 三 工作中遇到的实例 1.源代码 2.原因分析 四 解决方法 1.方法一 2.方法二 原理: 3.方法三 4.总结 背景 当初刚到公司实习不久,就遇到一个问题:在同一个类中,调用有@redis,@Transactional的方法,发现注解失效了.因为这个问题,我当时纠结了很久.最后,还是老大一语惊醒梦中人,归根结底,还是Spring Aop原理没有理解彻底.今天就来聊一下关于"Spring AOP 同类调用失效问题". 一 目标 深入探究"Sp

  • Spring AOP 基于注解详解及实例代码

    Spring AOP  基于注解详解及实例代码 1.启用spring对@AspectJ注解的支持: <beans xmlns:aop="http://www.springframework.org/schema/aop"...> <!--启动支持--> <aop:aspectj-autoproxy /> </beans> 也可以配置AnnotationAwareAspectJAutoProxyCreator Bean来启动Spring对@

  • Spring AOP切面解决数据库读写分离实例详解

    Spring AOP切面解决数据库读写分离实例详解 为了减轻数据库的压力,一般会使用数据库主从(master/slave)的方式,但是这种方式会给应用程序带来一定的麻烦,比如说,应用程序如何做到把数据写到master库,而读取数据的时候,从slave库读取.如果应用程序判断失误,把数据写入到slave库,会给系统造成致命的打击. 解决读写分离的方案很多,常用的有SQL解析.动态设置数据源.SQL解析主要是通过分析sql语句是insert/select/update/delete中的哪一种,从而对

  • Spring AOP实现Redis缓存数据库查询源码

    应用场景 我们希望能够将数据库查询结果缓存到Redis中,这样在第二次做同样的查询时便可以直接从redis取结果,从而减少数据库读写次数. 需要解决的问题 操作缓存的代码写在哪?必须要做到与业务逻辑代码完全分离. 如何避免脏读? 从缓存中读出的数据必须与数据库中的数据一致. 如何为一个数据库查询结果生成一个唯一的标识?即通过该标识(Redis中为Key),能唯一确定一个查询结果,同一个查询结果,一定能映射到同一个key.只有这样才能保证缓存内容的正确性 如何序列化查询结果?查询结果可能是单个实体

  • 详解Spring Aop实例之AspectJ注解配置

    上篇<Spring Aop实例之xml配置>中,讲解了xml配置方式,今天来说说AspectJ注解方式去配置spring aop. 依旧采用的jdk代理,接口和实现类代码请参考上篇博文.主要是将Aspect类分享一下: package com.tgb.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Aft

  • 浅谈SpringBoot集成Redis实现缓存处理(Spring AOP实现)

    第一章 需求分析 计划在Team的开源项目里加入Redis实现缓存处理,因为业务功能已经实现了一部分,通过写Redis工具类,然后引用,改动量较大,而且不可以实现解耦合,所以想到了Spring框架的AOP(面向切面编程). 开源项目:https://github.com/u014427391/jeeplatform 第二章 SpringBoot简介 Spring框架作为JavaEE框架领域的一款重要的开源框架,在企业应用开发中有着很重要的作用,同时Spring框架及其子框架很多,所以知识量很广.

随机推荐