当你定义一个<bean>在Spring中,你可以声明bean的范围。例如,每次在需要的时候让Spring产生一个新的实例,你需要将bean的scope属性声明为prototype。同样如果你想在每次需要时返回同一个实例,你需要声明bean的scope属性为singleton。
Spring框架提供对如下5种范围的支持,3种是只有在使用web方面的ApplicationContext时才可使用。
范围 | 描述 |
singleton | 这个范围定义bean在每个Spring IoC容器中只存在一个实例。 |
prototype | 这个范围定义bean可以有任意多个实例。 |
request | 这个范围定义bean是为HTTP request定义的。只有在web相关的Spring ApplicationContext才可使用。 |
session | 这个范围定义bean是为HTTP session。只有在web相关的Spring ApplicationContext才可使用。 |
global-session | 这个范围定义bean是为全局的HTTP session。。只有在web相关的Spring ApplicationContext才可使用。 |
本章将讨论前两种范围,余下的叁种将在讨论到web相关的Spring ApplicationContext再做讨论。
Spring配置元数据
Spring IoC容器完全解耦于实际配置元数据的方式。如下三种重要的方法提供配置元数据给Spring容器:1、 基于XML的配置文件
2、 基于注解的配置
3、 基于Java的配置
你已经看到过基于XML的配置元数据是如何提供给容器的,但让我们看看另一种用不同的bean定义基于XML的配置文件包括lazy initialization、initialization method和destruction method:
<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="singleton">
<!-- collaborators and configuration for this bean go here -->
</bean>
示例—singleton
让我们按如下的步骤在Eclipse IDE中创建一个Spring应用:
步骤 | 描述 |
1 | 创建一个SpringExample的项目并在src目录下创建com.tutorialspoint包。 |
2 | 在Add External JARs选项卡中添加Spring库如在Spring Hello World章节中所讲。 |
3 | 在com.tutorialspoint包下创建HelloWorld和MainApp类。 |
4 | 在src目录下创建bean的配置文件Beans.xml |
5 | 最后一步在Java类中和Bean配置文件中添加内容,并运行应用。 |
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);
}
}
如下是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();
}
}
如下是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.tutorialspoint.HelloWorld" scope="singleton">
</bean>
</beans>
一旦你已经完成了源代码和bean配置文件,运行应用,如果应用一切正常,会打印如下消息:
Your Message : I'm object A
Your Message : I'm object A
prototype 范围
如果scope设置为prototype,Spring IoC容器每次在请求获取bean时会创建一个新的bean实例。作为一个准则,所有有状态bean用prototype范围,无状态的bean使用singleton范围。
为了定义prototype范围,你可以设置scope属性是prototype在bean配置文件中,如下所示:
<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="prototype">
<!-- collaborators and configuration for this bean go here -->
</bean>
示例—prototype
让我们按如下的步骤在Eclipse IDE中创建一个Spring应用:
步骤 | 描述 |
1 | 创建一个SpringExample的项目并在src目录下创建com.tutorialspoint包。 |
2 | 在Add External JARs选项卡中添加Spring库如在Spring Hello World章节中所讲。 |
3 | 在com.tutorialspoint包下创建HelloWorld和MainApp类。 |
4 | 在src目录下创建bean的配置文件Beans.xml |
5 | 最后一步在Java类中和Bean配置文件中添加内容,并运行应用。 |
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);
}
}
如下是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();
}
}
如下是prototype范围配置文件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>
一旦你已经完成了源代码和bean配置文件,运行应用,如果应用一切正常,会打印如下消息:
Your Message : I'm object A
Your Message : null