Advices Spring Tutorial Examples Screenshots

Advices Spring Tutorial Examples Screenshots


After knowing what is Spring cross cutting concern, joinpoint, pointcut and advice, let us explore some programs using these concepts in this Advices Spring Tutorial.

AOP is the main concept in Spring application development. Advice gives actual implementation code for an aspect. Spring provides advice execution at method level and is known as Method Aspect. AspectJ supports FieldAspect also as advices can be applied at field (variables) level also. Following gives different types of advices Spring supports.

Types of Advices

  1. Before Advice: Executes before a joinpoint (actual business method call).
  2. After Advice: Executes after a joinpoint
  3. Around Advice: Executes before and after joinpoint (surrounds).
  4. Throws Advice: Executes when a method exists by throwing an exception.

Of all the above "around advice" is the most used as it can change the flow of control also (imparting custom behavior). It can decide to execute the business method (joinpoint) or not (with proceed() method).

The interfaces support for the above advices.

Advice name Interface name Method to override
1 Before Advice MethodBeforeAdvice before()
2 After Advice AfterReturningAdvice afterReturning()
3 Around Advice MethodInterceptor invoke()
4 Throws Advice It is a marker interface Generally programmers write their own methods like afterThrowing() etc.

All the above interfaces are from org.springframework.aop package.

Let us see the difference of using without advice and using advice.

1. A normal bean program without advice – File Name: Student.java

Bean configuration file to inject values to the variables of the above bean

File Name: applicationContext.xml

Use the id student1 in client program with getBean() method to get an object of Student class.

Client program to run the above bean – Client1.java


 Advices Spring Tutorial
Screenshot in MyEclipse of running Client1.java of this Advices Spring Tutorial.

Learn the difference between using BeanFactory and ApplicaitonContext interfaces from the Introduction to Java Spring Framework.

Let us apply the 4 advises to this program one-by-one (instead all the four can be applied in one program also, but a for beginner it is difficult to understand).

Pass your comments on this Advices Spring Tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *