Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the code that cannot be intercepted by calling the current class method by Spring this

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to write code that cannot be intercepted by calling the current class method by Spring this". In daily operation, I believe many people have doubts about how to write code that cannot be intercepted by calling the current class method by Spring this. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt that "how to write code that cannot be intercepted by calling the current class method by Spring this". Next, please follow the editor to study!

First, give a code example.

Package com.example.demo.service;import org.springframework.stereotype.Service;@Servicepublic class ProxyService {public void testA () {System.out.println ("enter A"); this.testB ();} public void testB () {System.out.println ("enter b");}} package com.example.demo.annotation;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect Import org.springframework.stereotype.Component;@Aspect@Componentpublic class AspectjTest {@ Around ("execution (* com.example.demo.service.ProxyService.testB ()") public void recordProxy (ProceedingJoinPoint joinPoint) throws Throwable {long start = System.currentTimeMillis (); joinPoint.proceed (); long end = System.currentTimeMillis (); System.out.println ("time spent:" + (end-start));} package com.example.demo.api Import com.example.demo.service.ProxyService;import com.example.demo.service.UserService;import org.springframework.aop.framework.AopContext;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PostMapping;@Controllerpublic class ProxyApi {/ / @ Autowired// ProxyService proxyService1; @ Autowired private ApplicationContext applicationContext @ PostMapping ("/ proxy") public String test1 () {ProxyService proxyService1 = applicationContext.getBean (ProxyService.class);; proxyService1.testA (); return "string";}}

Run the above code to find that configuring the aop intercept method will not be executed

Let's look at the difference between proxyService1 and this through debug and see what their values are.

Found to be different, in fact, this is the cause of the problem.

1. When we intercept the aop configuration, we will specify the method path under the class. When spring starts, we will first load the ProxyService class and generate a bean. But because you have configured it with aop, you need to proxy the ProxyService class, so the bean object in the spring container is the proxied bean object. Therefore, we are using the container to obtain the bean or the dependency injection to obtain the address path of the bean to show the proxied bean.

2. A reference to the current object method obtained by this, so when calling the testB method, you do not use the proxied object, and the self-heating will not be intercepted by aop. The principle is the same as when we use ordinary dynamic proxies, only proxy objects can take custom methods.

3. You can view the zhi value before and after the ProxyService class is proxied through debug

It is found that it is consistent with the values in the previous debug screenshot.

The solution is to use the bean object in the spring container when calling the testB method

@ Servicepublic class ProxyService {@ Autowired private ProxyService proxyService; public void testA () {System.out.println ("enter A"); proxyService.testB ();} public void testB () {System.out.println ("enter b");}

Or

@ Servicepublic class ProxyService {public void testA () {System.out.println ("enter A"); ProxyService currentProxy = (ProxyService) AopContext.currentProxy (); currentProxy.testB ();} public void testB () {System.out.println ("enter b");}}

The final result is correct.

At this point, the study on "how to write code that cannot be intercepted by Spring this calling the current class method" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report