You can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Spring AOP – Types Of Advice With Complete Explanation

Spring » On Jul 21, 2012 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

  • 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

Before Services

Proxy Class

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

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






By [ Java/J2EE Developer & Web Designer ]
Founder of Java4s.com - Indian, lives in United States. You can contact me at sivateja [at] java4s.com
Follow me on


Comments

2 Responses to “Spring AOP – Types Of Advice With Complete Explanation”
  1. Raju Kapadne says:

    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.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
Sitemap :
spring Hibernate struts Json Ajax Log4j Log4j coreJava Servlets Web Services


Upcoming
  Web Services [ In Progress ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
© 2010 - 2014 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.