2
2

More than 1 year has passed since last update.

SpringBootのお勉強

Posted at

概要

以前、業務でSpring Bootを触る機会があったため、社内wikiに備忘録的にメモを残していました。
オープンな場に残しておこうと思い、ここに書いておきます。

Spring Bootとは

  • Spring Framework一族のひとり
  • 簡単な設定で使えるSpring
  • Webコンテナを内包する
    • Webコンテナをjarに含めるので、jar単体でWebアプリとして成立する(スタンドアロン)

環境構築

  • Eclipse or IntelliJ IDEA Community EditionにSTS(Spring tool suite)のプラグインを入れる
  • Spring Starter Projectを作成する
    • ウィザードに従う
      • とりあえずweb -> Spring webを入れておけばおk

実践

AutoConfigure

  • Bean定義自動設定
    • @SpringBootApplication
      • ↑のアノテーションに@SpringBootConfigurationが付与、それに@Configurationが付与されている
    • @Configuration + @EnableAutoConfiguration + @ComponentScan
    • @EnableAutoConfigurationを付与することで自動設定が動く
      • org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector.classの設定による

Unit Test

  • @SpringBootTest

    • ApplicationContextを簡単操作
  • @TestConfiguration

    • カスタムコンフィグ
  • @MockBean

    • MockをDIできる
  • Web Environment test

    • Web環境を構成した上でテスト可能になる
  • SpringBootTestの使用について

    • 遅いからUnit Testに向かない
      • -> テスト実行時の都度Spring Bootの起動が必要だから
      • Mockitoを使おう
      • ロジックのテストではなくユースケースのテストを書くなら有用

Actuator

  • システムの状態を監視
  • HTTPエンドポイントで管理できる

WebAPI用のControllerクラスを作成

  • @RestControllerをつける

    • @Controller + @ResponseBody
      • JSONやXML等を返す
    • @RequestMappingでUrlを設定
  • パラメーター

    • @PathVariableを使用
    • @RequetstMappingに{xxx}で@PathVariableでアノテートしたパラメータにバインドする
  • JavaBeanの返却

    • @RestControllerではJavaBeanをJSONにして返却する
    • HashMapも同じでJSONにして返却する
  • APIの呼び出し

    • RequestEntity(リクエストオブジェクト)
    • ResponseEntity(レスポンスオブジェクト)
    • RequestEntityをRestTemplateでJOSN化し、JSONで返ってきたデータをRestTemplateでオブジェクトに戻す
    • RestTemplate内にMessageConvertorがある
  • ファイル

    • org.springframework.core.io.FileSystemResourceを使用する
  • 例外ハンドラ

    • クラスに@RestControllerAdvice
      • @RestControllerAdvice->ResponseBodyを返却、@ControllerAdvice->パスを返却
    • ↑は例外ハンドラではなく、共通処理の実装方法
    • メソッドに@ExceptionHandler、引数は対象となる例外
  • Singleton

    • @Scopeでスコープを指定できる
    • 基本的にSingletonでOKなのかは疑問

セキュリティ

  • ErrorAttribute
    • DefaultErrorAttributesを継承してカスタムする

SpringBootのデメリット

  • 設定が楽=設定部分が隠蔽されているということだから、いざ自分でカスタムする必要が出た時のコストが高め?
    • 初学者はSpring MVCを先に学んだ方が良い
    • 自分で設定、制御したいマンには向かない
  • スタンドアローンだからモノリシックなシステムに合わない
  • 設計をミスると依存モジュールが膨大な数になる→起動時間の増加に繋がる(Spring MVCでも同じか)
  • 大部分は設定が楽になることに対するデメリット

参考

https://spring.io/projects/spring-boot
https://spring.pleiades.io/
https://qiita.com/sugaryo/items/5695bfcc21365f429767
https://qiita.com/kazuki43zoo/items/8645d9765edd11c6f1dd
https://dev.classmethod.jp/articles/spring-boot/
https://meetup-jp.toast.com/452
https://dawaan.com/mockbean-vs-mock/
https://www.springboottutorial.com/spring-boot-projects-with-code-examples
https://www.javaguides.net/2018/10/free-open-source-projects-using-spring-boot.html
https://dzone.com/articles/top-5-spring-boot-features-java-developers-should
https://www.educba.com/what-is-spring-boot/
https://stackoverflow.com/tags/spring-boot/info
https://www.java2novice.com/java_interview_questions/spring-boot-pros-and-cons/

2
2
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
2
2