BeanFactoryの詳細(Spring1.2.1)
今まで,BeanFactoryと言えば,作成してgetBeanメソッド呼び出しをするだけだったが,他のBeanFactoryのメソッドにも触れてみる.
BeanFacotroyはInterfaceであり,以下の六つのメソッドを持つ.
- boolean containsBean(String)
- 指定したIDのbeanが設定ファイルにて定義されているか否かを返す.
- Object getBean(String)
- 指定したIDのbeanを返す. 今まで特に注目してきた機能のインタフェースとなるメソッドである. 設定ファイルにてsingleton指定しているか否かで毎回newするか否かが決まる. 指定したIDのbeanが無ければNoSuchBeanDefinitionExceptionが投げられる.
- Object getBean(String,Class)
- 上記getBean(String)の型指定追加版である. 指定IDで定義されているbeanの型が第二引数のClassとあわないと,BeanNotOfRequiredTypeExceptionが投げられる.
- boolean isSingleton(String)
- 指定したIDのbeanがsingletonか否か(設定ファイルにて指定)を返す. 指定したIDのbeanが設定にないと,NoSuchBeanDefinitionExceptionが投げられる.
- String[] getAliases(String)
- 指定したIDのbeanの別名(alias)を返す. 指定したIDのbeanが設定にないとやはりNoSuchBeanDefinitionExceptionが投げられる.
- Class getType(String name)
- 指定したIDのbeanの別名(alias)を返す. 指定したIDのbeanが設定にないとやはりNoSuchBeanDefinitionExceptionが投げられる.
- 指定したIDのbeanのClassを返す. 指定したIDのbeanが設定にないとNoSuchBeanDefinitionExceptionが投げられる.
設定ファイルにて要素beanの属性classにFactoryクラスを指定し,さらにFactoryメソッドを指定してbeanを取得する方法を以前に示した.
<bean id="sample3" class="org.fireproject.springsample.SampleBeanFactory"
factory-method="createSampleBean"/>
上記設定により,
factory.getBean("sample3");
などとすると,SampleBeanFactory#createSampleBeanの返り値が得られる.
さて,このbeanのIDの先頭に&を付加すると,beanのFactory,上の例で言うとSampleBeanFactoryを取得することができる.
ただし,FactoryとなるクラスがFactoryBeanを実装していないと,BeanIsNotAFactoryExceptionが投げら得れる.
以上の内容を確認するサンプルを作成した.
IDがsample1のbeanはsingletonでaliasが一つ,sample2のbeanは非singletonでaliasが二つ,sample3はFactoryクラスにて生成される.
これらのIDを指定して,BeanFactoryのいろいろなメソッドを呼んでみた.
実行結果を以下に示す.
springsample.HelloBeanFactory:33 - ================================ springsample.HelloBeanFactory:35 - containsBean(sample1) = true support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'sample1' springsample.HelloBeanFactory:37 - getBean(sample1) = org.fireproject.springsample.SampleBean@fec107 springsample.HelloBeanFactory:38 - getBean(sample1, class org.fireproject.springsample.SampleBean) = org.fireproject.springsample.SampleBean@fec107 springsample.HelloBeanFactory:43 - org.springframework.beans.factory.BeanNotOfRequiredTypeException springsample.HelloBeanFactory:47 - isSingleton(sample1) = true springsample.HelloBeanFactory:54 - getAliases(sample1) = String[1] springsample.HelloBeanFactory:56 - sample1alias springsample.HelloBeanFactory:63 - getType(sample1) = class org.fireproject.springsample.SampleBean springsample.HelloBeanFactory:68 - ================================ springsample.HelloBeanFactory:35 - containsBean(sample2) = true springsample.HelloBeanFactory:37 - getBean(sample2) = org.fireproject.springsample.SampleBean@64f6cd springsample.HelloBeanFactory:38 - getBean(sample2, class org.fireproject.springsample.SampleBean) = org.fireproject.springsample.SampleBean@872380 springsample.HelloBeanFactory:43 - org.springframework.beans.factory.BeanNotOfRequiredTypeException springsample.HelloBeanFactory:47 - isSingleton(sample2) = false springsample.HelloBeanFactory:54 - getAliases(sample2) = String[2] springsample.HelloBeanFactory:56 - sample2alias2 springsample.HelloBeanFactory:56 - sample2alias1 springsample.HelloBeanFactory:63 - getType(sample2) = class org.fireproject.springsample.SampleBean springsample.HelloBeanFactory:68 - ================================ springsample.HelloBeanFactory:35 - containsBean(notexists) = false springsample.HelloBeanFactory:41 - org.springframework.beans.factory.NoSuchBeanDefinitionException springsample.HelloBeanFactory:49 - org.springframework.beans.factory.NoSuchBeanDefinitionException springsample.HelloBeanFactory:59 - org.springframework.beans.factory.NoSuchBeanDefinitionException springsample.HelloBeanFactory:65 - org.springframework.beans.factory.NoSuchBeanDefinitionException springsample.HelloBeanFactory:68 - ================================ springsample.HelloBeanFactory:76 - org.springframework.beans.factory.BeanIsNotAFactoryException support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'sample3' springsample.HelloBeanFactory:79 - sample3 = org.fireproject.springsample.SampleBean@2d9c06 springsample.HelloBeanFactory:82 - org.springframework.beans.factory.BeanIsNotAFactoryExceptionFactoryの取得は今回のサンプルではFactoryBeanを実装していないので,BeanIsNotAFactoryExceptionが投げられている.

