Archive for January, 2009

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();
}


 

Synchronizer Token Pattern in Struts


The main problem that this pattern is trying to solve is the double submissions of forms.
For example, look at this list of event that can lead to a double submission.

  • Clicking more than once on a submit control
  • Using Refresh button
  • Using the browser back button to resubmit form
  • Using Browser history feature and re-submit form.
  • Malicious submissions to the server

[more ...]