javascript
Spring Bean范围
介紹:
Spring核心容器實例化bean并管理其生命周期。 在定義bean時,我們可以提供其范圍。 除非明確提供,否則單例是Spring容器中Bean的默認范圍。
Spring提供了五種類型的bean作用域。 在本教程中,我們將探討它們中的每一個。
1.
單例作用域可確保每個Spring IoC容器僅存在該bean的一個實例。 該單個Bean實例存儲在所有單個Bean的緩存中,并為每個后續請求返回對此緩存對象的引用。
眾所周知,默認情況下,所有Spring bean都是單例的。
假設我們有一個Student bean:
@Component public class Student {private int id;private String name;//constructor, getters, setters}默認情況下, Student類將是單例 bean。 讓我們嘗試將其加載到我們的main()方法中:
public class App {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml");Student student1 = (Student) context.getBean("student");student1.setName("John");System.out.println(student1.getName()); //Prints 'John'Student student2 = (Student) context.getBean("student");System.out.println(student2.getName()); //Prints 'John'} }顯然,已經為第二次調用返回了Student Bean的現有實例。 換句話說, student1和student2引用都指向同一對象。
2.
對于基于原型的 bean, 將 在我們的應用程序代碼中為該特定bean的每個請求創建 一個新實例 。
我們可以使用@Scope批注將bean的范圍配置為原型 :
@Component @Scope("prototype") public class Student {... }或使用基于XML的配置時,我們將:
<bean id = "student" class="org.programmergirl.domain.Student"scope="prototype"> </bean>現在,讓我們看看在main()方法中會發生什么:
public class App {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml");Student student1 = (Student) context.getBean("student");student1.setName("John");System.out.println(student1.getName()); //Prints 'John'Student student2 = (Student) context.getBean("student");System.out.println(student2.getName()); //Prints null} }這里, student2引用Student類的新實例,其名稱屬性為null 。
注意, 我們應該始終對有狀態bean使用原型作用域,對無狀態bean使用單例作用域。
3.
使用請求范圍時, 將為每個HTTP請求創建一個新實例。 HTTP請求完成時,該bean實例被銷毀。
我們可以使用注釋配置請求范圍:
@Component @Scope("request") public class Student {... }//Or@Component @RequestScope public class Student {... }或通過XML配置:
<bean id = "student" class="org.programmergirl.domain.Student"scope="request"> </bean>4.
對于會話范圍,Spring容器為每個HTTP會話創建一個新的bean實例。 HTTP會話中的所有HTTP請求將共享相同的bean實例。
要配置會話范圍的Bean,我們將使用:
@Component @Scope("session") public class Student {... }//Or@Component @SessionScope public class Student {... }對于基于xml的配置,我們將有:
<bean id = "student" class="org.programmergirl.domain.Student"scope="session"> </bean>當HTTP會話關閉時,容器將銷毀會話范圍的Bean實例。
5.
通過應用程序范圍,一個可感知Web的容器會根據ServletContext創建一個bean實例。 請注意, 應用程序范圍不同于單例范圍:
- 我們可以在獨立應用程序中使用單例作用域。 該應用程序范圍僅對基于Web的Spring容器/應用程序有效
- 對于單例作用域,每個應用程序上下文存在一個實例。 另一方面, 應用程序范圍的Bean對于整個ServletContext具有單個實例。 我們可能有多個應用程序共享同一個ServletContext
讓我們將Student bean配置為應用程序范圍的bean:
@Component @Scope("application") public class Student {... }//Or@Component @ApplicationScope public class Student {... }基于xml的定義如下所示:
<bean id = "student" class="org.programmergirl.domain.Student"scope="application"> </bean>結論:
在本教程中,我們探索了各種Spring bean范圍。
請注意, 請求,會話和應用程序 bean范圍僅對可感知網絡的Spring ApplicationContext有效 。
翻譯自: https://www.javacodegeeks.com/2019/06/spring-bean-scopes.html
總結
以上是生活随笔為你收集整理的Spring Bean范围的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大众汽车贝瑞德:合肥电动汽车工厂即将投产
- 下一篇: jooq中record_在Spring中