2013年5月2日木曜日

JAVA Spring 3.2.2を使ってみた

Springの使い方について、調べてみた。


今回の環境は、
Java 1.6系
Spring 3.2.2
spring-context-3.2.2.RELEASE.jar
spring-context-support-3.2.2.RELEASE.jar
spring-core-3.2.2.RELEASE.jar
spring-beans-3.2.2.RELEASE.jar
spring-expression-3.2.2.RELEASE.jar
Apache Commons
commons-logging-1.1.3.jar

今回作成するもの
実行用クラスとBeanクラス、Spring用設定XMLの3種類です。
今回のサンプルでは、Spring用設定XMLファイルを配置する場所にクラスパスを通してください。

実行用クラスのソース(SpringTest.java)

package sample;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class SpringTest  {
   public static void main(String args[]){
      // アプリケーションコンテキストの読み込み
      ApplicationContext contxt = new ClassPathXmlApplicationContext("applicationContext.xml");
      
      // Beanを取得
      SampleBean bean = (SampleBean)contxt.getBean("target");
      
      // 標準出力へ
      System.out.println(bean.getMessage());
   }
}


Beanクラスのソース(SampleBean.java)

package sample;

public class SampleBean {
 // コンストラクタ
 public SampleBean() {
 }

 /**
  * メッセージ
  */
 private String message;

 /**
  * メッセージ取得メソッド
  * @return
  */
 public String getMessage() {
  return this.message;
 }

 /**
  * メッセージ設定メソッド
  * @param message
  */
 public void setMessage(String message) {
  this.message = message;
 }
}


SQLMapperXMLファイル(User.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
   <bean id="target" class="sample.SampleBean" >
      <property name="message" >
         <value>Hello World!</value>
      </property>
   </bean>
</beans>


今回作成したものは、簡単なサンプルです。

0 件のコメント:

コメントを投稿