Note that there are some explanatory texts on larger screens.

plurals
  1. POwhat is the Difference Between singleton and prototype scopes in Spring 2.5
    primarykey
    data
    text
    <p>i read tutorial about spring bean scopes in that they mentioned if bean scope is <code>prototype</code> Spring container will create new object every <code>context.getBean("id")</code> statement. and if we specify scope is <code>singleton</code> it will create only one object even though we write <code>context.getBean("id")</code> statement two times or more... i did small example</p> <pre><code>Demo.java public class Demo { public static void main(String[] args) { ApplicationContext con=new ClassPathXmlApplicationContext("spconfig.xml"); Person p=(Person)con.getBean("person"); Person q=(Person)con.getBean("person"); System.out.println(" P hashCode :"+p.hashCode()); System.out.println(" Q hashCode :"+q.hashCode()); if (p==q) { System.out.println("Same instance!"); } else { System.out.println("Different instance"); } } } </code></pre> <p>the above program prints</p> <pre><code> P hashCode :18303751 Q hashCode :18303751 Same instance! </code></pre> <p>But in <code>Person</code> bean scope i given <code>scope="prototype"</code> why it is printing same Hashcode ????</p> <p>Explain anyone...</p> <p>Thanks in Advance...</p> <h2> spconfig.xml</h2> <pre><code>&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"&gt; &lt;beans&gt; &lt;bean id="person" class="demo.Person" scope="prototype"&gt; &lt;property name="name" value="Hello World" /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <h2> Person.java</h2> <pre><code>package demo; public class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void printHello() { System.out.println("Hello ! " + name); } @Override public int hashCode() { return super.hashCode(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload