Aspect Oriented Programming Using Annotation
Below is an example of 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.
- At the time of function exception
- Before method execution
- After method execution
- 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.
- Open Spring Tool kit we installed here.
- 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, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.