Spring Beanの役割領域:SpringでStrutsのアクションの一例問題を解決します.


私たちはStrutsでアクションがmapから取り出したのを知っています.一例です.マルチスレッドの呼び出し時に問題が発生します.
Springでは、getBenメソッドで毎回呼び出しますと、springはnewという例を示してくれますので、この点を利用してStrutsのactionの作成をSpringに渡して処理してもいいです.
Spring Beanの役割領域:
ビーンスコープ
スコープの説明
シングルトン
各Spring IoC容器におけるbeanは、対応するオブジェクトの例を定義する.
プロトタイプ
1つのbeanは、対応する複数のオブジェクトのインスタンスを定義する.
request
一度のHTTP要求では、1つのbeanは、対応する一例を定義する.つまり、HTTP要求毎に、それぞれのbeanインスタンスがあり、それらはあるbean定義に基づいて作成される.この作用領域は、ウェブベースのSpring ApplicationContextの場合にのみ有効である.
セッション
1つのHTTP Sessionにおいて、1つのbeanは対応する一例を定義する.この作用領域は、ウェブベースのSpring ApplicationContextの場合にのみ有効である.
global session
グローバルHTTP Sessionにおいて、1つのbeanは対応する一例を定義する.典型的には、ポートcontextを使用する場合にのみ有効です.この作用領域は、ウェブベースのSpring ApplicationContextの場合にのみ有効である.
続ける前にちょっと振り返ってみます.=equalsとの使い方です.
私たちは、=比較するのは2つのオブジェクトのアドレスで、equals比較するのは2つのオブジェクトの内容です.だから
	String s1 = new String("sfsf");
	String s2 = new String("sfsf");
	System.out.println(s1==s2);
	System.out.println(s1.equals(s2));
 上の印刷の結果は、
false
true
 
説明:s 1,s 2はそれぞれスタックにメモリを割り当て、すなわち2つの局所変数です.彼らの値は住所です.
new Stringの場合は、スタックにオブジェクトを割り当てる空間があり、2回のnewを呼び出して2つのオブジェクトのアドレスを割り当てたことが明らかになっています.したがって、s 1とs 2の値であるアドレスは異なるので、s 1==s 2はfalseに戻ります.
s 1.equals(s 2)は2つのオブジェクトの内容を比較します.明らかにオブジェクトの内容はすべて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
 
テストでは、bean 1==bean 2、bean 1.equals(bean 2)はいずれも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   。