Spring Bootによるマルチモジュールプロジェクト

Spring Bootを使用したマルチモジュールプロジェクト

1. 概要

このクイックチュートリアルでは、how to create a multi-module project with Spring Bootを示します。

まず、アプリケーション自体ではないライブラリjarを作成し、次にライブラリを使用するアプリケーションを作成します。

Spring Bootの概要については、this articleを参照してください。

2. セットアップ

マルチモジュールプロジェクトを設定するには、Maven構成でcreate a simple module using pom packaging to aggregate our library and application modulesを使用します。

org.example
parent-multi-module
pom

プロジェクト内に2つのディレクトリを作成し、アプリケーションモジュールをライブラリjarモジュールから分割します。

モジュールをpom.xmlで宣言しましょう:


    library
    application

3. ライブラリージャー

libraryモジュールには、jarパッケージを使用します。

com.baledung.example
library
jar

take advantage of Spring Boot dependency managementにしたいので、spring-boot-starter-parent を親プロジェクトとして使用し、Mavenがリポジトリから親pom.xmlを解決するようにset <relativePath/> to an empty valueに注意します。


    org.springframework.boot
    spring-boot-starter-parent
    2.0.2.RELEASE
    

pom.xml<dependencyManagement/>セクションのif we have our own parent project, we can instead import the dependency management as a Bill of Materials (BOM)に注意してください。


    
        
            org.springframework.boot
            spring-boot-dependencies
            pom
            2.0.2.RELEASE
            import
        
    

最後に、最初の依存関係は非常に単純になります。


    
        org.springframework.boot
        spring-boot-starter
    

このモジュールでは、ライブラリに必要のないSpring Boot plugin isn’t necessary because the main function of it is to create an executable*über-jar*,を使用します。

その後、develop a service component that will be provided by the libraryの準備が整います。

@Service
public class EvenOddService {

    public String isEvenOrOdd(Integer number) {
        return number % 2 == 0 ? "Even" : "Odd";
    }
}

4. アプリケーションプロジェクト

libraryモジュールと同様に、アプリケーションモジュールはjarパッケージを使用します。

com.example.example
application
jar

そして、以前と同じようにSpring Bootの依存関係管理を利用します。


    org.springframework.boot
    spring-boot-starter-parent
    2.0.2.RELEASE
    

Spring Bootスターターの依存関係に加えて、include our library jar created in the previous section


    
        org.springframework.boot
        spring-boot-starter-web
    
    
        com.example.example
        library
        ${project.version}
    

最後に、use the Spring Boot plugin:


    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    

この場所で上記のプラグインを使用するいくつかの便利な理由があります。

まず、Spring Bootの依存関係に一致するようにバージョン番号を設定する組み込みの依存関係リゾルバーを提供します。

次に、メインメソッドを検索して、実行可能クラスとしてフラグを立てます。

最後に、おそらく最も重要なこととして、classpathのすべてのjarsを収集し、単一の実行可能なüber-jarを構築します。

これで、アプリケーションクラスを記述して要点に進む準備ができたので、implement a controller inside the main application class

@SpringBootApplication(scanBasePackages = "com.example")
@RestController
public class EvenOddApplication {

    private EvenOddService evenOddService;

    // constructor

    @GetMapping("/validate/")
    public String isEvenOrOdd(
      @RequestParam("number") Integer number) {
        return evenOddService.isEvenOrOdd(number);
    }

    public static void main(String[] args) {
        SpringApplication.run(EvenOddApplication.class, args);
    }
}

5. 結論

この記事では、マルチモジュールプロジェクトを実装および構成し、Spring Bootを使用して独自にライブラリjarを構築する方法について説明しました。

いつものように、コードサンプルはover on GitHubで見つけることができます。