Writing First AspectJ Program in Spring

Spring AOP (Aspect-oriented programming) framework is used to modularize cross-cutting concerns in aspects. Put it simple, it’s just an interceptor to intercept some processes, for example, when a method is execute, Spring AOP can hijack the executing method, and add extra functionality before or after the method execution.

In Spring AOP, 4 type of advices are supported :

  • Before advice – Run before the method execution
  • After returning advice – Run after the method returns a result
  • After throwing advice – Run after the method throws an exception
  • Around advice – Run around the method execution, combine all three advices above.

1. Setting up the project structure:

Launch your preferred IDE (I use STS) and Create New Java Project. And then create the file structure as shown below…

Writing First AspectJ Program in Spring

You need to download all the JARs that are in User Libraries “aspectJ-lib” and add them to the project’s class path. You can find these JARs here: Spring, Commons-logging, aspectjrt, aspectjweaver.

2. Writing our Logging Aspect:

package com.dineshonjava.sdnext.aop.tutorial.aspectJ;

@Aspect
public class LoggingAspect
{
        public void loggingAdvice()
 {
    System.out.println("Advice run. Get method is called");
 }
}

3. Configuring Spring AOP and @AspectJ support:

<beans>
.....
  <bean class="com.dineshonjava.sdnext.aop.tutorial.aspectJ.LoggingAspect" name="loggingAspect"></bean>
.....
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
.....
<aop:config>
   <aop:aspect id="loggingAspect" ref="loggingAspect">
        .....
        <aop:after method="..." pointcut="..."></aop:after>
        .....
    </aop:aspect>
</aop:config>
....
</beans>
Spring supports the @AspectJ annotation style approach and the schema-based approach to implement custom aspects. These two approaches have been explained in detail in the following.
1. XML Schema based : Aspects are implemented using regular classes along with XML based configuration.

2. @AspectJ based : @AspectJ refers to a style of declaring aspects as regular Java classes annotated with Java 5 annotations.

Spring AOP Related Posts

  1. Spring AOP Interview Questions and Answers
  2. Spring AOP-Introduction to Aspect Oriented Programming
  3. @Aspect Annotation in Spring
  4. Advices in Spring AOP
  5. Spring AOP JoinPoints and Advice Arguments
  6. Spring AOP-Declaring pointcut Expressions with Examples
  7. Spring AOP XML configuration
  8. Spring AOP XML Schema based Example
  9. Spring AOP AspectJ @Before Annotation Advice Example
  10. Spring AOP Before Advice Example using XML Config
  11. Spring AOP AspectJ @After Annotation Advice Example
  12. Spring AOP After Advice Example using XML Config
  13. Spring AOP AspectJ @AfterReturning Annotation Advice Example
  14. Spring AOP After-Returning Advice Example using XML Config
  15. Spring AOP AspectJ @AfterThrowing Annotation Advice Example
  16. Spring AOP After Throwing Advice Example using XML Config
  17. Spring AOP AspectJ @Around Annotation Advice Example
  18. Spring AOP Around Advice Example using XML Config
  19. Spring AOP Proxies in Spring
  20. Spring AOP Transaction Management in Hibernate
  21. Spring Transaction Management
  22. Spring Declarative Transaction Management Example
  23. Spring AOP-Ordering of Aspects with Example