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:

public class Student
  String name;         				// just two variables
  String college;

  public void setName(String name)    		// just two set methods
  { = name;
  public void setCollege(String college)
  { = college;
  public void printName()            		// 3 print methods to print variables
    System.out.println("Student Name is " + name);
  public void printCollege()
    System.out.println("Student College is " + college);
  public void printThrowException()
    throw new IllegalArgumentException();

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 –

import org.springframework.context.ApplicationContext;

public class Client1
  public static void main(String[] args)
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

    Student std1 = (Student) ac.getBean("student1");

    }  			               // this is not executed as no exception is thrown by the code
    catch (Exception e) {  }

 Advices Spring Tutorial
Screenshot in MyEclipse of running 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 Comment

Your email address will not be published.