LoginSignup
24
24

More than 5 years have passed since last update.

Spring Bootで遊んでみる

Last updated at Posted at 2014-09-01

対象

SpringFrameworkに興味があるけど、敷居が高いと思ってる人。

環境

Java8
maven3.x
Eclipse4.4(m2e導入済み)

Spring Bootとは

SpringMVCをつかったWebアプリを「簡単に」とても手早く制作できる仕組み。

やってみる

ソース

  • EclipseでJava/Mavenプロジェクトの作成
    社内の人に聞かれたので、追記。

    • 新規プロジェクトでJavaProjectを選択し、必要な入力を行い作成。
    • 作成したプロジェクトで右クリック→Configure→Convert to Maven Project
  • Spring Boot対応のpom.xmlを作成

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.justsystems</groupId>
    <artifactId>spring-boot-hello</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.5.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compile.source>1.8</maven.compile.source>
        <maven.compile.target>1.8</maven.compile.target>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • コントローラクラスを作成
    • @Controller - SpringにControllerと識別させる。
    • @RequestMapping("/hello") - Webリソースのマッピングを行う。
    • @ResponseBody - メソッドの返り値がそのままレスポンスボディとする。(この場合文字列。)
com.justsystems.springboot.sample.controller.HelloController
package com.justsystems.springboot.sample.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        return "hello.";
    }
}
  • 実行クラスを作成
com.justsystems.springboot.sample.Main
package com.justsystems.springboot.sample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
@EnableAutoConfiguration
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

実装こんだけ。

動かす

ビルド

eclipseでプロジェクトを右クリック→Run As→Maven install

実行

eclipseでプロジェクトを右クリック→Run As→Java Application→com.justsystems.springboot.sample.Mainを実行

起動ログ


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.1.5.RELEASE)

中略

2014-09-01 16:57:10.847  INFO 3220 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2014-09-01 16:57:10.899  INFO 3220 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http
2014-09-01 16:57:10.900  INFO 3220 --- [           main] com.justsystems.springboot.sample.Main   : Started Main in 2.336 seconds (JVM running for 2.667)
2014-09-01 16:57:46.489  INFO 3220 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2014-09-01 16:57:46.490  INFO 3220 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2014-09-01 16:57:46.507  INFO 3220 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 15 ms

見る

hello

と表示されればOK

Spring Bootのライブラリのバージョンを上げたい場合

spring-boot-dependencieをいじることで、変更可能。
デフォルトがtomcat7系のため、サンプルでは8系に上げてある。

pom.xml@properties
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compile.source>1.8</maven.compile.source>
        <maven.compile.target>1.8</maven.compile.target>
        <java.version>1.8</java.version>
        <spring.version>4.0.6.RELEASE</spring.version>
        <tomcat.version>8.0.11</tomcat.version>
    <properties>
24
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
24