Spring_04_Bean的作用域
2018-07-16 本文已影响15人
有_味
Bean的作用域
当在Spring中定义个bean时,你必须声明bean的作用域选项.例如,为了强制Spring在每次需要时都产生一个新的bean实例,你应该声明bean的作用于的属性为prototype.同理,如果你项让Spring每次需要时都返回同一个bean实例,你应该声明bean的作用域为singleton.
| 作用域属性 | 描述 |
|---|---|
| singleton | 该作用域将bean的定义限制在每一个Spring IOC容器中的一个单一实例(默认). |
| prototype | 该作用域将单一bean的定义限制在任意数量的对对象实例. |
| request | 该作用域将bean的定义限制为HTTP请求,只在 web-aware Spring ApplicationContext 的上下文中有效。 |
| session | 该作用域将 bean 的定义限制为 HTTP 会话。 只在web-aware Spring ApplicationContext的上下文中有效。 |
| global-session | 该作用域将 bean 的定义限制为全局 HTTP 会话。只在 web-aware Spring ApplicationContext 的上下文中有效。 |
singleton 作用域
如果作用域设置为singleton,那么Spring IOC容器刚好创建一个由该bean定义的对象的实例,该单一实例将存储在这种单例bean的告诉缓存中,以及针对该bean的所有后续的请求和引用都返回缓存对象.
默认作用域始终是singleton,但是当仅仅需要bean的一个实例时,你可以在bean的配置文件中设置作用域属性为singleton.如下所示:
<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="singleton">
<!-- collaborators and configuration for this bean go here -->
</bean>
例子:
| 步骤 | 描述 |
|---|---|
| 1 | 创建一个名称为 SpringExample 的项目,并且在创建项目的 c src 文件夹中创建一个包 com.tutorialspoint |
| 2 | 使用 Add External JARs 选项,添加所需的 Spring 库,在 Spring Hello World Example 章节解释。 |
| 3 | 在 com.tutorialspoint 包中创建 Java 类 HelloWorld 和 MainApp 。 |
| 4 | 在 c src 文件夹中创建 Beans 配置文件 Beans.xml 。 |
| 5 | 最后一步是创建的所有 Java 文件和 Bean 配置文件的内容,并运行应用程序,解释如下。 |
HelloWorld.java
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void getMessage() {
System.out.println("yout message : "+ message);
}
public void setMessage(String message) {
this.message = message;
}
}
MainApp.java
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import com.sun.org.apache.bcel.internal.util.ClassPath;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
objA.setMessage("Im Object A");
objA.getMessage();
HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
objB.getMessage();
System.out.println(objA == objB);
}
}
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">
<property name="message" value="Hello World!" />
</bean>
</beans>
输出结果:
yout message : Im Object A
yout message : Im Object A
true
prototype 作用域
如果作用域设置为prototype,那么每次特定的bean发出请求时Spring IOC 容器就创建对象的新的bean实例. 一般来说,有状态的bean使用prototype作用域和没有状态的bean使用singleton作用域.
为了定义prototype的作用域 ,你可以在bean的配置文件中设置作用域的属性为prototype,如下所示:
<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="prototype">
<!-- collaborators and configuration for this bean go here -->
</bean>
例子:
| 步骤 | 描述 |
|---|---|
| 1 | 创建一个名称为 SpringExample 的项目,并且在创建项目的 c src 文件夹中创建一个包 com.tutorialspoint. |
| 2 | 使用 Add External JARs 选项,添加所需的 Spring 库,解释见 Spring Hello World Example 章节。 |
| 3 | 在 com.tutorialspoint 包中创建 Java 类 HelloWorld 和 MainApp 。 |
| 4 | 在 c src 文件夹中创建 Beans 配置文件 Beans.xml 。 |
| 5 | 最后一步是创建的所有 Java 文件和 Bean 配置文件的内容,并运行应用程序,解释如下所示。 |
HelloWorld.java
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void getMessage() {
System.out.println("yout message : "+ message);
}
public void setMessage(String message) {
this.message = message;
}
}
MainApp.java
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import com.sun.org.apache.bcel.internal.util.ClassPath;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
objA.setMessage("Im Object A");
objA.getMessage();
HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
objB.getMessage();
System.out.println(objA ==objB);
}
}
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">
<property name="message" value="Hello World!" />
</bean>
</beans>
输出结果:
yout message : Im Object A
yout message : Hello World!
false