spring bean配置后再默认情况下是单例的,如果需要配置可以选择 prototype, request, session和global session

在配置spring mvc的action时,可以对action使用 prototype

    <!-- singleton: 默认单例 prototype: 多例 request ,session和global session: 只适用于web程序 -->
<bean id="scope_prototype" class="com.lee.spring005.scope.Scope"
scope="prototype"></bean> <!-- destroy-method 一般在数据源的时候用到,关闭容易后就销毁连接 -->
<bean id="initDestory" class="com.lee.spring006.init_destory.InitDestory"
init-method="init" destroy-method="destory"></bean>

bean的创建销毁过程:

 package com.lee.spring006.init_destory;

 public class InitDestory {

     public InitDestory() {
System.out.println("InitDestory() ");
} public void hello() {
System.out.println("Hello InitDestory!");
} public void init() {
System.out.println("init");
} public void destory() {
System.out.println("destory");
}
}

测试:

     @Test
public void testInitDestory() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); InitDestory initDestory = (InitDestory)context.getBean("initDestory");
initDestory.hello(); ClassPathXmlApplicationContext app = (ClassPathXmlApplicationContext)context;
app.close();
}

github地址:https://github.com/leechenxiang/maven-spring001-helloworld

05-08 15:14