Here are the steps to run basic AOP samples that will explain the AfterAdvice, BeforeAdvice, AroundAdvice, and ExceptionAdvice concepts.
Create a Spring Maven project in STS and add the BookService
class:
Create AOPBeforeMethod.java
and attach BeforeAdvice to BookService.java
as configured in the Spring-Book.xml
:
From the config
file, you can understand that we are creating a bean for the BeforeMethod class and proxy bean for BookService. The target
tag specifies the bean that needs to be applied with the Aspect. The interceptorNames
denotes the Aspect class that will be attached to the proxy bean:
Add the CGLIB2
library in the Project Object Model (POM) file:
Create RunAOP.java
and run it as a Java application and you can see the BeforeAdvice code is executed before the actual bean method is invoked:
Add another AfterAdvice to the project:
Modify the configurations as shown in the following:
Run the Java program to see After Advice in action: