Announcement Announcement Module
Collapse
No announcement yet.
Question about init-method/destroy-method attributes Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Question about init-method/destroy-method attributes

    Hi guys. I'm new in Spring and have encountered some unintelligible (at least at first sight) behaviour with init-method/destroy-method attributes.
    From the comments in the spring-beans-2.0.dtd:
    <!--
    Optional attribute for the name of the custom initialization method
    to invoke after setting bean properties. The method must have no arguments,
    but may throw any exception.
    -->
    <!ATTLIST bean init-method CDATA #IMPLIED>

    <!--
    Optional attribute for the name of the custom destroy method to invoke
    on bean factory shutdown. The method must have no arguments,
    but may throw any exception...
    -->
    <!ATTLIST bean destroy-method CDATA #IMPLIED>

    My test bean:

    Code:
    public class Test {
        public Test() {
        }
    
        /*public void start () {
            System.out.println("Test.start()");
        }*/
    
        public void start (boolean flag) {
            System.out.println("Test.start(flag = " + flag + ")");
        }
    
        /*public void shutdown () {
            System.out.println("Test.shutdown()");
        }*/
    
        public void shutdown (boolean flag) {
            System.out.println("Test.shutdown(flag = " + flag + ")");
        }
    }
    Bean's config:
    Code:
        <bean id="test" class="Test" init-method="start" destroy-method="shutdown" lazy-init="false"/>
    There is all ok with init-method referencing start method with argument - i've got
    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'test' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodException: Couldn't find an init method named 'start' on bean with name 'test'
    But shutdown method with argument does not "confuse" the framework - it is called with true argument.

    So the question: is method referenced by destroy-method nevertheless can have argument(s)?

  • #2
    The docs aren't in sync I guess.

    This is what the concerning method says in the javadocs, DisposableBeanAdapter.invokeCustomDestroyMethod (it is private so no docs are available online).

    Code:
    /**
     * Invoke the specified custom destroy method on the given bean.
     * <p>This implementation invokes a no-arg method if found, else checking
     * for a method with a single boolean argument (passing in "true",
     * assuming a "force" parameter), else logging an error.
     */
    So it assumes the boolean to be some kind of force setting.

    Comment


    • #3
      Originally posted by mdeinum View Post
      The docs aren't in sync I guess.

      This is what the concerning method says in the javadocs, DisposableBeanAdapter.invokeCustomDestroyMethod (it is private so no docs are available online).

      Code:
      /**
       * Invoke the specified custom destroy method on the given bean.
       * <p>This implementation invokes a no-arg method if found, else checking
       * for a method with a single boolean argument (passing in "true",
       * assuming a "force" parameter), else logging an error.
       */
      So it assumes the boolean to be some kind of force setting.
      Thank you very much. You have just made clear the problem. The start of the story was the example configuration for
      org.enhydra.jdbc.pool.StandardXAPoolDataSource with destroy-method="shutdown", but StandardXAPoolDataSource inherits from the StandardPoolDataSource the such method only:
      Code:
      public void shutdown(boolean force)

      Comment


      • #4
        The docs aren't in sync I guess.
        Might be worth adding a JIRA request for that. I was slightly confused until Marten replied.

        Comment

        Working...
        X