Javatpoint Logo
Javatpoint Logo

Spring AOP AspectJ Xml Configuration Example

Spring enables you to define the aspects, advices and pointcuts in xml file.

In the previous page, we have seen the aop examples using annotations. Now we are going to see same examples by the xml configuration file.

Let's see the xml elements that are used to define advice.

  1. aop:before It is applied before calling the actual business logic method.
  2. aop:after It is applied after calling the actual business logic method.
  3. aop:after-returning it is applied after calling the actual business logic method. It can be used to intercept the return value in advice.
  4. aop:around It is applied before and after calling the actual business logic method.
  5. aop:after-throwing It is applied if actual business logic method throws exception.

To understand the aop concepts, its advantage etc. visit here AOP Concepts Tutorial



1) aop:before Example

The AspectJ Before Advice is applied before the actual business logic method. You can perform any operation here such as conversion, authentication etc.

Create a class that contains actual business logic.

File: Operation.java

Now, create the aspect class that contains before advice.

File: TrackOperation.java

Now create the applicationContext.xml file that defines beans.

File: applicationContext.xml

Now, let's call the actual method.

File: Test.java

Output

As you can see, additional concern is printed before msg(), m() and k() method is invoked.


2) aop:after example

The AspectJ after advice is applied after calling the actual business logic methods. It can be used to maintain log, security, notification etc.

Here, We are assuming that Operation.java, TrackOperation.java and Test.java files are same as given in aop:before example.

Now create the applicationContext.xml file that defines beans.

File: applicationContext.xml

Output

You can see that additional concern is printed after calling msg(), m() and k() methods.


3) aop:after-returning example

By using after returning advice, we can get the result in the advice.

Create the class that contains business logic.

File: Operation.java

Create the aspect class that contains after returning advice.

File: TrackOperation.java
File: applicationContext.xml
File: Test.java

Now create the Test class that calls the actual methods.

Output

You can see that return value is printed two times, one is printed by TrackOperation class and second by Test class.


4) aop:around example

The AspectJ around advice is applied before and after calling the actual business logic methods.

Create a class that contains actual business logic.

File: Operation.java

Create the aspect class that contains around advice.

You need to pass the PreceedingJoinPoint reference in the advice method, so that we can proceed the request by calling the proceed() method.

File: TrackOperation.java
File: applicationContext.xml
File: Test.java

Now create the Test class that calls the actual methods.

Output

You can see that additional concern is printed before and after calling msg() and display methods.


5) aop:after-throwing example

By using after throwing advice, we can print the exception in the TrackOperation class. Let's see the example of AspectJ AfterThrowing advice.

Create the class that contains business logic.

File: Operation.java

Create the aspect class that contains after throwing advice.

Here, we need to pass the Throwable reference also, so that we can intercept the exception here.

File: TrackOperation.java
File: applicationContext.xml
File: Test.java

Now create the Test class that calls the actual methods.

Output








Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA