post-image

Spring – Bean Scopes

Spring Boot Starters & Common Configurations Tổng quan

NỘI DUNG BÀI VIẾT

Khi định nghĩa một bean bạn tùy chọn khai báo phạm vi của bean đó. Ví dụ, để Spring tạo ra 1 bean mới mỗi khi cần thiết thì bạn nên khai báo thuộc tính phạm vi bean là prototype. Tương tự, nếu bạn muốn Spring trả về cùng một bean, bạn nên khai báo thuộc tính phạm vi của bean là signleton. Spring Framework hỗ trợ năm phạm vi sau, ba trong số đó chỉ khả dụng nếu bạn sử dụng ApplicationContext.

STTPhạm vi và mô tả
1singleton
Phạm vi này định nghãi bean thành một thể hiện duy nhất cho mỗi vùng chứa Spring Ioc(mặc định).
2prototype
Phạm vi này nói với Spring IoC container rằng tạo ra một thể hiện mới của bean mỗi khi cần thiết.
3request
Phạm vi này định nghĩa một bean cho một HTTP request. Chỉ có giá trị trong ngữ cảnh của một ứng dụng Spring Web.
4session
Phạm vi này định nghịa bean cho 1 HTTP session. Chỉ hợp lệ trong ngữ cảnh của Spring ApplicationContext.
5global-session
Phạm vi này định nghĩa một bean cho một global HTTP session. Chỉ có giá trị trong ngữ cảnh của một ứng dụng Spring ApplicationContext.

Phạm vi Singleton

Nếu phạm vi của bean được thiết lập là singleton, Spring IoC container tạo ra chính xác một thể hiện của đối tượng được xác định bởi định nghĩa của bean đó. Đối tượng này được lưu trữ trong một bộ nhớ cache, và tất cả các yêu cầu tiếp theo mà tham chiếu đến bean đó thì đối tượng lưu trữ được trả về.

Phạm vi mặc định luôn là singleton. Tuy nhiên, bạn có thể thiết lập thuộc tính scope thành singleton trong tệp tin cấu hình bean, như được trình bày trong đoạn mã sau đây.

<!-- A bean definition with singleton scope -->
<bean id = "..." class = "..." scope = "singleton">
   <!-- collaborators and configuration for this bean go here -->
</bean>Code language: HTML, XML (xml)

Ví dụ:

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);
   }
}Code language: JavaScript (javascript)

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();
   }
}Code language: JavaScript (javascript)

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">
   </bean>

</beans>Code language: HTML, XML (xml)

Kết quả:

Your Message : I'm object A
Your Message : I'm object ACode language: PHP (php)

Phạm vi Prototype

Nếu phạm vi được thiết lập là prototype, Spring IoC container tạo ra một thể hiện bean mới của đối tượng mỗi khi được yêu cầu.

Để định nghĩa phạm vi prototype, bạn có thể thiết lập thuộc tính scope thành prototype trong tệp tin cấu hình bean, như thể hiện trong đoạn mã sau đây:

<!-- A bean definition with prototype scope -->
<bean id = "..." class = "..." scope = "prototype">
   <!-- collaborators and configuration for this bean go here -->
</bean>Code language: HTML, XML (xml)

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);
   }
}Code language: JavaScript (javascript)

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();
   }
}Code language: JavaScript (javascript)

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">
   </bean>

</beans>Code language: HTML, XML (xml)

Kết quả:

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

Nguồn:

https://www.tutorialspoint.com/spring/spring_bean_scopes.htm

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *