Spring AOP

Posted By :Harshit Verma |30th July 2018

AOP framework is one of the key feature served by the Spring framework.The Ioc container of the spring is not depend on the AOP. It includes the following points

-> It is a replacement of the EJB.

-> The AOP allows user to implement custom aspects.

Some AOP concepts and terminologies are as follows :-
1). Aspect
2). Advice
3). Pointcut
4). Target object
5). AOP proxy
6). Weaving

Aspect:
       An aspect, it is a class which implements enterprise application concerns which cut across multiple classes, include as transaction management.

Join Point: 
           A join point is the  point in the application like as method execution, exception handling etc.


Advice:
       Advices are actions which taken for a particular join point.

Pointcut:
           Pointcut are that which is matched with join points which determine whether advice needs to be executed or not.

Target Object:
              They are the object on  advices are applied.

AOP proxy:
           Spring AOP uses JDK dynamic proxy to create  Proxy classes and advice invocations, and are called AOP proxy classes.

Weaving: 
        The process of linking aspects with other objects that create the advised proxy objects. 


Types of advice:-

1). Before advice
2). After returning advice
3). After throwing advice
4). After (finally) advice
5). Around advice

Before Advice:
               These advices runs just before the join point methods.

After (finally) Advice:
                     An advice which executed after the join point method finishes the  executing.

After Returning Advice: 
                     If we want advice methods to execute only if the join point method executes normally.

After Throwing Advice:
                      This will  gets executed only when join point method throws exception.

Around Advice:
               This advice surrounds the join point method which  can also choose whether to execute the join point method or may not.

Example of Spring AOP

@Aspect
public class EmployeeAspectMethod {

	@Before("execution(public String getName())")
	public void getNameAdvice1(){
		System.out.println(" Advice on getName()");
	}
	
	@After("execution(* com.spring.service.*.get*())")
	public void getAllAdvice(){
		System.out.println("Service method called");
	}
}


@Aspect
public class EmployeeAfterAspect2 {

	@After("args(name)")
	public void logStringArguments(String name){
		System.out.println("Running After Advice);
	}
	
	@AfterThrowing("within(com.spring.model.Employee)")
	public void logExceptions(JoinPoint joinPoint){
		System.out.println("Exception thrown in Employee Method=");
	}
	
	@AfterReturning(pointcut="execution(* getName())", returning="returnString")
	public void getNameReturningAdvice(String returnString){
		System.out.println("getNameReturningAdvice executed. Returned String="+returnString);
	}
	
}

About Author

Harshit Verma

Harshit is a bright Web Developer with expertise in Java and Spring framework and ORM tools Hibernate.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us