preface

Welcome to our GitHub repository Star: github.com/bin39232820… The best time to plant a tree was ten years ago, followed by now. I know many people don’t play QQ anymore, but for a moment of nostalgia, welcome to join the six-vein Shenjian Java rookie learning group, group chat number: 549684836 encourage everyone to write blog on the road of technology

omg

Recently xiao 66 encountered some real experience in the project, to record some records about AOP

AOP

For faceted programming,Spring AOP exists for decoupling, and AOP allows a set of classes to share the same behavior. AOP makes up for OOP’s shortcomings by only inheriting classes and implementing interfaces to increase the coupling degree of code, and class integration can only be single inheritance, preventing more behavior from being added to the same class.

Spring supports AspectJ’s annotated aspect programming.

  • Declare a facet using @aspectJ
  • Advice is defined with @after, @before, and @around, taking the interception rule (pointcut) directly as an argument.
  • The interception rules for @after, @before, and @around parameters are PointCut. To reuse pointcuts, you can use @pointcut to define interception rules and invoke them from @after, @before, and @around parameters.
  • Each intercepted point that meets the conditions is JoinPoint.

In fact, specific usage is also very simple, the following is small 66 code written by himself

But when I went to use it, I posted some problems,

  • The first one I used in FeignClient is a custom annotation similar to the one shown below

But I found a problem, this way, although we can go to the section successfully, but in the section to get the custom annotation parameter, always null. This one is really annoying, as follows

Why is it empty? Spring has enhanced the @feignClient annotation, so when you slice, you are already a proxy class, so you can’t get the content. Oh, my god. Okay, let’s try another one,

Small 66 thought of a way, I extract a method out, so I will be another method to cut the surface, so you can complete, ha ha, I want to perfect. However, this was not the case. In the end, I could not even get into the section. What’s the reason for fuck?

Cause analysis,

The reason for this is actually quite simple, because the method I extracted uses this, and this represents the current object, so let’s look at what the current object is

ServiceImp

Obviously, the current object is a Java object, and the call is a Spring-enhanced object, and only spring-enhanced objects have faceted capabilities.

So the conclusion is that the method caller, iPaperService, is an object instance that has not been enhanced by Spring AOP. So the idea to solve the problem is to find a way to use the enhanced HelloService instance to call!

The solution

Method one uses Autowired to inject an instance of itself

First of all, Spring supports circular dependencies, so it is possible to inject yourself,

Method two: Get the enhanced instance reference from the Spring context

At the end

So much for a little question. I hope I can bring help to the friends I meet

Daily for praise

Ok, everybody, that’s all for this article, you can see people here, they are real fans.

Creation is not easy, your support and recognition, is the biggest motivation for my creation, we will see in the next article

Six pulse excalibur | article “original” if there are any errors in this blog, please give criticisms, be obliged!