Spring AOP Introduction


               This tutorial explains the Spring AOP Step by step.

Aspect Oriented Programming(AOP) is another way of program structure like other programming models (Object oriented, Procedural …).  It gives you aspects. Aspect is one of the key components of Spring AOP framework.  It is necessary to know that the Spring IOC container does not depend on AOP, meaning you don't need to use AOP if you don't want to.
 IOC container can work without the AOP framework.
AOP helps us to modularize the concerns in our application. So what kind of concerns they are? They can be called as ‘Cross-cutting’ concerns which cut across the different modules/layers of your application. The best examples are ‘Transaction Management’, ‘Logging’.
In an application we use logging, transaction management across the modules/layers. Logging cuts model, view, control and whatever layers you have in your application. Consider you need to change your logging logic [or] transaction management logic. In that case you have to revisit entire application and make the changes across the layers. Isn’t this a problem?

This is where Spring AOP gets more importance. AOP allows you to separate these cross-cutting concerns into separate modules called Aspects. This separates cross-cutting concerns from your business logic. So that you do not need to revisit entire application/need to make very few modifications if your application requires any change.

Spring AOP 

Java Spring
Spring AOP framework is implemented in pure Java. It is a proxy based AOP framework. This framework currently supports only method execution join points. It does not support field level interception. Field interception is possible in AspectJ which is another AOP api available. Spring AOP framework provides a close integration between AOP implementation and Spring IoC to help solve common problems in enterprise applications and it does not give you complete AOP capabilities.
So if you want to use complete features of Aspect Oriented Programming, then AspectJ is the best choice. This doesn’t mean that Spring AOP is a competitor of AspectJ. These two frameworks can be integerated in a single application to make use of both the features.

Spring AOP module provides us the control to intercept the application logic. For example, when a method is executed, you can add extra functionality before or after the method execution.

AOP concepts 

  •          Aspect
  •          Join point
  •          Advice
  •          Pointcut
  •          Introduction 
  •         Target object 
  •          AOP proxy 
  •          Weaving 

Aspect is a separate module in the application with the logic of cross-cutting concerns. For example, a logging module would be called AOP aspect for logging. An application can have any number of aspects depending on the requirement. In spring AOP Aspects are implemented in pure java class form.  It is an extra abstraction mechanism on top of existing modularization mechanisms such as methods, classes…  Aspects are used to tackle the problems of spreading of code belonging to a certain concern over different components. As we said earlier, it separates logging logic from your application so that logging logic will not spread across the layers.
Another best example is Transaction management in EJB. The container takes care of it. It separates the transaction management logic from core business logic of the application. That is dealt within the container without requiring the developer to know the corresponding implementation. 

Join point 

Joint point is a point during the execution of the program in your application where you can plug-in AOP aspect. You can also say it is the actual place in the application where an action will be taken using Spring AOP framework. These points could be method execution, handling the exception. In Spring AOP a joint point always represents the method execution.
At the join point, an Aspect is injected, nothing but the implementation of Aspect I mean Advice will be injected. So the aspects can be injected at below points in your program execution,
-Beforebusiness logic of the method starts 
-Afterbusiness logic of the method got completed

-If business logic throws anexceptionat run time.


An advice is the actual action to be taken at the joint point [either before or after the method execution]. Or we can say an advice is the implementation of the aspect.  This is actual piece of code that is invoked during program execution by Spring AOP framework. Spring AOP provides different type of advices. They are,


–AfterReturning Advice

–After throwing Advice

–After (finally) Advice

–Around Advice.
We will see those in details in following sections.


                Pointcut defines for what methods what services need to be executed. In other words a pointcut defines what advices are required at what join points. In fact all business methods of a class don’t require all services. So a pointcut informs to the IOC container that what business methods of a class need what type of services. Due to the proxy-based nature of spring’s AOP framework, protected/private methods are by definition not intercepted. In simple, any given pointcut will be matched against public methods only.


Introduction helps us to introduce new interfaces (and a corresponding implementation) to any proxied object. 

Target object

Target object is the object being advised by one or more aspects. Also referred to as the advised object


Weaving is the process of linking aspects with other application types or objects to create an advised object. This can be done at compile time, load time, or at runtime.

AOP proxy

AOP Proxy is an object created by the AOP framework in order to implement the aspect. Spring AOP framework uses two different proxying mechanisms. One is JDK dynamic proxy and another one is the CGLIB proxy. JDK Dynamic proxy will be used when you intercept a class which is an implementation on a Java Interface. CGLIB proxy will be used when you want to intercept a class which does not implement any Java Interface.

Advices in detail:

To know more about the different type of Advices with an example, go to this post.

No comments:
Write comments

Popular Posts