daybreaksnow's diary

私を私と呼びたい

[Spring Framework]SpringFramework DIサンプル

  • 環境設定

spring-framework-2.0.8.zipを以下からダウンロード。*1
http://sourceforge.jp/projects/sfnet_springframework/releases/



以下をビルドパスに通すだけでよい。
・spring.jar(ダウンロードしてきたzipの中のもの)
・commons-logging.jar*2


サンプル

  • Beanクラス(springで自動でnewされるもの)(getter/setterは省略) *3

SampleBean.java

public class SampleBean {
 private String sampleString;
 private Long sampleLong;
 private BigDecimal sampleBigDecimal;
 private BigDecimal sampleBigDecimalNew;
 private List<String> sampleStringList;
 private SampleSubBean sampleSubBean;
 private SampleSubBean sampleSubBeanShare;
}

SampleSubBean.java

public class SampleSubBean {
 private String name;
}
  • springの設定ファイル(spring-conf.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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd ">

  <bean id="sampleBean"
    class="spring.sample.SampleBean">
    <!-- 文字列セット -->
    <property name="sampleString" value="hoge"/>

    <!-- longセット -->
    <property name="sampleLong" value="1"/>

    <!-- BigDecimalセット -->
    <property name="sampleBigDecimal" value="1.123456"/>

    <!-- BigDecimalセット(明示的にコンストラクタ呼び出し) -->
    <property name="sampleBigDecimalNew" >
     <bean class="java.math.BigDecimal" >
      <constructor-arg>
       <value>10.5</value>
      </constructor-arg>
     </bean>
    </property>

    <!-- リスト -->
    <property name="sampleStringList">
      <list>
       <value>piyo</value>
       <value>fuga</value>
      </list>
	</property>

	<!-- すでに作成してあるBeanを利用 -->
    <property name="sampleSubBeanShare" ref="sampleSubBeanShare"/>

	<!-- beanを作成してセット -->
    <property name="sampleSubBean" >
     <bean class="spring.sample.SampleSubBean">
       <property name="name" value="inner"/>
     </bean>
    </property>
  </bean>


  <bean id="sampleSubBeanShare" class="spring.sample.SampleSubBean">
    <property name="name" value="share" />
  </bean>
</beans>
  • mainクラス(SpringSample.java)
public class SpringSample {
  public static void main(String[] args) {
    ApplicationContext applicationContext = null;
    applicationContext = new ClassPathXmlApplicationContext( "spring-conf.xml");
    Object bean = applicationContext.getBean("sampleBean");
    Object subBeanShare = applicationContext.getBean("sampleSubBeanShare");
    System.out.println(bean);
    System.out.println(subBeanShare);
  }
}

toStringを適当にオーバーライドしてやれば値がセットされていることが分かる。

*1:現在の最新バージョンは3.2.1のようだが、仕事で使っているのが2.0.8のため、このバージョンを利用する。

*2:spring-framework-2.0.8.zip内にはcommons-logging.jarはないが、spring-framework-2.0.8-with-dependencies.zipにはある。ただしこのzipは60MB程度あるので、apache-commonsから落としてきた方がよいだろう。commons-logging-1.1.1で動作することを確認

*3:なお、publicなsetterがないと以下のエラーが発生する。Caused by: org.springframework.beans.InvalidPropertyException: Invalid property 'name' of bean class [spring.sample.SampleSubBean]: No property 'name' found