• 事务(十三)


    Java service类中调用同类中的另一个方法时,aop不生效的解决方法

    1:演示代码内容

    1.1:Controller
     1 package com.ydg.cloud.lock.controller;
     2 
     3 import com.ydg.cloud.lock.service.TestService;
     4 import lombok.extern.slf4j.Slf4j;
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.web.bind.annotation.GetMapping;
     7 import org.springframework.web.bind.annotation.RestController;
     8 
     9 /**
    10  * @author YDG
    11  * @description
    12  * @since 2019-04-23
    13  */
    14 @Slf4j
    15 @RestController
    16 public class TestLockController {
    17 
    18 
    19     @Autowired
    20     private TestService testService;
    21 
    22     @GetMapping("test")
    23     public void test() {
    24         testService.testLock(1L);
    25     }
    26 }
    1.2:Service
     1 package com.ydg.cloud.lock.service;
     2 
     3 /**
     4  * @author YDG
     5  * @description
     6  * @since 2019-07-20
     7  */
     8 public interface TestService {
     9 
    10     void testLock(Long id);
    11 
    12     void testLock2(Long id);
    13 }
    1.3:ServiceImpl
     1 package com.ydg.cloud.lock.service.impl;
     2 
     3 import com.ydg.cloud.lock.annotation.Lock;
     4 import com.ydg.cloud.lock.service.TestService;
     5 import org.springframework.stereotype.Service;
     6 
     7 /**
     8  * @author YDG
     9  * @description
    10  * @since 2019-07-20
    11  */
    12 @Service
    13 public class TestServiceImpl implements TestService {
    14 
    15     @Override
    16     public void testLock(Long id) {
    17         assert id != null;
    18         this.testLock2(id);
    19     }
    20 
    21     @Override
    22     @Lock(keyName = "id")
    23     public void testLock2(Long id) {
    24         System.out.println("需要加锁的id是:" + id);
    25     }
    26 }

    2:不生效

    如上面的代码,在controller中注入了TestSevice,然后调用了TestService的testLock方法,在testLock方法中调用了testLock2方法,其中testLock2方法上面加了@Lock注解,这个注解是我自己写的一个分布式锁注解,里面用到了aop,会在进入加了@Lock注解的方法的时候,将keyName作为分布式锁的key对资源进行加锁,但是实际的执行状况是aop不生效

    3:使之生效

     1 package com.ydg.cloud.lock.service.impl;
     2 
     3 import com.ydg.cloud.lock.annotation.Lock;
     4 import com.ydg.cloud.lock.service.TestService;
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.context.ApplicationContext;
     7 import org.springframework.stereotype.Service;
     8 
     9 /**
    10  * @author YDG
    11  * @description
    12  * @since 2019-07-20
    13  */
    14 @Service
    15 public class TestServiceImpl implements TestService {
    16     @Autowired
    17     private ApplicationContext applicationContext;
    18 
    19     @Override
    20     public void testLock(Long id) {
    21         assert id != null;
    22         TestService testService = applicationContext.getBean(TestService.class);
    23         testService.testLock2(id);
    24     }
    25 
    26     @Override
    27     @Lock(keyName = "id")
    28     public void testLock2(Long id) {
    29         System.out.println("需要加锁的id是:" + id);
    30     }
    31 }

    我换了种方法,通过applicationContext拿到TestService的cglib动态代理对象,就行了,就能在testLock方法调用testLock2方法的时候使testLock2的aop生效

    转载:https://blog.csdn.net/qq1300375795/article/details/96575743

    带着疑问去思考,然后串联,进而归纳总结,不断追问自己,进行自我辩证,像侦查嫌疑案件一样看待技术问题,漆黑的街道,你我一起寻找线索,你就是技术界大侦探福尔摩斯
  • 相关阅读:
    [LeetCode]62. Excel Sheet Column Title Excel列序号
    [LeetCode]61. Excel Sheet Column Number Excel列序号
    [LeetCode]60. Rectangle Area矩形面积
    [LeetCode]59. H-Index H指数
    [LeetCode]58. Fraction to Recurring Decimal分数化小数
    [LeetCode]57. Binary Tree Inorder Traversal中序遍历二叉树
    Insert or Merge
    Root of AVL Tree
    是否同一棵二叉搜索树
    List Leaves
  • 原文地址:https://www.cnblogs.com/cainiao-Shun666/p/14532359.html
Copyright © 2020-2023  润新知