Archive for the 'Spring' Category

How to obtain a target object from an AOP proxy in Spring


Have you ever been in a situation or you need to access the target object, proxied by Spring AOP or CGLIB ?
Yes?

Check out this obscure Interface that I’ve found in the Spring API:
org.springframework.aop.framework.Advised

Any AOP proxy obtained from Spring can be cast to this interface to allow manipulation of its AOP advice. With this in mind, you can track down the target object, and get the information you need.

Here’s an example:

Object myBean = yourSpringContext.getBean(beanName);
if(myBean instanceof Advised){
      Advised advised = (Advised) actionBean;
      // Get the target object: The proxied object
      Object myObject = advised.getTargetSource().getTarget();
}


 

Defining Advice


Advice will be executed before, after and even around a method that have been selected by a pointcut. In this discussion, I will show you how to define Advice in various

Again, for the purpose of my post, consider that I use AOP annotations.

Advice Type

Advice comes in many flavours. I will explain in detail and demonstrate the differences that exist between them. Here’s the list that Spring AOP support.

  • Before
  • After
  • After returning
  • After throwing
  • Around

[more ...]


 

Defining Pointcuts


Pointcut nomenclature

Like I said in my post about Core AOP Concepts, Pointcut represent:

An expression that select one or more joint points. In other words, pointcut select joint point that will be advised by an aspect and when the application reaches a join point, advice on that join point may run.

Now, I’ll explain how pointcut actually works and how we can define it.
For the purpose of my post, consider that I use AOP annotations.

Important

Protected and private methods are not intercepted; pointcut will be matched against public methods only!

Pointcut have two important parts that we’ll look in the next points.
[more ...]