Listening Has Always Been Our Strongest Tool!

Just Give Us a Try, we respond to all messages within 1 business day. Really!


How can we reach out to you?






NDA From The Start
We keep your idea safe. We're always willing to sign a Non-Disclosure Agreement with you and fully protect your intellectual property.

captcha

Other Ways To Meet Us :info@zealousys.com
Our Blog
Dec 19
2013

Spring AOP integration

By zealousys

Spring AOP (Aspect-oriented programming) framework is used to modularize cross-cutting concerns in aspects. Simply putting it, it is just an interceptor to intercept some processes. For example: when a method is executed, Spring AOP can hijack the executing method, and add extra functionality before or after the method execution.

In Spring AOP, 4 types of advices are supported:

  • Before advice – Run before the method execution
  • After returning advice – Run after the method returns a result
  • After throwing advice – Run after the method throws an exception
  • Around advice– Run around the method execution, combine all three advices above.

Following is a simple example that shows how Spring AOP advice works.

Create a simple customer service class with few print methods for demonstration later.

1

File : Spring-Customer.xml – A bean configuration file

2

3
Run it

4

Spring AOP Advices

Now, attach Spring AOP advices to above customer service.

1. Before advice
It will execute before the method execution. Create a class which implements MethodBeforeAdvice interface.

5

In bean configuration file (Spring-Customer.xml), create a bean for BeforeMethod class, and a new proxy object named ‘customerServiceProxy‘.

‘target’ – Define which bean you want to hijack.
‘interceptorNames’ – Define which class (advice) you want to apply on this proxy /target object.

6

Note
To use Spring proxy, you need to add CGLIB2 library. Add below in Maven pom.xml file.

Extra

Run it again, now you get the new customerServiceProxybean instead of the original customerService bean.

7

8

It will run the BeforeMethod’s before() method, before every customerService’s methods are executed.

2. After returning advice
It will execute after the method returns a result. After that, create a class that implements AfterReturningAdvice interface.

9

Bean configuration file

10

Run it again

11

It will run the AfterMethod’s afterReturning() method, after every customerService’s methods that returned result.

3. After throwing advice
It will execute after the method throws an exception. Create a class which implements ThrowsAdvice interface, and create an afterThrowing method to the IllegalArgumentException exception.

12

Bean configuration file

13

Run it

14

It will run the ThrowException’s afterThrowing() method, if customerService’s methods throw an exception.

4. Around advice
It combines all three advices above, and executes during method execution. Create a class which implements MethodInterceptor interface. You have to call the “methodInvocation.proceed();” to proceed on the original method execution, else the original method will not execute.

15

Bean Configuration file

16

Run it again,

17

In Conclusion every time a developer uses around method advice but we should identify our requirement and base on that we should use particular advice of Sping AOP.

View My Stats