Spring Aspect Oriented Programming (AOP) using annotation

Below is an example of Spring Aspect Oriented Programming using annotation.

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:

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

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

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.

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.

@Aspect is to declare CustomAction class as an Aspect.

@Pointcut(“execution(* com.example.demo.*.*(..))”) is to define pointcut with an expression for where to execute. We can have multiple point cuts in the same class.

@Before annotation advice runs before the execution of join point methods.

@After annotation advice runs after the execution of join point methods.

@AfterReturnig annotation advice runs just after join point method return its value.

@AfterThrowing annotation is to execute just after join point method throws an exception.

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

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

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