Compare and analyze the differences between spring AOP and AspectJ English original: Comparative analysis between Spring AOP and AspectJ
Tags:AspectJ Spring
158 People collection This article, I want to collect Oschina recommended 3 years ago (total 7 paragraphs, translated 10-24) (19 reviews) participation in Translation (2 persons):Bearkidult, super0555 Chinese only | Chinese-English Comparison | English only | Print this article
aspect-oriented programming (AOP) is a programming paradigm designed to improve modularity by allowing the 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 3 years ago 3 Human top top Good translation! |
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 over 3 years ago4 Person Top Top translation of good Oh! |
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. ?
123456789101112131415161718192021 |
<!—Configuration snippet in appbeans.xml -->
<bean id=
"myServices" 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 method
ApplicationContext beans =newClassPathXmlApplicationContext(
"appbeans.xml"
);
MyServices myServices = (MyServices) beans.getBean(
"myServices"
);
myServices.service();
// 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: ?
1234567 |
// MyServices service method public void service() { 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. ?
123456789 |
// MyServices service method public void service() { // Logging advice going to apply here ((MyServices) AopContext.currentProxy()).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. ?
1234567 |
public void service() { MyObject obj = 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. ?
12345678910111213 |
public void service() {
MyObject obj =
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 over 3 years ago3 Person Top Top translation of good Oh! |
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. ?
12345678910111213 |
<!—Configuration -->
<bean id=
"myServices" 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 over 3 years ago3 Person Top Top translation of good Oh! |
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. ?
1234567891011 |
// Configuration file <bean id= "myServices" class = "com.ashutosh.MyServicesImpl" /> //Java file public final 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 over 3 years ago2 Person top Top translation of good Oh! |
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 over 3 years ago2 Person top Top translation of good Oh! |
For example, create a facet that contains the following pointcuts. ?
123456789 |
public aspectLogging { Object around() : execution( public * * (..)) Sysytem.out.println(thisJoinPoint.getSignature()); return 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