Spring_04_Bean的作用域

Bean的作用域

 当在Spring中定义个bean时,你必须声明bean的作用域选项.例如,为了强制Spring在每次需要时都产生一个新的bean实例,你应该声明bean的作用于的属性为prototype.同理,如果你项让Spring每次需要时都返回同一个bean实例,你应该声明bean的作用域为singleton.

作用域属性描述
singleton该作用域将bean的定义限制在每一个Spring IOC容器中的一个单一实例(默认).
prototype该作用域将单一bean的定义限制在任意数量的对对象实例.
request该作用域将bean的定义限制为HTTP请求,只在 web-aware Spring ApplicationContext 的上下文中有效。
session该作用域将 bean 的定义限制为 HTTP 会话。 只在web-aware Spring ApplicationContext的上下文中有效。
global-session该作用域将 bean 的定义限制为全局 HTTP 会话。只在 web-aware Spring ApplicationContext 的上下文中有效。

singleton 作用域

 如果作用域设置为singleton,那么Spring IOC容器刚好创建一个由该bean定义的对象的实例,该单一实例将存储在这种单例bean的告诉缓存中,以及针对该bean的所有后续的请求和引用都返回缓存对象.

 默认作用域始终是singleton,但是当仅仅需要bean的一个实例时,你可以在bean的配置文件中设置作用域属性为singleton.如下所示:

<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="singleton">
<!-- collaborators and configuration for this bean go here -->
</bean>

例子:

步骤描述
1创建一个名称为 SpringExample 的项目,并且在创建项目的 c src 文件夹中创建一个包 com.tutorialspoint
2使用 Add External JARs 选项,添加所需的 Spring 库,在 Spring Hello World Example 章节解释。
3在 com.tutorialspoint 包中创建 Java 类 HelloWorld 和 MainApp 。
4在 c src 文件夹中创建 Beans 配置文件 Beans.xml 。
5最后一步是创建的所有 Java 文件和 Bean 配置文件的内容,并运行应用程序,解释如下。

HelloWorld.java

package com.tutorialspoint;

public class HelloWorld {
    private String message;

    public void getMessage() {
        System.out.println("yout message : "+ message);
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

MainApp.java

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

import com.sun.org.apache.bcel.internal.util.ClassPath;

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context  = new ClassPathXmlApplicationContext("Beans.xml");
        HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
        objA.setMessage("Im Object A");
        objA.getMessage();

        HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
        objB.getMessage();

        System.out.println(objA == objB);

    }
}

Beans.xml

<?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-3.0.xsd">
    <bean id="helloWorld" class="com.tutorialspoint.HelloWorld" scope="singleton">
        <property name="message" value="Hello World!" />
    </bean>
</beans>

输出结果:

yout message : Im Object A
yout message : Im Object A
true

prototype 作用域

 如果作用域设置为prototype,那么每次特定的bean发出请求时Spring IOC 容器就创建对象的新的bean实例. 一般来说,有状态的bean使用prototype作用域和没有状态的bean使用singleton作用域.

为了定义prototype的作用域 ,你可以在bean的配置文件中设置作用域的属性为prototype,如下所示:

<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="prototype">
<!-- collaborators and configuration for this bean go here -->
</bean>

例子:

步骤描述
1创建一个名称为 SpringExample 的项目,并且在创建项目的 c src 文件夹中创建一个包 com.tutorialspoint.
2使用 Add External JARs 选项,添加所需的 Spring 库,解释见 Spring Hello World Example 章节。
3在 com.tutorialspoint 包中创建 Java 类 HelloWorld 和 MainApp 。
4在 c src 文件夹中创建 Beans 配置文件 Beans.xml 。
5最后一步是创建的所有 Java 文件和 Bean 配置文件的内容,并运行应用程序,解释如下所示。

HelloWorld.java

package com.tutorialspoint;

public class HelloWorld {
    private String message;

    public void getMessage() {
        System.out.println("yout message : "+ message);
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

MainApp.java

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

import com.sun.org.apache.bcel.internal.util.ClassPath;

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context  = new ClassPathXmlApplicationContext("Beans.xml");
        HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
        objA.setMessage("Im Object A");
        objA.getMessage();

        HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
        objB.getMessage();

        System.out.println(objA ==objB);

    }
}

Beans.xml

<?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-3.0.xsd">
    <bean id="helloWorld" class="com.tutorialspoint.HelloWorld" scope="prototype">
        <property name="message" value="Hello World!" />
    </bean>
</beans>
输出结果:
yout message : Im Object A
yout message : Hello World!
false

由此可见: 在每调用一次getBean()方法后,会产生一个新的对象.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值