bean标签scope属性

  • scope=“singleton” 表示单例,同一个bean只能获取同一个对象
  • scope="prototype"表示多例,同一个bean,获取的是不同的对象

public class People {
    
    
	private String name;
	
}

public class Test {
    
    
	public static void main(String[] args) {
    
    
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		People peo=ac.getBean("people",People.class);
		People peo2=ac.getBean("people",People.class);
		System.out.println(peo);
		System.out.println(peo2);
		
	}
}

单例测试

 <bean id="people" class="cn.wit.test.People" scope="singleton"></bean>

在这里插入图片描述

多例测试

 <bean id="people" class="cn.wit.test.People" scope="prototype"></bean>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/WA_MC/article/details/112655657