title: Spring: A bean's init-method is being triggered when invoking other beans
body: <p>I'm currently experimenting on bean callback methods and noticed that if I define callback methods for one bean, these methods are also called on other beans.  </p>
  
  <p>I have a classes named <code>x.HelloWorld</code> (no callbacks) and <code>y.HelloWorld</code> (has init and destroy callback methods).</p>
  
  <p><strong>HelloWorld.java:</strong></p>
  
  <pre><code>public class HelloWorld {
      private String message;
  
      public void setMessage(String message) {
          this.message = message;
      }
  
      public void getMessage() {
          System.out.println("Your Message : " + message);
      }
  }
  </code></pre>
  
  <p><strong>HelloWorld2.java:</strong></p>
  
  <pre><code>public class HelloWorld {
      private String message;
  
      public void setMessage(String message) {
          this.message = message;
      }
  
      public void getMessage() {
          System.out.println("Your Message : " + message);
      }
  
      public void init() {
          System.out.println("Bean is going through init.");
      }
  
      public void destroy() {
          System.out.println("Bean will destroy now.");
      }
  }
  </code></pre>
  
  <p><strong>x.MainApp.java:</strong></p>
  
  <pre><code>import org.springframework.context.support.AbstractApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  
  public class MainApp {
      public static void main(String[] args) {
  
          AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
  
          HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
          obj.getMessage();
      }
  }
  </code></pre>
  
  <p><strong>y.MainApp.java:</strong></p>
  
  <pre><code>import org.springframework.context.support.AbstractApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  
  public class MainApp {
      public static void main(String[] args) {
  
          AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
  
          HelloWorld obj = (HelloWorld) context.getBean("helloWorld2");
          obj.getMessage();
          context.registerShutdownHook();
      }
  } 
  </code></pre>
  
  <p><strong>beans.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.3.xsd"&gt;
  
      &lt;bean id="helloWorld" class="x.HelloWorld"&gt;
          &lt;property name="message" value="Hello World (1)" /&gt;
      &lt;/bean&gt;
      &lt;bean id="helloWorld2" class="y.HelloWorld"
          init-method="init" destroy-method="destroy"&gt;
          &lt;property name="message" value="Hello World (2)" /&gt;
      &lt;/bean&gt; 
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>Results:</strong>
  Running <code>y.MainApp</code> (works as expected):</p>
  
  <pre><code>Bean is going through init. 
  Your Message : Hello World (2)
  Bean will destroy now.
  </code></pre>
  
  <p>Running <code>x.MainApp</code> (the init callback of <code>x.HelloWorld</code> is triggered by <code>y.HelloWorld</code>. Confused of this one.. any help is very much appreciated...)</p>
  
  <pre><code>Bean is going through init.
  Your Message : Hello World (1)
  </code></pre>