Results 1 to 5 of 5

Thread: Intercepting static methods?

  1. #1
    Join Date
    Mar 2005
    Posts
    11

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

    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
    Alef Arendsen
    SpringSource
    http://www.springsource.com

  3. #3
    Join Date
    Mar 2005
    Posts
    11

    Default

    Thanks for the reply.

  4. #4

    Default

    Quote 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....

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Similar Threads

  1. Replies: 5
    Last Post: Jan 23rd, 2008, 04:30 PM
  2. static methods in spring
    By christophe_grosjean in forum Container
    Replies: 4
    Last Post: Oct 17th, 2005, 04:47 PM
  3. Accessing beans from static methods
    By Agardner in forum Container
    Replies: 5
    Last Post: Jun 23rd, 2005, 06:10 PM
  4. Replies: 7
    Last Post: Nov 3rd, 2004, 06:41 AM
  5. intercepting protected methods
    By souljahboi in forum AOP
    Replies: 2
    Last Post: Oct 19th, 2004, 09:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •