Publish Your Article Here

Spring AOP - AspectJ - @AfterReturning example

Last few pages talked about spring advices (before advice, after return advice, around advice and pointcut examples). In this page you will get an idea about how to integrate AspectJ annotations with Spring AOP framework. Using spring AOP and AspectJ, you can easily intercept methods.

AspectJ comes with below annotations:

1) @Before
2) @After
3) @AfterReturning
4) @AfterThrowing
5) @Around

In this page we will see an example for @AfterReturning annotation. @AfterReturning annotation intercepts method after calling it. You have seen similar behavior with @After annotation in the previous example, but @AfterReturning will intercept the return value as well.

pom.xml file gives all required dependencies: Add dependencies for spring-aop jar, aspectjrt jar and aspectjweaver jar file.

<project xmlns="" 

My business logic service class:

package com.java2novice.bean;

public class MyBusinessService {

        public String runMyBusinessLogic(){
                System.out.println("Running business logic...");
                return "Successfully executed my business logic";
        public void testThrowException() {
                throw new NullPointerException();

Here is the Aspect class example. The after() method will be executed after calling the method. Note here you have specified "returning" attribute as part of your annotation. Make sure that the annotation returning attribute value should be same as method input parameter.

package com.java2novice.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;

public class RunAfterExecution {

        pointcut="execution(* com.java2novice.bean.MyBusinessService.runMyBusinessLogic(..))", 
        public void runAfter(JoinPoint joinPoint, String returnValue) throws Throwable {
                System.out.println("Inside RunAfterExecution.afterReturning() method...");
                System.out.println("inserted after : " + joinPoint.getSignature().getName());
                System.out.println("Method returned value is : " + returnValue);

Here is the xml based configuration file. By adding "<aop:aspectj-autoproxy />" tag, you can enable AspectJ with in your application. Add bean definitions for your normal bean and your Aspect.

<beans xmlns=""
    <aop:aspectj-autoproxy />
    <bean id="busService" class="com.java2novice.bean.MyBusinessService" />
        <bean id="beforeAspectBean" class="com.java2novice.aop.RunAfterExecution" />

Here is the final demo class:

package com.java2novice.test;

import org.springframework.context.ConfigurableApplicationContext;

import com.java2novice.bean.MyBusinessService;

public class SpringDemo {

        public static void main(String a[]){

                String confFile = "applicationContext.xml";
                ConfigurableApplicationContext context = 
                                                                new ClassPathXmlApplicationContext(confFile);
                MyBusinessService busServ = (MyBusinessService) context.getBean("busService");

Next page shows an example for spring aop - AspectJ - @AfterThrowing example.

Running business logic...
Inside RunAfterExecution.afterReturning() method...
inserted after : runMyBusinessLogic
Method returned value is : Successfully executed business logic
<< Previous Program | Next Program >>

Spring framework examples

  1. Spring 3 hello world example
  2. Spring bean java based configuration using @Configuration and @Bean
  3. How to get spring application context object reference?
  4. How to load multiple spring bean configuration files?
  5. Spring java based configuration @Import example
  6. Spring Dependency Injection and Types
  7. Spring Dependency Injection via setter method
  8. Spring Dependency Injection via Constructor
  9. Constructor overloading issue with spring constructor injection
  10. Constructor vs Setter dependency Injection in Spring
  11. How to inject value into spring bean instance variables?
  12. Spring bean tag properties
  13. Differen types of spring bean scopes
  14. How to inject inner bean in spring?
  15. Set spring bean scope using annotation
  16. How to invoke spring bean init and destroy methods?
  17. Spring bean initialization callback
  18. Spring bean destruction callback
  19. Configure default initialization and destroy method in all spring beans
  20. Spring bean init and destroy methods using annotations
  21. Spring Bean Post Processors
  22. How to read property file in spring using xml based configuration file?
  23. How to read property file in spring 3.0 using java based configuration?
  24. How to inject date into spring bean property?
  25. How to inject date into spring bean with CustomDateEditor?
  26. Spring bean inheritance configuration
  27. Spring dependency checking with @Required annotation
  28. How to define a custom Required-style annotation for dependency checking?
  29. How to inject List into spring bean?
  30. How to inject Set into spring bean?
  31. How to inject Map into spring bean?
  32. How to enable auto component scanning in spring?
  33. Difference between @Component, @Service, @Repository and @Controller
  34. How to filter components in auto scanning?
  35. Spring expression language basic example using xml based configuration.
  36. Spring expression language basic example using annotations.
  37. Bean reference example using spring expression language
  38. Spring expression language operators example
  39. Spring expression language ternary operator example
  40. How to use regular expressions with spring expression language?
  41. How to use collections with spring expression language?
  42. Spring bean auto-wiring modes
  43. Spring auto-wiring mode byName
  44. Spring auto-wiring mode byType
  45. Spring auto-wiring mode constructor
  46. Spring auto-wiring using @Autowired annotation example
  47. Spring auto-wiring using @Qualifier annotation example
  48. Spring log4j configuration
  49. How to schedule jobs using @Scheduled annotation in spring?
  50. Send E-mail using spring 3
  51. Send E-mail with attachment using spring 3
  52. Simple spring JDBC example
  53. Spring JDBC example with JdbcTemplate
  54. Spring JDBC example with JdbcDaoSupport
  55. Spring JDBC query example using JdbcDaoSupport
  56. How to query single column using spring JdbcTemplate?
  57. Spring JDBC batch updates using JdbcTemplate?
  58. Spring AOP Advices - Before advice example - xml based configuration
  59. Spring AOP Advices - After returning advice example - xml based configuration
  60. Spring AOP Advices - After throwing advice example - xml based configuration
  61. Spring AOP Advices - Around advice example - xml based configuration
  62. Spring AOP Advice - Pointcuts – Name match example
  63. Spring AOP Advice - Pointcuts – Regular expression example
  64. Spring AOP - AspectJ - @Before example
  65. Spring AOP - AspectJ - @After example
  66. Spring AOP - AspectJ - @AfterReturning example
  67. Spring AOP - AspectJ - @AfterThrowing example
  68. Spring AOP - AspectJ - @Around example
Knowledge Centre
What is System.out in Java
In System.out, out is an instance of PrintStream. It is a static member variable in System class. This is called standard output stream, connected to console.
Famous Quotations
Education is what remains after one has forgotten what one has learned in school.
-- Albert Einstein

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2024 by Nataraja Gootooru. All Rights Reserved.