spring配置文件中的scope详解以及代码

1bean有5种预定义作用域:

1.1singleton:请求都只会返回唯一的bean,传统的模式,要为每一个单独实例编写代码,spring以容器的功能直接提供单实例,很方便
           应用:应为spring有面向切向的特点,所以DAO这些类都可以做成单实例模式
     
     spring因为localThread和面向切向,使得非线程安全的类可以变成线程安全的类,因此默认作用域就是singleton.

1.2.prototype:每次请求都创建一个新的bean对象,所以spring初始化是不会实例化prototype这种bean的

1.3.request跟http请求对应,http中的request没了,作用域为request的bean也会被销毁
1.4.session 跟http请求对应,http中的session没了,作用域为session的bean也会被销毁
1.5.global session

2.代码演示singleton和prototye的区别

2.1目录结构

2.2代码目的,Boss类调用Car类的对象,不同配置会有不同结果

设置scope为prototype

<bean id="car" class="com.jike.spring.chapter04.scope.Car" scope="prototype"/>

那么每次都会创建一个新的Car()对象

设置scope为singleton或者不设置(默认为singleton)

<bean id="car" class="com.jike.spring.chapter04.scope.Car" scope="singleton"/>

那么每次都是调用的同一个Car()对象

2.3 Boss.java

package com.jike.spring.chapter04.scope;

public class Boss {
    private String name ;
    private Car car;
    public Boss(){
    }
    public Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}


    public String toString() {
    	return "name:"+name+"\n car:"+car;
    }
}

2.4 Car.java

package com.jike.spring.chapter04.scope;

public class Car {

	private String color;
	private String brand;
	private double price;

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

}

2.5 Main.java

package com.jike.spring.chapter04.scope;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	public static void main(String[] args) {
		//xml的文件路径名很重要,犯了一个错误,com.jike这种格式是错误的,一定要换成com/jike,把.换成/
		BeanFactory beanFactory = new ClassPathXmlApplicationContext("com/jike/spring/chapter04/scope/conf-scope.xml");
		
		Boss boss1 = beanFactory.getBean("boss1", Boss.class);
		Boss boss2 = beanFactory.getBean("boss2", Boss.class);
		Boss boss3 = beanFactory.getBean("boss3", Boss.class);
		System.out.println(boss1.getCar());
		System.out.println(boss2.getCar());
		System.out.println(boss3.getCar());
	}
	
}

2.6 scope-config.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"
	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="com.jike.spring.chapter04.scope.Car" scope="prototype"/><!--scope变成singleton,那么测试返回的就是同一个car对象  -->
   <bean id="boss1" class="com.jike.spring.chapter04.scope.Boss" p:car-ref="car" />
   <bean id="boss2" class="com.jike.spring.chapter04.scope.Boss" p:car-ref="car" />
   <bean id="boss3" class="com.jike.spring.chapter04.scope.Boss" p:car-ref="car" />
</beans>

2.7结果对比

设置为singleton

com.jike.spring.chapter04.scope.Car@1a5fe33
com.jike.spring.chapter04.scope.Car@1a5fe33
com.jike.spring.chapter04.scope.Car@1a5fe33

设置为prototype

com.jike.spring.chapter04.scope.Car@7740a2
com.jike.spring.chapter04.scope.Car@1a5fe33
com.jike.spring.chapter04.scope.Car@3108bc

猜你喜欢

转载自542255641.iteye.com/blog/2237948