• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Spring AOP and Java Refelction

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,



I have one method which is called using invoke() of the java.lang.reflect.Method class. How to intercept that method using Spring AOP ?

Thanks,
Kasi
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your annottated class is built to an interface, Spring AOP internally creates a Java proxy at runtime, and replaces the bean with the proxy bean

So, let's say you had a bean declared like this



this can be considered to be functionally equivalent to



If the bean is annottated with an aspect, you can consider that during initialization time, Spring creates this class



and the beans will be created like this



Please note the above code is for explanation only. Internally what happens is differrent.

So, here's the pitfall.. The proxy class implements only the methods in the interface. So, if you are using reflection on the proxy, you will not get all the methods. You will get only the interface methods. What you need to do is for the non interface methods, you need to get the underlying target object. I haven't done this myself before but I think this will work. You might have to try it. Every AOP proxy implements the org.springframework.aop.framework.Advised interface. This interface has a method to get the targetSource, and you can get the target object.

I think this will work. Maybe there is a better way of doing it.
 
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic