Announcement Announcement Module
Collapse
No announcement yet.
Does MethodInvokingFactoryBean support destroy-method? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Does MethodInvokingFactoryBean support destroy-method?

    I have a bean generated by MethodInvokingFactoryBean, on which I want to call a shutdown method when the application closes down. I've got a destroy-method in the bean config, but I suspect it isn't being called. Is this supported for a bean created in this way?

    Specifically the bean is a CacheManager for ehcache, and the xml to create it is:
    Code:
        <bean id="cacheManager" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
                                                                                destroy-method="shutdown">
            <property name="targetClass"><value>net.sf.ehcache.CacheManager</value></property>
            <property name="targetMethod"><value>create</value></property>
            <property name="arguments">
                <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                    <property name="targetObject">
                        <bean class="org.springframework.core.io.ClassPathResource">
                            <constructor-arg><value>ehcache.xml</value></constructor-arg>
                        </bean>
                    </property>
                    <property name="targetMethod"><value>getURL</value></property>
                </bean>
            </property>
        </bean>
    And if this doesn't work, is there an alternative way of doing it declaratively? Or will I have to do it programmatically?

    Thanks,

  • #2
    This doesn't answer your question, but looking at your xml - are you aware of 'org.springframework.cache.ehcache.EhCacheManagerF actoryBean' class. It is included since version 1.1.1.

    Rgds, Ales

    Comment


    • #3
      I wasn't - I'm still on 1.1
      Thanks for pointing it out, I'll check it out when I upgrade.

      Comment


      • #4
        Re: Does MethodInvokingFactoryBean support destroy-method?

        Originally posted by cmgharris
        I have a bean generated by MethodInvokingFactoryBean, on which I want to call a shutdown method when the application closes down. I've got a destroy-method in the bean config, but I suspect it isn't being called. Is this supported for a bean created in this way?
        No, to the best of my knowledge Spring doesn't do any lifecycle methods on the results of a FactoryBean. It's outside the scope, if you think about it, to call destroy for example, since it can't call init. And the init and destroy attributes really refer to the bean itself (the factory bean in this case), not the result.

        However, why do you not just use the factory-method mechanism (described in the main container chapter) to get the object? Factory-methods (an alternative to constructors) can be used in almost every place that MethodInvokingFactoyBean can, and are more powerful and flexible. For a bean obtained via a factory method, Spring will do proper lifecycle handling. Note though that for prototypes a destroy method will not be called since Spring doesn't manage prototypes past initialization.

        Comment


        • #5
          Thanks Colin, that's what I need to know.

          Comment

          Working...
          X