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

Why does Spring AOP fail?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, I will tell you why Spring AOP is invalid. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.

How to understand AOP agent.

Spring AOP is agent-based.

Before you write your own aspect or use any Spring AOP-based aspect provided by Spring Framework, it is important to understand the actual meaning of the last statement.

First consider a scenario where you have a normal, unproxied, nothing special, direct object reference, as shown in the following code snippet.

Public class SimplePojo implements Pojo {

Public void foo () {

/ / this next method invocation is a direct call on the

'this' reference this.bar ();} public void bar () {/ / some logic... }}

If a method is called on an object reference, the method is called directly on that object reference, as shown below.

Public class Main {

Public static void main (String [] args) {Pojo pojo = new SimplePojo (); / / this is a direct method call on the 'pojo' reference pojo.foo ();}}

The situation changes slightly when the reference owned by the client code is a proxy.

Consider the following diagram and code snippet.

Public class Main {

Public static void main (String [] args) {

ProxyFactory factory = new ProxyFactory (new

SimplePojo (); factory.addInterface (Pojo.class); factory.addAdvice (new RetryAdvice ()); Pojo pojo = (Pojo) factory.getProxy (); / / this is a method call on the proxy! Pojo.foo ();}}

The key to understand here is main (..) The client code in the.

The main class has a reference to the proxy.

This means that the method call referenced to the object will be a call to the proxy, so the proxy will be able to delegate to all interceptors (notifications) associated with that particular method call.

However, once the call finally reaches the target object (in this case, a SimplePojo reference), any method calls it may make to itself, such as this.bar () or this.foo (), will target the this reference rather than the proxy call.

This is of great significance.

This means that the self-invocation does not cause the notification associated with the method call to be executed.

Okay, so what do we do?

The best way (the term "best" is loosely used here) is to ReFactor your code so that self-invocation does not occur.

Of course, this does require you to do some work, but this is the best and least intrusive approach.

The next method is absolutely terrible, and I hardly want to point it out, precisely because it is so terrible.

You can (suffocate!) .

Fully bind the logic in the class to Spring AOP by doing the following:

Public class SimplePojo implements Pojo {

Public void foo () {

/ / this works, but... Gah! ((Pojo) AopContext.currentProxy ()) .bar ();} public void bar () {

/ / some logic... }}

This completely couples your code to Spring AOP, and it makes the class itself aware that it is used in the context of AOP, which runs counter to AOP.

Some additional configuration is required when creating the agent:

Public class Main {

Public static void main (String [] args) {ProxyFactory factory = new ProxyFactory

(new SimplePojo ()); factory.adddInterface (Pojo.class); factory.addAdvice (new RetryAdvice ()); factory.setExposeProxy (true); Pojo pojo = (Pojo) factory.getProxy ()

/ / this is a method call on the proxy! Pojo.foo ();}}

Finally, it is important to note that AspectJ does not have this self-invocation problem because it is not a proxy-based AOP framework.

Programmatic creation of @ AspectJ Proxies

In addition to using or declaring aspects in the configuration, you can also programmatically create proxies that provide advice to the target object.

For full details of Spring's AOP API, see the next chapter.

Here, we want to focus on the ability to automatically create agents using the @ AspectJ aspect.

The class org.springframework.aop.aspectj.annotation.AspectJProxyFactory can be used to create proxies for target objects suggested by one or more @ AspectJ aspects.

The basic usage of this class is very simple, as shown below.

For complete information, see Javadoc.

/ / create a factory that can generate a proxy for the given target object

AspectJProxyFactory factory = new AspectJProxyFactory (targetObject)

/ / add an aspect, the class must be an @ AspectJ aspect

/ / you can call this as many times as you need with different aspects

Factory.addAspect (SecurityManager.class)

/ / you can also add existing aspect instances, the type of the object supplied must be an @ AspectJ aspect

Factory.addAspect (usageTracker)

/ / now get the proxy object...

MyInterfaceType proxy = factory.getProxy ()

Use AspectJ in Spring applications.

So far, everything we've covered in this chapter is pure Spring AOP.

In this section, if your requirements exceed the capabilities provided by Spring AOP alone, we will look at how to use the AspectJ compiler / weaver instead of Spring AOP, or use the AspectJ compiler / weaver in addition to Spring AOP.

Spring comes with a small AspectJ aspect library, which is provided separately as Spring-aspects.jar in your distribution; you need to add it to the classpath to use the aspects in it.

This is why Spring AOP failed all the content, more content related to the failure of Spring AOP can search the previous articles or browse the following articles to learn ha! I believe the editor will add more knowledge to you. I hope you can support it!

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

Internet Technology

Wechat

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

12
Report