Compare and analyze the differences between Spring AOP and AspectJ

Source: Internet
Author: User

Aspect-oriented programming (AOP) is a programming paradigm designed to improve modularity by allowing separation of crosscutting concerns. AOP provides an aspect to modularize the point of focus across objects. While many AOP frameworks are now available, there are only two popular frameworks that we want to differentiate here: Spring AOP and AspectJ. This will help you choose the right technology for your project based on some key information.

Spring AOP differs from most other AOP frameworks. Spring AOP is not intended to provide the most complete AOP implementation (although spring AOP is quite capable), but rather to help solve common problems in enterprise applications, it provides a tight integration between the AOP implementation and the spring IOC. Because spring AOP is easy to implement, spring's goal will be one of the main points if you plan to modularize the crosscutting concerns on top of spring beans. But the same goal can be a limitation if you use a generic Java object instead of spring beans, and then modularize the crosscutting concerns based on this. On the other hand, ASPECTJ can be used for modularity based on common Java objects, but requires a good knowledge of the subject before it is implemented.

super0555 translated more than 3 years ago3 person TopTopWell translated!

Before deciding which framework to use to implement your project, there are a few key points that can help you make the right choice (the same applies to other frameworks).

Spring AOP is dedicated to providing an aspect-oriented framework that can be tightly integrated with the spring IOC to address common problems encountered in developing enterprise-level projects. Make clear whether you need to deal with spring beans or pojo when you apply crosscutting concerns (such as cross-cutting concern), such as things management, logs, or performance evaluations. If you are developing a new application, there is no resistance to choosing spring AOP. But if you are maintaining an existing application that does not use the spring framework, ASPECTJ will be a natural choice. To illustrate this, if you are using spring AOP, when you want to add log functionality as a notification (advice) to your app to track the program flow, then the notification (advice) can only be applied to the spring beans connection point ( Joinpoint) above.

Bearkidult translated more than 3 years ago4 person TopTopWell translated!

Example: In Appbeans.xml, the following pointcut (Pointcut) is configured, and the log notification (advice) is applied when the service method of the MyServices bean is called.

<!-configuration snippet in Appbeans.xml--<bean id=class=" Com.ashutosh.MyServicesImpl "/> <aop:config> <aop:aspect id=" Loggingaspect " Ref= "logging" > <aop:around method= "log" Pointcut= "Execution (public * * (..))" /> </aop:aspect> </aop:config-//Java file calling service Methodapplicationcontext Beans =newclasspathxmlapplicationcontext (//Logging advice applied here         

Take a look at the note where the log notification will be applied, where the application will record the details of the called method. However, when you call other methods in the same class in the service () method, if you do not use a proxy object, then the log notification is not applied to the method call.

For example:

MyServices Service methodService() {performoperation ();  Logging advice not going to apply here}  

If you want to apply a notification on a method that is called through the This object, you must use the Currentproxy object and call the appropriate method on it.

MyServices Service methodservice//Logging advice going to apply here ((myservices) aopcontext.cur Rentproxy ()). Performoperation ();} 

Similarly, if you want to apply a notification to a method on an object, you must use the spring bean that corresponds to the object.

Service() {  new MyObject (); Obj.performoperation (); //Logging advice not going to apply here}  

If you want to apply the notification, the above code must be modified to the following form.

Service() {  new MyObject (); Obj.performoperation (); //Logging advice not going to apply here ApplicationContext beans =newclasspathxmlapplicationcontext (" Appbeans.xml "); MyObject obj = (MyObject) beans.getbean ("MyObject"); Obj.performoperation ()//Logging advice applied here} 

Unlike this, using "AspectJ" You can apply notifications on any Java object without having to create or configure any beans in any file.

Bearkidult translated more than 3 years ago3 person TopTopWell translated!

Another factor to consider is whether you want to weave in during compilation (weaving), post-compilation (Post-compile), or Runtime (Run-time). Spring only supports run-time weaving. If you have multiple teams that develop multiple modules written using spring (resulting in multiple jar files, such as one jar file per module), and one team wants all spring beans in the entire project (for example, Include a jar file that has already been packaged by another team to apply the log notifications (where the log is just an example of a crosscutting concern), you can easily do this by configuring the team's own spring configuration file. This is possible because spring uses run-time weaving.

<!-configuration--><bean id=class="Com.ashutosh.MyServicesImpl"/> <aop:config>  <aop:aspect id="Loggingaspect" ref="Logging" > <aop:around method="log" pointcut=" Execution (Public * * (..)) " /> </aop:aspect></aop:config      --

If you use ASPECTJ to do the same thing, you may need to recompile all the code and repackage it using the ACJ (AspectJ compiler). Otherwise, you can also choose to weave with ASPECTJ after compilation (post-compile) or load-in (Load-time).

Bearkidult translated more than 3 years ago3 person TopTopWell translated!

Because spring is based on the proxy pattern (using cglib), it has a usage limit, that is, you cannot apply crosscutting concerns on beans that use final adornments. Because the proxy needs to inherit from the Java class, it cannot be done once the keyword final is used.

For example, use the keyword final on spring bean Myservicesimpl and configure a "execution (public * * (..))" Such a pointcut will result in a run-time exception (exception) because spring cannot generate a proxy for Myservicesimpl.

Configuration file<bean id=class="Com.ashutosh.MyServicesImpl"/>//java filefinal Classmyservicesimpl {---}   

In this case, you might consider using ASPECTJ, which supports compile-time weaving and does not require a build agent.

Similarly, it is not possible to apply crosscutting concerns on static and final methods. Because spring is based on proxy mode. If you configure notifications on these methods, you will cause a run-time exception because the static and final methods cannot be overwritten. In this case, you will also consider using ASPECTJ because it supports compile-time weaving and does not require a build agent.

Bearkidult translated more than 3 years ago2 person TopTopWell translated!

You will want to use an easy-to-implement approach. Because spring AOP supports annotations, it is more convenient to use @aspect annotation creation and configuration. With ASPECTJ, you need to create the facet through the. aj file, and you need to use the AJC (aspect compiler) to compile the code. So if you are sure that the limitations mentioned earlier will not be an obstacle to your project, use Spring AOP.

An indirect limitation of using ASPECTJ is that because ASPECTJ notifications can be applied to pojo, it is possible to apply notifications on top of a configured notification. For a wide range of applications where you have not noticed this problem, this can lead to an infinite loop.

Bearkidult translated more than 3 years ago2 person TopTopWell translated!

For example, create a facet that contains the following pointcuts.

Public Aspectlogging {  aroundexecution(public* * (..)) Sysytem.out. printlnreturn proceed ();}     

In this case, when proceed is about to be called, the log notification is applied again, which results in a nested loop.

So, if you want to apply crosscutting concerns in a relatively simple way on spring beans, and these beans are not labeled with the final modifier, and the similar method is not marked with the static or final modifier, use spring AOP. In contrast, if you need to apply crosscutting concerns on top of the mentioned restrictions, or if you want to apply concerns on Pojo, use ASPECTJ. You might also choose to use both methods, because spring supports this.

Reference Link: http://docs.spring.io/spring/docs/3.0.x/reference/aop.html

Compare and analyze the differences between Spring AOP and AspectJ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.