この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
はじめに
Javaを学習するのにフレームワークを使ってみると良いとの助言を頂いたので、 Spring Bootを使ってみました。 今回は導入編です。
環境
OS : Mac OSX 10.9.5 IDE : Eclipse Java : 8 Update 91
準備
EclipseからSpring Bootが使える様に、必要なものをインストールします。
「help」->「Eclipse Marketplace」を選択してマーケットプレイスを開きます。
検索してSpring Bootをダウンロード。
Gradle(STS)もダウンロードしました。
Spring Starter Projectから始めてみる。
Eclipseの、File -> New -> Projectからウィザードを開き、spring starter projectを選択してNextを押します。
プロジェクト名、Javaのバージョンなどを設定にしてNextを押します。
Typeには今回はGradle(STS)を選択します。
ここでテンプレートを選択できますが、今回はそのままFinishを押します。
この時、エクスプローラーのプロジェクトの箇所に、赤いビックリマークが出てたりします。
JREが重複して登録されているのが原因の様なので、
Build Path -> Configure Build Pathを開いてJava Build Pathを選択。
該当するJREを2つ削除してOKを押して閉じ、
もう一度開いて登録すると治ります。
JRE System Libraryを選択。
使用したいバージョンのJavaを選択。
コードを書いてみる。
設定を記述したbuild.gradleはこんな感じです。
buildscript {
ext {
springBootVersion = '1.3.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
jar {
baseName = 'demo'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
とりあえず、"hello"。
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SbDemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext cac = SpringApplication.run(SbDemoApplication.class, args);
SbDemoApplication app = cac.getBean(SbDemoApplication.class);
app.hello();
}
public void hello() {
System.out.println("hello()");
}
}
7行目、@SpringBootApplicationでSpring Bootの設定を自動で行います。 11行目、Spring Bootを走らせています。 SpringApplication.runの第二引数は「コマンドラインの引数」となります。
実行すると...
hello()
でました。
ブラウザでhello。
build.gradleの30行目を下記の様に変更します。
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
変更を反映させるために、エクスプローラーのプロジェクト名を右クリックして、 Gradle(STS) -> Refresh Dependenciesを選択してください。 これでもろもろがダウンロードされ、web用のアノテーションが使用できる様になります。
次は新しくクラスを作成します。
package com.example;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WebController {
@RequestMapping("/web")
public String hello() {
return "hello()";
}
}
9行目、@RequestMapping()の引数に"/web"を設定していますが、> これはブラウザからアクセスするアドレスになります。
まず、実行してみます。 次にブラウザでこのURLを実行してみます。 http://localhost:8080/web
hello()
ブラウザに表示されました。
さいごに
導入から簡単な実行まで進めてみました。 次回は、もう少しアノテーションを増やした形で紹介したいと思います。