Java/SpringFramework/QuickStart
1月 1, 2003 · Posted in Java, SpringFramework
※このページは、むかしのやりかたで記述されています。
※最新のは、こっち
1.2方式。
バイナリは2.0でも2.5でも大丈夫。
WEB環境(J2EE, JEE)は不要。Javaの実行環境だけあれば○
1.環境構築
SpringFrameworkを入手
公式サイトのDownloadより、バイナリファイルを入手。
http://www.springframework.org/
利用ライブラリ格納
内包されているspring.jarをクラスパス配下に置く(commons-loggingも必要かも)。
2.ビーン(POJO)の作成
Job
package bean;
public class Job {
private String job;
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
適当なパッケージに置く。
Person
package bean;
public class Person {
private String name;
private Job job;
public Job getJob() {
return job;
}
public void setJob(Job job) {
this.job = job;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
同じく適当なパッケージに置く。
3.定義ファイルの作成
beans.xmlの作成
beans.xmlというファイルに以下の内容を記述して、任意のディレクトリに置く
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "<a href="http://www.springframework.org/dtd/spring-beans.dtd">" target="_blank">http://www.springframework.org/dtd/spring-beans.dtd"></a> <beans> <bean id="person" class="bean.Person"> <property name="name"> <value>Bush</value> </property> <property name="job"> <ref bean="secretary"/> </property> </bean> <bean id="secretary" class="bean.Job"> <property name="job"> <value>secretary</value> </property> </bean> </beans>
4.実行ファイルの作成
Main
以下のクラスを作成して任意の位置に保存。
できたら実行。
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import bean.Person;
public class Main {
public static void main(String[] args) throws Exception {
ApplicationContext appContext = new FileSystemXmlApplicationContext("beans.xml");
Person person = (Person)appContext.getBean("person");
System.out.println("name:" + person.getName());
System.out.println("job:" + person.getJob().getJob());
}
}
実行結果
name:Bush
job:secretary
job:secretary
5.まとめ
セッターインジェクションの完成!
NoSQLの本
関連エントリー
- Spring Framework
- Java/SpringFramework/メッセージソースの利用
- Commons Validator – 利用方法・サンプル
- Java/SpringFramework/QuickStart2
- Apache Struts – とりあえずメモ
Comments
Leave a Reply

