AOP (Aspect-oriented programming) is a complement of OOP (Object-oriented programming), which is defined as a programming technique. The important unit of modularity in AOP is an aspect; whereas in OOP, the units of modularity are classes and objects. Aspects provide the modularization of various concerns, such as transaction management, which crosscut multiple types and objects. In AOP, such concerns are termed as cross-cutting concerns. For example, security is a cross-cutting concern in an application, as EmployeeService, SalaryService, and other services of that application must implement the security functionality. Here we will see about AOP with Spring Framework.
- Replaces the EJB declarative services with new declarative enterprise services such as declarative transaction management.
- Allows you to implement custom aspects by using OOP with AOP.
- Allows AOP to be combined with Acegi security framework to provide declarative security service. Acegi security framework is a kind of Spring security framework that provides security solutions for Java EE-based Web applications.
The following list defines and explains the various key terms of the AOP module:
- Advice: Defines both what and when of a facet. We know that each aspect has a purpose in AOP, the purpose of an aspect is known as advice. In the real-world, advice defines what the job is and when it can be performed. The advice can be applied either before or after method invocation by using the @Aspect annotation.
- Joinpoint: Refers to a point where an aspect can be plugged in. It is the single location in the code where advice should be executed (i.e. method invocation). For specifying a joinpoint, you have to implement the org.springframework.aop. Pointcut interface and the information about the joinpoint can be accessed by the Method Invocation interface.
- Pointcut: Refers to many joinpoints that are grouped together to perform advice, which makes a pointcut. As we know that in Spring, a joinpoint is always a method invocation; therefore, we can say that a pointcut is a set of methods invocation.
- Aspect: Combines both advice and pointcuts. Therefore, an aspect consists of information such as what is the role of the aspect, and where and when to apply the aspect.
- Introduction: Adds methods and fields to an existing object. For using this concept, Spring has introduced some interfaces that must be implemented by the advised class.
- AOP proxy: Refers to an object created by the Spring AOP to implement the aspect. In Spring, there are two types of AOP proxies-JDK dynamic proxy and code generator library (CGLIB) proxy.
- Weaving: Refers to the process that links aspects with other application types or objects to create an advised object. This process can be performed at compile-time, load time, and runtime.
Types of Advices
The different types of advices are as follows:
- Around advice: Specifies whether to proceed to the joinpoint or to make a cross-cutting concern by returning its own return value or throwing an exception. The around advice is used in the context of a join point such as a method invocation.
- Before advice: Fires advice before the execution of a joinpoint.
- After throws advice: Fires advice when a method throws an exception. To handle this exception, you have to write the corresponding try and catch block in a class.
- After returning advice: Fires advice when a method invocation or joinpoint is completed. For example, if the return statement of a method is executed, it returns an appropriate value without throwing an exception.
- After finally advice: Fires advice regardless of whether a joinpoint exits with a normal or exceptional return.
Most of the AOP frameworks provide only around advice. However, in other instances, you might need to use other types of advices. For example, if you want to update a cache with a method that returns a result, you need to implement an after returning advice instead of an around advice.
Spring AOP Capabilities and Its Goals
Since Spring AOP is a fully OOP-based concept, the compilation process of a Spring application is the same as that of the simple Java applications. As a result, it can be used successfully in Java EE compatible application server and Web container.
The classes that represent pointcuts and different advice types are provided by Spring. The term advisor is used for an object that represents an aspect. This aspect has both advice and a pointcut that targets a particular joinpoint.
In Spring different types of methods, interceptors are used with advice. The org.springframework.aop package has defined all the advices. The org.aopalliance.aop.Advice tag interface must be implemented by every advice. Some of the common advice interfaces in Spring AOP are Methodinterceptor, Throws Advice, Before Advice, and After Returning Advice.
Generally, the features of Spring AOP are used with a Spring IoC container. AOP advice is defined by using simple bean definition syntax. Spring loc is capable of managing both advice and pointcuts. There are some situations where Spring AOP is very difficult to use. For example, when advice is very fine-grained, it is difficult to implement Spring AOP. In such a situation, Aspect annotation should be used.
Java EE 5 application server provides secondary services, which are heavyweight. These secondary services are managing transactions, implementing security, and logging errors of an application. Therefore, a heavyweight Java EE 5 application server should be run while accessing the business logic of an enterprise application. However, in the case of Spring application, only the AOP framework of the Spring framework (a lightweight container) should be used.