Spring Bean Scopes作用域
来源:互联网 发布:淘宝助理导入快递模板 编辑:程序博客网 时间:2024/06/01 21:49
当定义一个Spring的<bean>,必须声明bean 作用域的选项。例如,要强制Spring需要产生一个新的bean实例,应该声明bean的scope属性为prototype。如果你希望Spring 每次都返回同一个bean实例,应该声明bean的作用域,方式类似属性是单例。
Spring框架支持以下五个作用域,其中三个只有当您使用Web感知的 ApplicationContext 可用。
本章将讨论前两个范围和其余三将讨论的时候,我们将讨论有关Web感知Spring的ApplicationContext。
singleton作用域:
如果范围设置为单例,Spring IoC容器创建了一个由该bean定义的对象只有一个实例。这个单一实例存储在这样的单例bean的高速缓存,以及所有后续请求和引用针对该bean返回缓存对象。
默认范围是始终单例,但是当你需要bean的一个实例,可以设置的范围属性单例在bean配置文件中,如下图所示:
<!-- A bean definition with singleton scope --><bean id="..." class="..." scope="singleton"> <!-- collaborators and configuration for this bean go here --></bean>
示例:
让我们使用Eclipse IDE,然后按照下面的步骤来创建一个Spring应用程序:
这里是HelloWorld.java 文件的内容:
package com.yiibai;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.yiibai;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(); }}
以下是需要singleton作用域配置文件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.yiibai.HelloWorld" scope="singleton"> </bean></beans>
一旦创建源代码和bean配置文件来完成,运行应用程序。如果一切顺利,这将打印以下信息:
Your Message : I'm object AYour Message : I'm object A
原型作用域:
如果范围设置为原型,那么Spring IoC容器创建对象的新的bean实例为每个特定的bean发出请求时的时间。作为一项规则,使用prototype作用域为所有状态的bean类和singleton作用域为无状态的bean。
要定义一个原型作用域,可以设置的范围属性为原型的bean配置文件中,如下图所示:
<!-- A bean definition with singleton scope --><bean id="..." class="..." scope="prototype"> <!-- collaborators and configuration for this bean go here --></bean>
示例:
让我们在地方工作的Eclipse IDE,然后按照下面的步骤来创建一个Spring应用程序:
这里是HelloWorld.java 文件的内容:
package com.yiibai;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.yiibai;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:
<?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.yiibai.HelloWorld" scope="prototype"> </bean></beans>
创建源代码和bean配置文件完成后,让我们运行应用程序。如果一切顺利,这将打印以下信息:
Your Message : I'm object AYour Message : null
- Spring Bean Scopes作用域
- 【Java.Spring.Core】【IoC】Bean 作用域 - Scopes
- Spring Bean Scopes Example
- Spring Bean scopes
- Spring bean scopes
- spring bean scopes
- Spring bean scopes example
- Spring(5)-Spring bean scopes example
- Bean scopes
- Bean scopes
- Spring bean 作用域
- spring bean作用域
- Spring bean作用域
- spring bean 作用域
- Spring - Bean作用域
- Spring bean作用域
- spring bean作用域
- Spring bean作用域
- nginx + php(fpm) 提高并发性调优实战
- linux系统下一个基于TCP的网络聊天室
- LayoutInflater的使用
- SQL中varchar和nvarchar有什么区别?
- LI中内容超过长度后以省略号显示的方法
- Spring Bean Scopes作用域
- eclipse安装插件
- 如何让层显示在FLASH之上呢
- linux上用c实现算术编码(二)--具体代码
- vs2012搭建lua+c++环境
- java解惑之字符之谜(谜题16)
- MySQL导入.sql文件及常用命令
- PHP 支持ioncube
- linux设备编译到内核一般有两种方法