Spring Bean的作用域:用Spring来解决Struts中Action的单实例有关问题

Spring Bean的作用域:用Spring来解决Struts中Action的单实例问题

我们知道在Struts中Action是从map中拿出来的,是单实例的。那么在多线程调用的时候会出问题。

那么在Spring中通过getBean方法每调用一次,spring都会new一个实例给我们,所以可以利用这一点把Struts中action的创建交给Spring来处理。

Spring Bean的作用域:

Bean作用域

作用域 描述

singleton

在每个Spring IoC容器中一个bean定义对应一个对象实例。

prototype

一个bean定义对应多个对象实例。

request

在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例, 它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。

session

在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。

global session

在一个全局的HTTP Session中,一个bean定义对应一个实例。典型情况下,仅在使用portlet context的时候有效。该作用域仅在基于web的Spring ApplicationContext情形下有效。

继续之前先回顾一下==与equals的用法

我们说==比较的是两个对象的地址,而equals比较的是两个对象的内容。所以

	String s1 = new String("sfsf");
	String s2 = new String("sfsf");
	System.out.println(s1==s2);
	System.out.println(s1.equals(s2));

 上面打印的结果为:

false
true

 

说明:s1,s2分别在栈中分配内存,即两个局部变量。那么他们的值存放的是地址。

new String的时候会在堆中分配对象的空间,显然调用了两次new分配了两个对象的地址。所以s1与s2的值即地址是不一样的,所以s1==s2返回为false.

而s1.equals(s2)比较的是两个对象的内容,显然对象内容都是sfsf所以返回为true.

 

相关知识如:

String s = new String("xyz");创建了几个String Object

  答:"xyz"本身作为字符常量,在汇编语言中应该作为常量放在数据段,Java有一个类似数据段的constant pool保存这个常量,在classloader加载这个类的时候就把"xyz"和这个类的其他一些信息放在constant   pool  new   String("xyz")根据常量"xyz"在heap上创建String对象所以,一共两个对象 
   String(String   original)  Initializes   a   newly   created   String   object   so   that   it   represents   the   same  sequence   of   characters   as   the   argument;   in   other   words,   the   newly   created   string   is   a   copy   of  the   argument   string.   

 

那么现在我们对从spring得到的对象进行测试。

 

测试代码:

package com.lwf.bean;

public class Bean1 {

}

 配置文件:

<?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:aop="http://www.springframework.org/schema/aop"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<bean id="bean1" class="com.lwf.bean.Bean1"></bean>
	
	
</beans>

 测试类:

package com.lwf.client;

import junit.framework.TestCase;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lwf.bean.Bean1;


public class Client extends TestCase {
	public void testScope() {
	
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext*.xml");
		Bean1 bean1 = (Bean1)ctx.getBean("bean1");
		Bean1 bean2 = (Bean1)ctx.getBean("bean1");
		System.out.println(bean1==bean2);
		System.out.println(bean1.equals(bean2));
		
		
	}
}

 

运行结果:

2010-05-19 14:34:07,419 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c78e57: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1c78e57]; startup date [Wed May 19 14:34:07 CST 2010]; root of context hierarchy
2010-05-19 14:34:07,654 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from file [D:\workdirlocal\SpringBean\bin\applicationContext.xml]
2010-05-19 14:34:08,076 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1c78e57]: org.springframework.beans.factory.support.DefaultListableBeanFactory@80fa6f
2010-05-19 14:34:08,123 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@80fa6f: defining beans [bean1]; root of factory hierarchy
true
true

 

测试发现bean1==bean2,bean1.equals(bean2)均返回为true。这说明默认的情况下,Spring采用的是单例Singleton模式创建对象,一旦创建,以后每次只是把引用返回。所以所有返回的对象都是同一个。

 

这个在配置文件中是可以修改的,如我们在bean的配置上增加scope属性:

<bean id="bean1" class="com.lwf.bean.Bean1" scope="prototype"></bean>

 

测试上面代码:

结果为:

2010-05-19 16:05:47,353 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c78e57: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1c78e57]; startup date [Wed May 19 16:05:47 CST 2010]; root of context hierarchy
2010-05-19 16:05:47,524 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from file [D:\workdirlocal\SpringBean\bin\applicationContext.xml]
2010-05-19 16:05:47,899 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1c78e57]: org.springframework.beans.factory.support.DefaultListableBeanFactory@80fa6f
2010-05-19 16:05:47,931 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@80fa6f: defining beans [bean1]; root of factory hierarchy
false
false

 

可以看到返回为false,false即每次创建的实例是不同的。我们在使用这一特性解决struts action单实例时就要这样配置。

 

Prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)时都会创建一个新的bean实例。根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。