Spring AOP

Spring AOP is a Java-compatible and easy-to-use framework, which is used to integrate AOP in the Spring Framework. It provides a close integration of AOP implementation in components, taking advantage of Spring IoC. It is a proxy-based framework, which can be used upon method execution.

The Spring AOP fulfills maximum requirements to apply cross-cutting concerns. However, the following are the few limitations where Spring AOP cannot be applied:

  • Spring AOP cannot be applied on fields
  • We cannot apply any other Aspect on one Aspect
  • Private and protected methods can't be advised
  • Constructors cannot be advised

Spring supports AspectJ and Spring AOP integration to use cross-cutting concerns with less coding. Both Spring AOP and AspectJ are used for implementation of cross-cutting technology, but the following are a few points that help developers make the best choice, which is to be used in implementation:

  • Spring AOP is based on a dynamic proxy, which supports only method join points; however, AspectJ can be applied on fields and constructors even if they are private, public, or protected, and it supports fine-grained advice.
  • Spring AOP cannot be used on a method that calls methods of the same class, or that is static or final, but AspectJ can.
  • AspectJ doesn't need the Spring container to manage a component, while Spring AOP can be used only with the components that are managed by the Spring container.
  • Spring AOP supports runtime weaving based on proxy pattern, and AspectJ supports compile time weaving, which does not require proxy creation. The proxy of the objects is created once the bean is asked by the application.
  • Aspects written by Spring AOP are Java-based components, but those written in AspectJ are with language, which is an extension of Java, so the developers need to learn it before using.
  • Spring AOP is easy to implement by annotating a class with the @Aspect annotation, or by simple configuration. However, to use AspectJ, one needs to create the *.aj files.
  • Spring AOP doesn't require any special container, but aspects need them, as aspects created using AspectJ need to compile using the AspectJ compiler.
  • AspectJ is the best choice for applications that already exist.
A simple class without final and static methods simply uses Spring AOP; otherwise, choose AspectJ to write the aspects.

Let's discuss Spring AOP and its ways of implementation in depth. The Spring AOP can be implemented using either XML-based aspect configuration or AspectJ style annotation-based implementation. The XML-based configuration can be split up at several points, making it a bit complex. In XML, we cannot define the named pointcuts. However, the aspect written by annotations is within the single module that supports writing named pointcuts. So, without wasting time, let's start with XML-based aspect development.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.59.231