1,单例模式(spring默认机制)
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>
2,原型模式(每次用容器get时都会产生一个新的对象)
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
3,其余的requst,session,application都只能在web开发中用到
测试:
@Test public void test2(){ ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml"); User user = context.getBean("user2", User.class); User user2 = context.getBean("user2", User.class); System.out.println(user==user2); } 单例模式下输出结果为true 原型模式下输出结果则为false