zoukankan      html  css  js  c++  java
  • Spring中Bean的作用域

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/bean-scopes.html

    Bean的作用域(Scope)

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

    Spring 框架支持以下五个作用域,如果你使用web-aware ApplicationContext时,其中三个是可用的。

    作用域描述
    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,如下所示:

    <bean id="..." class="..." scope="singleton"></bean>

    例子:

    pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.jsoft.testspring</groupId>
      <artifactId>testsingletonscope</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>testsingletonscope</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        
        <!-- Spring Core -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
         
        <!-- Spring Context -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        
      </dependencies>
    </project>

    HelloWorld.java:

    package com.jsoft.testspring.testsingletonscope;
    
    public class HelloWorld {
        private String messageString;
        
        public void setMessage(String message){
            this.messageString = message;
        }
        
        public String getMessage(){
            return this.messageString;
        }
    }

    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.xsd">
                            
        <bean id="helloWorld" class="com.jsoft.testspring.testsingletonscope.HelloWorld" scope="singleton">
        </bean>
       
    </beans>

    App.java:

    package com.jsoft.testspring.testsingletonscope;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Hello world!
     *
     */
    public class App 
    {
        public static void main( String[] args )
        {
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
            HelloWorld helloWorld = (HelloWorld)applicationContext.getBean("helloWorld");
            helloWorld.setMessage("A");
            System.out.println(helloWorld.getMessage());
            HelloWorld helloWorld2 = (HelloWorld)applicationContext.getBean("helloWorld");
            System.out.println(helloWorld2.getMessage());
        }
    }

    运行结果:

    prototype作用域:

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

    例子:

    pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.jsoft.testspring</groupId>
      <artifactId>testprototypescope</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>testprototypescope</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        
        <!-- Spring Core -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
         
        <!-- Spring Context -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        
      </dependencies>
    </project>

    HelloWorld.java:

    package com.jsoft.testspring.testprototypescope;
    
    public class HelloWorld {
        private String messageString;
        
        public void setMessage(String message){
            this.messageString = message;
        }
        
        public String getMessage(){
            return this.messageString;
        }
    }

    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.xsd">
                            
        <bean id="helloWorld" class="com.jsoft.testspring.testprototypescope.HelloWorld" scope="prototype">
        </bean>
       
    </beans>

    App.java:

    package com.jsoft.testspring.testprototypescope;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Hello world!
     *
     */
    public class App 
    {
        public static void main( String[] args )
        {
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
            HelloWorld helloWorld = (HelloWorld)applicationContext.getBean("helloWorld");
            helloWorld.setMessage("A");
            System.out.println(helloWorld.getMessage());
            HelloWorld helloWorld2 = (HelloWorld)applicationContext.getBean("helloWorld");
            System.out.println(helloWorld2.getMessage());
        }
    }

    运行结果:

    测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test4

  • 相关阅读:
    一次安装。net core的经历
    c# task 等待所有子线程执行完的写法
    .net 中的async,await理解
    dbeaver pgsql连接工具
    oracle 导出表结构和备注
    abp
    发布站点
    excel 拆分多个excel并保持
    重定向和反向代理的区别
    es6中的解构赋值
  • 原文地址:https://www.cnblogs.com/EasonJim/p/6876445.html
Copyright © 2011-2022 走看看