Spring_Bean的作用域

在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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值