Spring Bean 作用域

Spring Bean 作用域

<bean id="helloworld" class="com.spring.study.HelloWorld" scope="prototype">
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
		HelloWorld obj=(HelloWorld) context.getBean("helloworld");
		obj.setMessage("Hello world");
		String str=obj.getMessage();
		System.out.println(str);
		
		HelloWorld obj1=(HelloWorld) context.getBean("helloworld");
		String str1=obj1.getMessage();
		System.out.println(str1);

//输出结果
Hello world
null
<bean id="helloworld" class="com.spring.study.HelloWorld" scope="singleton">
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
		HelloWorld obj=(HelloWorld) context.getBean("helloworld");
		obj.setMessage("Hello world");
		String str=obj.getMessage();
		System.out.println(str);
		
		HelloWorld obj1=(HelloWorld) context.getBean("helloworld");
		String str1=obj1.getMessage();
		System.out.println(str1);

//输出结果
Hello world
Hello world