美文网首页
Spring_04_Bean的作用域

Spring_04_Bean的作用域

作者: 有_味 | 来源:发表于2018-07-16 21:50 被阅读15次

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()方法后,会产生一个新的对象.

相关文章

  • Spring_04_Bean的作用域

    Bean的作用域  当在Spring中定义个bean时,你必须声明bean的作用域选项.例如,为了强制Spring...

  • 一网打尽 JavaScript 的作用域

    JavaScript 的作用域包括:模块作用域,函数作用域,块作用域,词法作用域和全局作用域。 全局作用域 在任何...

  • JS的作用域

    JS的作用域: 全局作用域、函数作用域、eval 作用域、块级作用域 全局作用域: 函数作用域: 结果截屏: 说...

  • js作用域

    1 - 作用域 1.1 作用域概述 全局作用域 局部作用域(函数作用域) 1.2 全局作用域 1.3 局部作用域 ...

  • 作用域,作用域链

    1 - 作用域 1.1 作用域概述 全局作用域 局部作用域(函数作用域) 1.2 全局作用域 1.3 局部作用域 ...

  • 变量作用域

    变量作用域:静态作用域、动态作用域JS变量作用域:JS使用静态作用域JS没有块级作用域(全局作用域、函数作用域等)...

  • 作用域

    词法作用域,动态作用域,全局作用域,局部作用域,函数作用域,块级作用域,有些地方还能看到隐式作用域和显示作用域。 ...

  • C - 作用域

    C - 作用域 一个 C 变量的作用域可以是: 块作用域 函数作用域 函数原型作用域 或 文件作用域 作用域描述程...

  • [ES6]1.1作用域

    作用域 全局作用域(global/window) 函数作用域/局部作用域(function) 块状作用域({}) ...

  • js中的作用域

    作用域就是决定变量有效范围 作用域按决定的时期来区分可分为词法作用域和动态作用域词法作用域: 词法作用域就是作用域...

网友评论

      本文标题:Spring_04_Bean的作用域

      本文链接:https://www.haomeiwen.com/subject/hsdipftx.html