File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes pointcut for method in parent abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "pointcut for method in parent abstract class" Watch "pointcut for method in parent abstract class" New topic
Author

pointcut for method in parent abstract class

Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
Hi I have a scenario where my method to be intercepted is in parent class that is abstract but its not overwridden in my pointcut class. following is my setting



<aop:config>
<aop:pointcut id="servicePointcut" expression="execution(* com.me.xwe.reporting.transaction.TransactionReportGenerator.getReport(..))" />

<aop:aspect id="loggingAspect" ref="methodLogger">
<aop:before method="logMethodEntry" pointcut-ref="servicePointcut" />
<aop:after-returning method="logMethodExit"
returning="result" pointcut-ref="servicePointcut" />
</aop:aspect>
</aop:config>



now the problem is this works only if i override the method in TransactionReportGenerator class or i replace execution to com.me.xwe.ParetnReportGenerator(parent class), but if the implementation is in parent class then the proxy (CGILIB) is created but the methods from MethodLogger are not invoked , any pointers to make this work without overridding the method in child class?

-Praful


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

See if adding a "+" works for you

"execution(* com.me.xwe.reporting.transaction.TransactionReportGenerator.getReport(..))"

becomes

"execution(* com.me.xwe.reporting.transaction.TransactionReportGenerator+.getReport(..))

The "+" is about inheritance either extending a class or implementing the interface

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
did not work.. more observations if i add * to end of method name then a proxy is created "Cglib2AopProxy" but the methods from aspect are not invoked



but if i add + as you suggested or normal class.method(..) then the proxy is not at all created..

Thanks for your reply though, any more pointers?

-Praful
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

So thinking out loud here.

Spring AOP by default can only advice methods on your interfaces that your Class implements as long as that class is defined as a Spring Bean.

If you include, CGLIB in your classpath, then Spring can also create proxies for you on your classes that are defined as Spring Beans, in which it extends your Spring Bean class. So in this scenario, in theory if I have a Class that extends a Parent Abstract class, then that Class has all the methods of the parent class, and if that class is a Spring bean, then the CGLIB proxy should extend your class which extends the parent class and in theory should be advisable through your abstract.

But, by the sounds of it, it isn't quite working that way.

Maybe the abstract class has to be defined as a Spring Bean (I doubt it, but hey it is worth a shot)

<bean id="theAbstractClass" class="com.mycompany.something.TheAbstractClass" abstract="true"/>

<bean id="theSubClass" class="com.mycompany.something.TheSubClass" parent="theAbstractClass"/>

Maybe with that defined the pointcut will work, or maybe not. ;)

Mark
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
extremely logical thought Mark, but did not work, pasting my xml below, just incase I am missing something

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

I decided to do a little sample and it all worked perfectly without any tricks, so I am not sure why it doesn't work on your code. Here is what I was using. Now I am also using Spring 3.0 so that might be different.


My abstract class


The extended class



Here is my Aspect class



And here is my main application code getting the OrderServiceImpl and calling the findOrder method which is only implemented in my abstract class. I also added CGLIB to my classpath, version 2.2



And here is what the result was when running the application.



So the aspect ran. Which doesn't help you out much, but there must be something else occuring in your app that isn't posted here. It just works with CGLIB creating proxies and AOP.

Mark

Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
Thanks much for your efforts Mark, Appreciate it, yes I am using Spring 2.5 and not 3.0, can you check if it works you modify



To

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

No it did not work with that.

So I changed it to

@After("execution(* org.springsource.service.AbstractOrderService.findOrder(..))")

And that worked.

And so did

@After("execution(* org.springsource.service.AbstractOrderService+.findOrder(..))")

Mark
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
yeha, that wroks for me too, so looks like the reference class/interface should always have the method implemented in itself and not in parent class.

anyways, will leave with this restriction for now, thanks again for your help.

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

"should always"

That is too strict word.

No you shouldn't always.

You can put it in the abstract class, but, in your pointcut expression, you have to have it correct so that it picks up that method in the parent class.

Mark
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
yeha i mean if your pointcut is specific to the class and not to package or wildcard level then that class should have the method,not very sure , but what i think is spring first makes list of all the defined aspects and the pointcuts and then before making object of every bean,it tries to resolve if any of the joinpoint (methods in spring) corrosponds to any of pointcut.

so if pointcut is specific to a class, in your case org.springsource.service.OrderServiceImpl.findOrder then before loading a bean of OrderServiceImpl, spring will try to find existance of findOrder method in static class file not object and it wont find it.

Just wild logical thinking might be completely worng..

-Praful
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Praful Thakare wrote:yeha i mean if your pointcut is specific to the class and not to package or wildcard level then that class should have the method,not very sure , but what i think is spring first makes list of all the defined aspects and the pointcuts and then before making object of every bean,it tries to resolve if any of the joinpoint (methods in spring) corrosponds to any of pointcut.

so if pointcut is specific to a class, in your case org.springsource.service.OrderServiceImpl.findOrder then before loading a bean of OrderServiceImpl, spring will try to find existance of findOrder method in static class file not object and it wont find it.

Just wild logical thinking might be completely worng..

-Praful


Yeah these DynamicProxies are created by a BeanPostProcessor which doesn't get triggered till after instance are created/dependencies injected/and properties set.

Mark
 
Don't get me started about those stupid light bulbs.
 
subject: pointcut for method in parent abstract class