i read tutorial about spring bean scopes in that they mentioned if bean scope is prototype
Spring container will create new object every context.getBean("id")
statement. and if we specify scope is singleton
it will create only one object even though we write context.getBean("id")
statement two times or more...
i did small example
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");
}
}
}
the above program prints
P hashCode :18303751
Q hashCode :18303751
Same instance!
But in Person
bean scope i given scope="prototype"
why it is printing same Hashcode ????
Explain anyone...
Thanks in Advance...
spconfig.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="person" class="demo.Person" scope="prototype">
<property name="name" value="Hello World" />
</bean>
</beans>
Person.java
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();
}
}