Spring bean的作用域
1、singleton 单例
singleton :在Spring IOC容器中仅存在一个bean实例,bean以单实例的方式存在。
举例说明:
Car类
package com.at.beans.scope;
public class Car {
private String brand;
private double price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
}
<?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.xsd">
<bean id="car" class="com.at.beans.scope.Car">
<property name="brand" value="Audi"></property>
<property name="price" value="400000"></property>
</bean>
</beans>
package com.at.beans.scope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestCar {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");
Car car = (Car) ctx.getBean("car");
Car car2 = (Car) ctx.getBean("car");
System.out.println(car==car2);
}
}
五月 23, 2017 7:08:39 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@37dc299: startup date [Tue May 23 19:08:39 CST 2017]; root of context hierarchy
五月 23, 2017 7:08:39 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-scope.xml]
true
2、prototype 原型
prototype:每次从容器中调用Bean时,都返回一个新的实例,也即是每次调用getBean()的时候,相当于执行new XxxBean()的操作。
通过修改配置文件
<?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.xsd">
<bean id="car" class="com.at.beans.scope.Car" scope="prototype">
<property name="brand" value="Audi"></property>
<property name="price" value="400000"></property>
</bean>
</beans>
五月 23, 2017 7:17:57 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@539269cb: startup date [Tue May 23 19:17:57 CST 2017]; root of context hierarchy
五月 23, 2017 7:17:57 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-scope.xml]
false
3、request
request:每次HTTP请求都会创建一个新的Bean,该作用域仅适用于WebApplicationContext环境。
4、session
session:同一个HTTP Session共享一个Bean,不同于HTTP Session使用不同的Bean,该作用域仅使用于WebApplicationContext环境。
5、总结
默认情况下, Spring 只为每个在 IOC 容器里声明的 bean创建唯一一个实例, 整个 IOC 容器范围内都能共享该实例,所有后续的 getBean() 调用和 bean引用都将返回这个唯一的 bean实例.该作用域被称为 singleton, 它是所有 Bean 的默认作用域。
By luoyepiaoxue2014
微博地址: http://weibo.com/luoyepiaoxue2014 点击打开链接