赞
踩
<bean id="userInfo" class="cn.lovepi.UserInfo" scope="singleton"></bean>
<bean id="userInfo" class="cn.lovepi.UserInfo" scope=" prototype "></bean>
<bean id="userInfo" class="cn.lovepi.UserInfo" scope=" request "></bean>
<bean id="userInfo" class="cn.lovepi.UserInfo" scope=" session "></bean>
<bean id="userInfo" class="cn.lovepi.UserInfo"scope=“globalSession"></bean>
- 1. <bean id="car" class="cn.lovepi.Car" scope="singleton"></bean>
- 2. <bean id="boss1" class="cn.lovepi .Boss” p:car-ref=“car"></bean>
- 3. <bean id="boss2" class="cn.lovepi .Boss” p:car-ref=“car"></bean>
- 4. <bean id="boss3" class="cn.lovepi .Boss” p:car-ref=“car"></bean>
在1中car这个Bean生命周期声明为了singleton模式,其他的bean如2,3,4引用了这个Bean。在容器中boss1、boss2、boss3的属性都指向同一个bean car。如下图所示:
- 1. <bean id=“car" class="cn.lovepi.Car" scope=“prototype"></bean>
- 2. <bean id=“boss1" class="cn.lovepi.Boss” p:car-ref=“car"></bean>
- 3. <bean id=“boss2" class="cn.lovepi.Boss” p:car-ref=“car"></bean>
- 4. <bean id=“boss3" class="cn.lovepi.Boss” p:car-ref=“car"></bean>
通过以上的配置,Boss1、boss2、boss3所引用的都是一个新的car的实例,每次通过容器getBean()方法返回的也是一个新的car实例。如下图所示:
- <filter>
- <filter-name>requestContextFilter</filter-name>
- <filter-class>org.springframework.web.filter.RequestContextFilter</filterclass>
- </filter>
- <filter-mapping>
- <filter-name>requestContextFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <listener> <listener-class>
- org.springframework.web.context.request.RequestContextListener
- </listener-class></listener>
- public class Boss {
- private String name;
- private Car car;
-
- public Boss() {
- }
-
- public Car getCar() {
- return car;
- }
-
- public void setCar(Car car) {
- this.car = car;
- }
-
- @Override
- public String toString() {
- return "Boss{" +
- "name='" + name + '\'' +
- ", car=" + car +
- '}';
- }
- }
-
- public class Car {
- private String color;
- private String brand;
- private double price;
-
- public String getColor() {
- return color;
- }
-
- public void setColor(String color) {
- this.color = color;
- }
-
- 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;
- }
-
- }
- <?xml version="1.0" encoding="UTF-8" ?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
-
- <bean id="car" class="cn.lovepi.chapter03.scope.Car" scope="singleton"/>
- <bean id="boss1" class="cn.lovepi.chapter03.scope.Boss" p:car-ref="car" />
- <bean id="boss2" class="cn.lovepi.chapter03.scope.Boss" p:car-ref="car" />
- <bean id="boss3" class="cn.lovepi.chapter03.scope.Boss" p:car-ref="car" />
- </beans>
最后我们创建测试类进行测试
- public class Main {
- public static void main(String[] args){
- //加载配置文件,启动IoC容器
- BeanFactory factory=
- new FileSystemXmlApplicationContext("src/conf/conf-scope.xml");
- //获得bean实例
- Boss boss1=factory.getBean("boss1",Boss.class);
- Boss boss2=factory.getBean("boss2",Boss.class);
- Boss boss3=factory.getBean("boss3",Boss.class);
- System.out.println(boss1.getCar());
- System.out.println(boss2.getCar());
- System.out.println(boss3.getCar());
- }
- }
- Scope customScope = new ThreadScope();
- beanFactory.registerScope(“thread”, customeScope);
- <bean id=“***” class=“***” scope=“scopeName” />
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。