Newsletter

Spring AOP – Types Of Advice With Complete Explanation

Spring » on Jul 21, 2012 { 8 Comments } By Sivateja

We did see about Aspect already, Advice is the implementation of Aspect.  An Advice provides the code for implementation of the service. As an example consider logging  service, logging is an Aspect and Advice denotes the implementation of Log4j.

Types of Advices

  • Before Advice
  • After Advice
  • Throws Advice
  • Around Advice

Let us see one by one with explanation and example.

Before Advice

  • This advice contains implementation of the services which are need to be applied before business logic of method is going to execute.
  • During compilation time the services will not be applied to our logic, services will apply only at run time.
  • In order to create a Before advice, class should implement MethodBeforeAdvice interface.
  • MethodBeforeAdvice interface is given in org.sp-fw.aop.* package.
  • If we implement MethodBeforeAdvice interface then we need to override a method called before() method.
  • The services which are implemented in before() method are executed at before business logic.

Syntax Of  MethodBeforeAdvice  — before() Method

public class  beforeAdvice implements MethodBeforeAdvice
{
    public void before(Method m,Object args[], Object target)throws Exception
    {
                //My Before Logic...
    }
}
  • The first parameter Method m,  is a class at java.lang.reflect.* package, and this parameter is used to access the name of the business method, through getName(), i will show you this in the example.
  • Second parameter object[] is used to access the parameter values of the business method, the parameters of business method stored in object array and those parameters are given to before() method by the container in the form of objects only.
  • The third parameter is an object to whom this service will be going to apply, usually this will taken care by container, as a programmer we no need to care this 😉
  • The service implemented in before advice will be called from a proxy class which is generated by the container for a target class.

Confused…? what are these targets and proxy’s ? 🙂 let me explain..

Target Class

public class MyOwnBusinessClass
{
    public void businessMethod()
    {
             //Our business logic will goes here.....
    }
}

Before Services

public class  BeforeAdvice implements MethodBeforeAdvice
{
    public void before(Method m,Object args[], Object target)throws Exception
    {
                //My Before Logic...
    }
}

Proxy Class

public class MyOwnBusinessClass-Proxy (This class name will be given by container)
{
    public void businessMethod()
    {
             before()  // logic in before() method from Before Services will be executed here
             //Our business logic will goes here.....
    }
}

Explanation:

  • See MyOwnBusinessClass.java is our own java class file, just our business logic will goes here
  • BeforeAdvice.java contains the services, what we need to execute before our business logic
  • So proxy is the class generated by the container at run time, see at run time container is executing the logic of before() method from BeforeAdvice class, then our business method logic as a single class.
  • In fact we cannot see this proxy class physically 🙂 , container will creates in the memory and gives the output normally.

Hope you are clear so far.

 After Advice (After Returning Advice)

  • In the annotations After Advice and After Returning Advice are different, but here both are almost same
  • This is also same as Before Advice, But this advice contains services which are applied after completion of our business method logic
  • In order to create an after returning advice in spring, our class should implement an interface called AfterReturningAdvice, given in org.sp-fw.aop.* package and we need to override a method given by this interface called afterReturning()

Syntax Of AfterReturningAdvice — afterReturning() Method

public class  afterAdvice implements AfterReturningAdvice
{
    public void afterReturning(Object returnValue,Object args[], Object target)throws Exception
    {
                //My Before Logic...
    }
}

First parameter contains the return value of the business method or null, rest is same as Before Advice.

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

8 Responses to “Spring AOP – Types Of Advice With Complete Explanation”
  1. hi Mr . Sivateja ,
    this is Raju kapadne from pune .sir , you have given AOP advices concept its very nice , but here is one drawback of AOP advices is spring bean class become Spring FW dependent becoz its implements the SpringFW provided interface . so please post the topic of declarative Spring AOP concept with aop scheama and aop namespace please…..

  2. eswar says:

    Hi ,this is eswar from pune.aop is also done with aop tags in configuration XML file.please explain which one is better for programming.

  3. Jawahar says:

    If I understand correctly, the first line under Before Advice should be “This advice contains implementation of the ASPECTS which are need to be applied before business logic of method is going to execute” and not “This advice contains implementation of the SERVICES which are need to be applied before business logic of method is going to execute.

  4. Lakshmi says:

    Siva, Very interesting article…Easy to understand…Thank you.

  5. Aruna says:

    Hi siva,

    In the syntax of afterReturningAdvice(), Method m parameter is missing. please update it
    void afterReturning(Object returnValue,
    Method method,
    Object[] args,
    Object target) throws Exception

    This article is very easy to understand. Thank you

  6. mayilvahanan says:

    Which type of advices in spring used for auditing style operations and security checks?

    a) Before advice
    b) After advice
    c) After returning advice
    d) Around advice

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.