java - How many spring beans will be created in following Case -
according me 1 bean should created since have given scope singleton output saying different thing. can please ellaborate following me please,
helloworld.java
public class helloworld { private string message; public helloworld(string message) { system.out.println(message+ "bean created"); this.message=message; } public void getmessage() { system.out.println("your message : " + message); } }
main.java
public class main { public static void main(string args[]) { applicationcontext context = new classpathxmlapplicationcontext("beans.xml"); helloworld obj = (helloworld) context.getbean("helloworld"); obj.getmessage(); helloworld obj2 = (helloworld) context.getbean("helloworld2"); obj2.getmessage(); obj.getmessage(); system.out.println(obj.hashcode()); system.out.println(obj.hashcode()); } }
beans.xml
<bean id = "helloworld" class = "helloworld" scope="singleton"> <!--<property name = "message" value = "hello world!"/>--> <constructor-arg value="helloworld1"/> </bean> <bean id = "helloworld2" class = "helloworld" scope="singleton"> <!--<property name = "message" value = "hello world2!"/>--> <constructor-arg value="helloworld2"/> </bean>
output:
helloworld1bean created helloworld2bean created message : helloworld1 message : helloworld2 message : helloworld1 935148943 935148943
definitely 2 try print obj2.hashcode. not obj hashcode
Comments
Post a Comment