Aspect Oriented Programming (AOP) using XML

This tutorial will help us to get complete understanding on Aspect Oriented Programming(AOP) using xml

Application can have multiple aspects which is nothing but a logger, security, custom aspect classes which will be having functions to run at the time of function exception, before method execution, after method execution and method throws an exception for every or selected class depending on expression.

every aspect will have multiple Pointcuts. Pointcuts are the one which will be having an expression for when to execute aspects.

Below are the expressions for where to apply pointcuts in the application:

Target Specific Package: 
expression=”execution(* com.example.demo.*.*(..))

Target Specific Class: 
expression=”execution(* com.example.demo.Person.*(..))

Target Specific Function: 
expression=”execution(*com.example.demo.Person.printThrowException(..))

Note: As shown in above example, same bean class can have multiple aspects and we can have multiple aspects of same bean class and one aspect can have multiple pointcuts.

Advice can be execute at below point of time.

  1. At the time of function exception
  2. Before method execution
  3. After method execution
  4. Method throws an exception

We will be using Spring Tool Suit to create spring Java application which is nothing but an extension of Eclipse editor as part of this blog using with bean.xml.

To download and setup Spring Tool Kit please click on this link.

Follow below steps to create our very first Spring application project.

1. Open Spring Tool kit we installed here.

2. Click on File in top navigation menu and than click on new. There will be an option Spring Starter Project start appearing once we click on create new option.

Below window will start appearing.

Provide below highlighted value for Group and Artifact for the project and click on Next.

3. In the below screen select open Web option and select Spring Web. Click on Finish will create Spring project.

4. Create Person.java class as mentioned in below screenshot with two methods getName() and printThrowException() methods as shown below.

5. Below dependency also require to get added as part of pom.xml

6. Create one more class customAction.java and place below code inside it.

As mentioned earlier, all above functions will run in below sequence:

  1. beforeAdvice() function will get execute just before execution of any function.
  2. afterAdvice() function will get execute just after execution of any function.
  3. afterRetrnAdvice() function will get execute just after function return its value.
  4. afterThrowAdvice() function will get execute just after exception gets throw.

7. Create bean.xml under below hierarchy src/main/resources.

8. Place below content inside created bean.xml in above step.

At line number 12, bean instance created and passed its reference to aop:aspect at line number 15 to create an aspect.

At line number 19, I created a pointcut with a unique id and an expression for when and where to run these aspects.

execution(* com.example.demo.*.*(..)) → It will run for all classes define inside com.example.demo package.

We can have n number of point cut for any aspect.

Line number 21, 23, 25 and 28, we have defined which function need to execute at the time of pointcut.

9. In SpringannotationApplication1.java class place below line of code.

10. Run Springannotation1Applicatio.java class main method as java application and below output will get appear:

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan