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 點擊打開鏈接