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

 

相关文章:

  • 2021-10-28
  • 2021-04-23
  • 2021-07-18
  • 2021-07-23
猜你喜欢
  • 2022-01-15
  • 2021-10-14
  • 2021-07-05
相关资源
相似解决方案