在spring2.0之前bean只有2种作用域即:singleton(单例)、non-singleton(也称prototype),Spring2.0以后,增加了session、request、global session、application四种专用于Web应用程序上下文的Bean。因此,默认情况下Spring2.0现在有六种类型的Bean.所以Spring中bean的scope有六种:singleton,prototype,request,session,global session,application。
application的scope和singleton的很像,不过有两点区别:application的是对于每个ServletContext产生一个实例而非每个Spring容器;另外它是ServletContext可见的。
例子如下:
配置文件
使用singleton时
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 使用bean 的scope属性来配置bean的作用域 singleton:默认值,容器初始化时创建bean实例,在整个容器的生命周期内只创建一个bean,单例的。 prototype:原型的,容器初始化时不创建bean的实例,而在每次请求时都创建一个新的Bean实例,并返回。 --> <bean id="car" class="relation.Car" scope="singleton"> <property name="name" value="hai"></property> <property name="price" value="10000.00"></property> <property name="color" value="red"></property> <property name="num" value="23"></property> </bean> </beans>
Car类文件:
package relation; public class Car { public String name; public int num; public String color; public double price; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Car(){ System.out.println ("Car'controle......."); } @Override public String toString() { return "relation.Car{" + "name='" + name + '\'' + ", num=" + num + ", color='" + color + '\'' + ", price=" + price + '}'; } }
main文件:
package scope; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import relation.Car; import relation.Person; public class Main { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("bean-scope.xml"); Car car = (Car) applicationContext.getBean ("car"); Car car1 = (Car) applicationContext.getBean ("car"); System.out.println (car == car1); } }
输出结果:
Car'controle....... true
使用property 时:
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 使用bean 的scope属性来配置bean的作用域 singleton:默认值,容器初始化时创建bean实例,在整个容器的生命周期内只创建一个bean,单例的。 prototype:原型的,容器初始化时不创建bean的实例,而在每次请求时都创建一个新的Bean实例,并返回。 --> <bean id="car" class="relation.Car" scope="prototype"> <property name="name" value="hai"></property> <property name="price" value="10000.00"></property> <property name="color" value="red"></property> <property name="num" value="23"></property> </bean> </beans>
car与main文件与上面的一样
但是输出结果是不一样的:
这个输出的结果是:
Car'controle....... Car'controle....... false
这就证明了用property时来配置bean的作用域,容器初始化时不创建bean的实例,而在每次请求时都创建一个新的Bean实例,并返回。所以就有了两个bean,而两个bean不一样,所以最后输出是false.