nameとalias(Spring1.2RC2)
BeanFacotryに対してbeanを指定するのに要素beanの属性id,nameを使用してきたが,nameには一度に複数の名前を指定可能らしい.また要素aliasを使用して文字通りbeanの別名を定義することが可能らしいので試してみた.
本頁のサンプルでは,idがhelloidである要素beanに三つの名前と二つの別名を指定する.
beans.xmlを以下に示す.
ポイントは以下.
- 要素beanの属性name
- 属性nameの値には,カンマ","あるいはセミコロン";"区切りで複数指定することができる.
name="hello,helloname;helloname2"
これによりBeanFacotryからは"hello","helloname","helloname2"を指定することによってid="helloid"のbeanを取得できる. - 要素alias
- beanの別名を指定する.
属性nameには別名設定するbeanのidを指定する.
そして属性aliasに別名を指定する.
<alias name="helloid" alias="helloalias1"/> <alias name="helloid" alias="helloalias2"/>
これによりBeanFacotryからは"helloalias1","helloalias2"を指定することによってid="helloid"のbeanを取得できる.
サンプルのドライバクラスHelloBeanFactoryを以下に示す.
要素beanの属性nameの値(カンマやセミコロンで分割した値)や要素aliasの属性aliasの値を指定することで,BeanFactoryからbeanを取得することができる.
String[] helloBeanKeys = new String[] {
"helloalias1",
"helloalias2",
"hello",
"helloname",
"helloname2"
};
for (int i = 0; i < helloBeanKeys.length; i++) {
logger.info("====== get HelloBean by " + helloBeanKeys[i]);
HelloBean helloBeanByName = (HelloBean)factory.getBean(helloBeanKeys[i]);
logger.info(helloBeanByName);
}
実行すると以下のログがでた.
2005-05-07 15:21:23,574 INFO [main] xml.XmlBeanDefinitionReader:132 - Loading XML bean definitions from URL [jar:file:/home/matsu/programing/java/spring/dicontainer4/springsample.jar !/org/fireproject/springsample/beans.xml] 2005-05-07 15:21:23,657 INFO [main] springsample.HelloBeanFactory:40 - ====== get HelloBean by helloalias1 2005-05-07 15:21:23,661 INFO [main] support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'helloid' 2005-05-07 15:21:23,662 INFO [main] support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'myBeanFactory' 2005-05-07 15:21:23,782 INFO [main] springsample.MyBeanFactory:14 - ===== createHelloBean start 2005-05-07 15:21:23,795 INFO [main] springsample.HelloBeanFactory:42 - org.fireproject.springsample.HelloBean [constractDate=1115446883784;factoryName=myBeanFactory1;messge=Hello Bean!!!!;value=1;] 2005-05-07 15:21:23,796 INFO [main] springsample.HelloBeanFactory:40 - ====== get HelloBean by helloalias2 2005-05-07 15:21:23,797 INFO [main] springsample.HelloBeanFactory:42 - org.fireproject.springsample.HelloBean [constractDate=1115446883784;factoryName=myBeanFactory1;messge=Hello Bean!!!!;value=1;] 2005-05-07 15:21:23,797 INFO [main] springsample.HelloBeanFactory:40 - ====== get HelloBean by hello 2005-05-07 15:21:23,797 INFO [main] springsample.HelloBeanFactory:42 - org.fireproject.springsample.HelloBean [constractDate=1115446883784;factoryName=myBeanFactory1;messge=Hello Bean!!!!;value=1;] 2005-05-07 15:21:23,798 INFO [main] springsample.HelloBeanFactory:40 - ====== get HelloBean by helloname 2005-05-07 15:21:23,798 INFO [main] springsample.HelloBeanFactory:42 - org.fireproject.springsample.HelloBean [constractDate=1115446883784;factoryName=myBeanFactory1;messge=Hello Bean!!!!;value=1;] 2005-05-07 15:21:23,798 INFO [main] springsample.HelloBeanFactory:40 - ====== get HelloBean by helloname2 2005-05-07 15:21:23,799 INFO [main] springsample.HelloBeanFactory:42 - org.fireproject.springsample.HelloBean [constractDate=1115446883784;factoryName=myBeanFactory1;messge=Hello Bean!!!!;value=1;]nameやaliasによる指定はidで指定することの代替なので,取得するオブジェクトとしては同一(デフォルトなのでsingletonモードになっている)であることに注意.

