In this Java Spring Training session, you will learn Spring AOP – Aspect Oriented Programming Topics covered in this session are:
For more information, visit this link:
• Auto-wiring
• Annotations based configuration
• Java based configuration
https://www.mindsmapped.com/courses/software-development/spring-fundamentals-learn-spring-framework-and-spring-boot/
2. Page 1Classification: Restricted
Review
• Spring framework
• Inversion of Control
• Dependency Injection – Two types
• Defining beans using XML
• Inheriting beans
• Auto-wiring
• Annotations based configuration
• Java based configuration
5. Page 4Classification: Restricted
What is Aspect Oriented Programming?
• Oriented Programming entails breaking down program logic into distinct
parts called so-called concerns.
• The functions that span multiple points of an application are called cross-
cutting concerns and these cross-cutting concerns are conceptually
separate from the application's business logic.
• Examples of aspects like
• logging
• auditing
• declarative transactions
• Security
• caching
6. Page 5Classification: Restricted
AOP does not replace OOP but makes it better
• The key unit of modularity in OOP is the class, whereas in AOP the unit of
modularity is the aspect.
• Dependency Injection helps you decouple your application objects from
each other and AOP helps you decouple cross-cutting concerns from the
objects that they affect.
8. Page 7Classification: Restricted
AOP Terminologies
Terms Description
Aspect A module which has a set of APIs providing cross-cutting requirements.
For example, a logging module would be called AOP aspect for logging.
An application can have any number of aspects depending on the
requirement.
Join point This represents a point 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.
Advice This is the actual action to be taken either before or after the method
execution. This is actual piece of code that is invoked during program
execution by Spring AOP framework.
Pointcut This is a set of one or more joinpoints where an advice should be
executed. You can specify pointcuts using expressions or patterns as we
will see in our AOP examples.
Weaving 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. Spring AOP performs weaving at runtime.
11. Page 10Classification: Restricted
Spring AspectJ AOP
Spring AspectJ AOP implementation provides many annotations:
• @Aspect declares the class as aspect.
• @Pointcut declares the pointcut expression.
The annotations used to create advices are given below:
• @Before declares the before advice. It is applied before calling the actual method.
• @After declares the after advice. It is applied after calling the actual method and
before returning result.
• @AfterReturning declares the after returning advice. It is applied after calling the
actual method and before returning result. But you can get the result value in the
advice.
• @Around declares the around advice. It is applied before and after calling the
actual method.
• @AfterThrowing declares the throws advice. It is applied if actual method throws
exception.
12. Page 11Classification: Restricted
• Before Advice: These advices runs before the execution of join point methods. We can use
@Before annotation to mark an advice type as Before advice.
• After (finally) Advice: An advice that gets executed after the join point method finishes
executing, whether normally or by throwing an exception. We can create after advice using
@After annotation.
• After Returning Advice: Sometimes we want advice methods to execute only if the join point
method executes normally. We can use @AfterReturning annotation to mark a method as
after returning advice.
• After Throwing Advice: This advice gets executed only when join point method throws
exception, we can use it to rollback the transaction declaratively. We use @AfterThrowing
annotation for this type of advice.
• Around Advice: This is the most important and powerful advice. This advice surrounds the join
point method and we can also choose whether to execute the join point method or not. We
can write advice code that gets executed before and after the execution of the join point
method. It is the responsibility of around advice to invoke the join point method and return
values if the method is returning something. We use @Around annotation to create around
advice methods.
14. Page 13Classification: Restricted
Pointcut examples
Expression Example Meaning
@Pointcut("execution(public * *(..))") applied on all the public methods.
@Pointcut("execution(public Operation.*(..))") applied on all the public methods
of Operation class.
@Pointcut("execution(* Operation.*(..))") applied on all the methods of
Operation class.
@Pointcut("execution(public Employee.set*(..))") applied on all the public setter
methods of Employee class.
@Pointcut("execution(int Operation.*(..))") applied on all the methods of
Operation class that returns int
value.
15. Page 14Classification: Restricted
AOP Example
// Operation.java
public class Operation{
public void msg(){System.out.println("msg method invoked");}
public int m(){System.out.println("m method invoked");return 2;}
public int k(){System.out.println("k method invoked");return 3;}
}
16. Page 15Classification: Restricted
AOP Example
//The Aspect Class that contains Before advice
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class TrackOperation{
@Pointcut("execution(* Operation.*(..))")
public void ptcut(){}//pointcut name
@Before(“ptcut()")//applying pointcut on before advice
public void myadvice(JoinPoint jp)//it is advice (before advice)
{
System.out.println("additional concern");
//System.out.println("Method Signature: " + jp.getSignature());
}
}
18. Page 17Classification: Restricted
More examples: @After
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class TrackOperation{
@Pointcut("execution(* Operation.*(..))")
public void k(){}//pointcut name
@After("k()")//applying pointcut on after advice
public void myadvice(JoinPoint jp)//it is advice (after advice)
{
System.out.println("additional concern");
//System.out.println("Method Signature: " + jp.getSignature());
}
}
19. Page 18Classification: Restricted
More example: @AfterReturning
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class TrackOperation{
@AfterReturning(pointcut = "execution(* Operation.*(..))", returning= "result")
public void myadvice(JoinPoint jp,Object result){ //ADVICE
System.out.println("additional concern");
System.out.println("Method Signature: " + jp.getSignature());
System.out.println("Result in advice: "+result);
System.out.println("end of after returning advice...");
}
}
20. Page 19Classification: Restricted
More example: @Around
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class TrackOperation
{
@Pointcut("execution(* Operation.*(..))")
public void abcPointcut(){}
@Around("abcPointcut()")
public Object myadvice(ProceedingJoinPoint pjp) throws Throwable
{
System.out.println("Additional Concern Before calling actual method");
Object obj=pjp.proceed();
System.out.println("Additional Concern After calling actual method");
return obj;
}
}
//Note: You need to pass the PreceedingJoinPoint reference in the advice method, so that we can proceed the
request by calling the proceed() method.
21. Page 20Classification: Restricted
More examples: @AfterThrowing
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class TrackOperation{
@AfterThrowing(
pointcut = "execution(* Operation.*(..))",
throwing= "error")
public void myadvice(JoinPoint jp,Throwable error)//it is advice
{
System.out.println("additional concern");
System.out.println("Method Signature: " + jp.getSignature());
System.out.println("Exception is: "+error);
System.out.println("end of after throwing advice...");
}
}