通过对scope属性的设置可实现单例或多例,默认不设置为单例
singleton 单例(默认值)
prototype 多例
例如
<bean id="user" class="spring.User" scope="prototype"></bean>
判断是多例还是单例
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
User user1 = beanFactory.getBean("user", User.class);
User user2 = beanFactory.getBean("user", User.class);
System.out.println(user1 == user2);