📜  Spring-Bean范围

📅  最后修改于: 2020-11-11 06:59:22             🧑  作者: Mango


定义时,可以选择声明该bean的作用域。例如,要强制Spring每次需要一个新的bean实例时,应将bean的scope属性声明为prototype 。同样,如果希望Spring每次需要一个实例时都返回相同的bean实例,则应将bean的scope属性声明为singleton

Spring框架支持以下五个范围,其中三个仅在使用Web感知的ApplicationContext时可用。

Sr.No. Scope & Description
1

singleton

This scopes the bean definition to a single instance per Spring IoC container (default).

2

prototype

This scopes a single bean definition to have any number of object instances.

3

request

This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.

4

session

This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.

5

global-session

This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.

在本章中,我们将讨论前两个范围,而其余三个将在讨论有关Web的Spring ApplicationContext时进行讨论。

单例范围

如果将范围设置为单例,则Spring IoC容器将恰好创建该bean定义所定义的对象的一个实例。该单个实例存储在此类单例bean的高速缓存中,并且对该命名bean的所有后续请求和引用都返回该高速缓存的对象。

默认范围始终为单例。但是,当您只需要一个bean的一个实例时,可以在bean配置文件中将scope属性设置为singleton ,如以下代码片段所示-



   

让我们拥有一个运行良好的Eclipse IDE,并采取以下步骤来创建一个Spring应用程序:

Steps Description
1 Create a project with a name SpringExample and create a package com.tutorialspoint under the src folder in the created project.
2 Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3 Create Java classes HelloWorld and MainApp under the com.tutorialspoint package.
4 Create Beans configuration file Beans.xml under the src folder.
5 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

这是HelloWorld.java文件的内容-

package com.tutorialspoint;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }
   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
}

以下是MainApp.java文件的内容-

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

      objA.setMessage("I'm object A");
      objA.getMessage();

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

以下是单例作用域所需的配置文件Beans.xml-





   
   


完成创建源和Bean配置文件后,让我们运行该应用程序。如果您的应用程序一切正常,它将打印以下消息:

Your Message : I'm object A
Your Message : I'm object A

原型范围

如果将范围设置为原型,则每次对该特定bean发出请求时,Spring IoC容器都会创建该对象的新bean实例。通常,对所有有状态的bean使用原型范围,对无状态的bean使用单例范围。

要定义原型范围,可以在bean配置文件中将scope属性设置为prototype ,如以下代码片段所示-



   

让我们准备好运行中的Eclipse IDE,并按照以下步骤创建Spring应用程序:

Steps Description
1 Create a project with a name SpringExample and create a package com.tutorialspoint under the src folder in the created project.
2 Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3 Create Java classes HelloWorld and MainApp under the com.tutorialspoint package.
4 Create Beans configuration file Beans.xml under the src folder.
5 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

这是HelloWorld.java文件的内容

package com.tutorialspoint;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }
   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
}

以下是MainApp.java文件的内容-

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

      objA.setMessage("I'm object A");
      objA.getMessage();

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

以下是原型范围所需的配置文件Beans.xml-





   
   


完成创建源和Bean配置文件后,让我们运行该应用程序。如果您的应用程序一切正常,它将打印以下消息:

Your Message : I'm object A
Your Message : null