AspectJ Before Advice

Step 1 – Create new maven-based java project

You can create normal java project and add jar file to your project build path. But it’s faster using maven as maven is integrated with Eclipse IDE; so you don’t need to manually install it. You need spring-context and  aspectjweaver dependencies.

Project Object Model (POM.XML)

Step 2 Create Bean class

Select your src folder of your project, copy the below code and press CTRL + V to paste the code. Eclipse IDE will automatically create new class with correct package.

Imagine this class is like a servlet lifecycle that includes 3 methods init, service & destroy. But the init & destroy methods are not implemented here.

Step 3 AspectJ Class

Step 4 Create XML Bean Configuration file

In this example, I use Spring Tool Suite (STS) wizard to generate the file. You can just create an empty xml file named aspectBean.xml under package and paste the content below.

Step 5 Create test class

Compile & Run the application

Go to Project -> Clean to clean & build the project. Eclipse will download & add maven dependencies to your project build path; then compile your classes.

Right click to the test class; select Run As -> Java Application