Announcement Announcement Module
Collapse
No announcement yet.
Intercepting static methods? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Intercepting static methods?

    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.ProxyFact oryBean">
    <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.RegexpMetho dPointcutAdvisor">
    <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

  • #2
    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

    Comment


    • #3
      Thanks for the reply.

      Comment


      • #4
        Originally posted by Alef Arendsen View Post
        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....

        Comment


        • #5
          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.

          Comment

          Working...
          X