==============
package myspring;
public class Employee {
private String name;
public Employee(){
// System.out.println("I am constructor...");
}
public String getName() {
return name;
}
public void setName(String name) {
// System.out.println("I am setter...");
this.name = name;
}
}
Beans.xml
===========
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--Default Scope is singleton , we can remove scope variable and observe the output. -->
<bean id="emp" class="myspring.Employee" scope="singleton" >
</bean>
<!--For prototype scope , un comment this and sees the out put for two beans creation. -->
<!--
<bean id="emp" class="myspring.Employee" scope="prototype" >
</bean>
-->
</beans>
Test.java
===========
package myspring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
System.out.println("is Singleton > "+context.isSingleton("emp"));
System.out.println("is Prototype > "+context.isPrototype("emp"));
Employee emp1 = (Employee)context.getBean("emp");
emp1.setName("I am Employee 1");
System.out.println(emp1.getName());
Employee emp2 = (Employee)context.getBean("emp");
System.out.println(emp2.getName());
System.out.println(emp1.hashCode());
System.out.println(emp2.hashCode());
if(emp1 == emp2){
System.out.println(" === >");
}
//System.out.println("==== > "+emp1.);
}
}
No comments:
Post a Comment