定义 <bean> 时,您可以选择声明该 bean 的作用域。例如,要强制 Spring 在每次需要时生成一个新的 bean 实例,您应该将 bean 的 scope 属性声明为prototype。类似地,如果您希望 Spring 在每次需要时返回相同的 bean 实例,您应该将 bean 的 scope 属性声明为singleton。

Spring Framework 支持以下五个范围,其中三个仅在您使用 web-aware ApplicationContext 时可用。

在本章中,我们将讨论前两个范围,其余三个范围将在讨论 Web 感知 Spring ApplicationContext 时讨论。

单例范围

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

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

<!--Abeandefinitionwithsingletonscope--><beanid="..."class="..."scope="singleton">
<!--collaboratorsandconfigurationforthisbeangohere--></bean>

示例

让我们有一个工作的 Eclipse IDE 并采取以下步骤来创建一个 Spring 应用程序 -

这是HelloWorld.java文件的内容-

packagecom.tutorialspoint;publicclassHelloWorld{
privateStringmessage;

publicvoidsetMessage(Stringmessage){
this.message=message;
}
publicvoidgetMessage(){
System.out.println("YourMessage:"+message);
}}

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

packagecom.tutorialspoint;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;publicclassMainApp{
publicstaticvoidmain(String[]args){
ApplicationContextcontext=newClassPathXmlApplicationContext("Beans.xml");
HelloWorldobjA=(HelloWorld)context.getBean("helloWorld");

objA.setMessage("I'mobjectA");
objA.getMessage();

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

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

<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="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">

<beanid="helloWorld"class="com.tutorialspoint.HelloWorld"scope="singleton">
</bean></beans>

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

YourMessage:I'mobjectA
YourMessage:I'mobjectA

原型范围

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

要定义原型范围,您可以在 bean 配置文件中将范围属性设置为原型,手机游戏买号平台如以下代码片段所示 -

<!--Abeandefinitionwithprototypescope--><beanid="..."class="..."scope="prototype">
<!--collaboratorsandconfigurationforthisbeangohere--></bean>

示例

让我们使用 Eclipse IDE 并按照以下步骤创建一个 Spring 应用程序 -

这是HelloWorld.java文件的内容

packagecom.tutorialspoint;publicclassHelloWorld{
privateStringmessage;

publicvoidsetMessage(Stringmessage){
this.message=message;
}
publicvoidgetMessage(){
System.out.println("YourMessage:"+message);
}}

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

packagecom.tutorialspoint;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;publicclassMainApp{
publicstaticvoidmain(String[]args){
ApplicationContextcontext=newClassPathXmlApplicationContext("Beans.xml");
HelloWorldobjA=(HelloWorld)context.getBean("helloWorld");

objA.setMessage("I'mobjectA");
objA.getMessage();

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

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

<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="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">

<beanid="helloWorld"class="com.tutorialspoint.HelloWorld"scope="prototype">
</bean></beans>

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

YourMessage:I'mobjectA
YourMessage:null

更多相关文章

  1. ES5、ES6 如何实现继承
  2. Vue组件为什么data必须是一个函数呢?本文案例详解
  3. dom(Document Object Model )初
  4. 如何在敏捷项目中管理范围变更?
  5. 先从_proto_下手理解原型--原型学习(一)
  6. 设计模式是什么鬼(原型)
  7. 一文带你领略JS中原型链的精妙设计!
  8. 深入了解JavaScript中基于原型(prototype)的继承机制
  9. 从问题入手,深入了解JavaScript中原型与原型链

随机推荐

  1. Android专家课程——课后小记
  2. Android开发者的演示工具——asm.jar
  3. Android 设备管理API概览(Device Administ
  4. Android的Activity屏幕切换动画(一)-左右
  5. 解析ANDROID ps命令执行后各项参数的含义
  6. android 开发-数据存储之文件存储
  7. Android(安卓)Studio Error: null, Canno
  8. Android中Touch事件的处理
  9. Android图形系统(八)-app与SurfaceFlinger
  10. 关于Android中传递数据的一些讨论