PDA

View Full Version : Intercepting static methods?



dune
Mar 17th, 2005, 04:41 PM
Is it possible to apply an advice (MethodInterceptor) to a static method? Specifically, I have the following applicationContext.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="testTarget" class="foo.Test"/>
<bean id="testService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref local="testTarget"/>
</property>
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="interceptorNames">
<list>
<value>testAdvisor</value>
</list>
</property>
</bean>
<bean id="testAdvisor" class="org.springframework.aop.support.RegexpMethodPointc utAdvisor">
<property name="advice">
<ref local="testAdvice"/>
</property>
<property name="pattern">
<value>.*</value>
</property>
</bean>
<bean id="testAdvice" class="foo.TestAdvice"/>
</beans>

foo.Test has one static and one non-static method. The advice is applied on the non-static method when it's run. Any thoughts?

Thanks,
Dave

Alef Arendsen
Mar 17th, 2005, 05:02 PM
Spring is a proxy-based AOP framework that does not do any classloading magic. All advices are applied to beans managed by the IoC container. As a result of this you cannot apply advices on static methods (the Spring IoC container manages objects, not classes so to say).

You'd have to use AspectJ for this. Spring integrates nicely with AspectJ. For more information on the subject, have a look at chapter 6 of the reference manual: http://www.springframework.org/docs/reference/ch06.html

regards,
Alef Arendsen

dune
Mar 17th, 2005, 09:03 PM
Thanks for the reply.

hammer_010101
Jan 29th, 2007, 04:43 AM
Spring is a proxy-based AOP framework that does not do any classloading magic. All advices are applied to beans managed by the IoC container. As a result of this you cannot apply advices on static methods (the Spring IoC container manages objects, not classes so to say).

You'd have to use AspectJ for this. Spring integrates nicely with AspectJ. For more information on the subject, have a look at chapter 6 of the reference manual: http://www.springframework.org/docs/reference/ch06.html

regards,
Alef Arendsen

I still don't understand why it's impossible to proxy a static method! The proxy could just call the static method... just it! It doesn't required any classloading magic IMHO....

Marten Deinum
Jan 29th, 2007, 05:05 AM
Well it would You couldn't use interface based proxying because static methods cannot be defined in interfaces. And you couldn't extend the classes (using proxying target classes) because overriding static methods isn't possible in java.

So then how would you proxy a target class from an instance?

The only thing you can do is use AspectJ to it's full extend.