English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SpringBoot マルチモジュールプロジェクト

Multi-モジュールプロジェクト

ネストされたMavenプロジェクトを含むSpring Bootプロジェクトは、 マルチモジュールプロジェクト。マルチモジュールプロジェクトでは、親プロジェクトはベースのMaven設定のコンテナとして機能します。

言い換えれば、 マルチモジュールプロジェクトは、管理する一連の子モジュールの親POMから構築されています。または、 マルチモジュールプロジェクト親POMから参照されている1つまたは複数の子モジュールで定義されます。

親Mavenプロジェクトは、以下を含む必要があります pom のパッケージ型により、このプロジェクトはアグリゲーションプロジェクトとなります。親プロジェクトの pom.xml ファイルには子プロジェクトが継承するすべてが含まれています モジュール、共有依存関係および 属性リストです。親POMはプロジェクトのルートディレクトリにあります。子モジュールは実際のSpring Bootプロジェクトであり、それらは親プロジェクトからMaven属性を継承しています。

多模块プロジェクトを実行するとき,すべてのモジュールがエンブリードしたTomcat Server上で同時にデプロイされます。また、単一のモジュールをデプロイすることもできます。

親POM

親POMが定義しています グループID、アーティファクトID、バージョン、および packaging。以前のMavenプロジェクトでは、親POMがパッケージングを定義していることを見ました。 jar。。しかし、多模块プロジェクトでは、親が POM パッケージングpomを定義します。パッケージングpomは他のMavenプロジェクトを参照しています。

なぜ多模块プロジェクトが必要なのか

プロジェクトを複数のモジュールに分けることは,役に立つかつ簡単にメンテナンスできます。私たちはプロジェクト内のモジュールを簡単に編集または削除することもできますが,他のモジュールに影響を与えません。モジュールをそれぞれでデプロイする必要がある場合に非常に役立ちます。

私たちは親pomの中ですべての依存関係を指定するだけで十分です。すべての他のモジュールは同じpomを共有しているため,各モジュールで同じ依存関係を指定する必要はありません。

サブモジュール-ear,warおよびjar

サブモジュールはどのようなプロジェクトでも,どのようなパッケージでもできます。私たちは自由にモジュールと束間でどのようなタイプの依存関係でも作成できます。

例えば、私たちは以下のように作成しています EAR (企業アーカイブ), WAR (Web ARchive)および JAR (Java ARchive)ファイル。JARファイルはWARファイルにバンドルされ,WARファイルはEARファイルにバンドルされます。EARファイルはアプリケーションサーバーにデプロイできる最終ソフトウェアパッケージです。

EARファイルには1つ以上のWARファイルが含まれています。各WARファイルにはサービスプロジェクトが含まれており,それはJARファイル内のすべてのWARファイルおよびパッケージ形式に共通するコードを持っています。

Mavenサブプロジェクト/サード

サブモジュールは独立したmavenプロジェクトであり,親プロジェクトの属性を共有しています。 すべてのサブプロジェクトは一つのコマンドでビルドできます,なぜならそれは親プロジェクトに位置しているからです。 プロジェクト間の関係を定義するのが簡単です。

多模块项目目录结构

让我们了解多模块项目目录结构。

在下图中,我们创建了一个名为 spring-boot-multi-module-project 的项目。它在目录底部包含父 pom 。之后,我们创建了两个 Mavenモジュール,分别命名为 module1 および module2 。这两个模块包含自己的pom文件。

让我们打开父POM,并查看在项目中创建Maven模块时其配置。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.w3codebox</groupId>
<artifactId>spring-boot-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-multi-module-project</name>
<description>デモプロジェクト for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> 
<dependency>
 <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework/groupId>
<artifactId>spring-webmvc/artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</<scope>runtime<
<exclusions>
<exclusion>
<groupId>org.junit.vintage/groupId>
<artifactId>junit-vintage-engine/artifactId>
</exclusion
</exclusions
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones/id>
<name>Spring Milestones/name>
<url>https://repo.spring.io/milestone/url>
</repository
<repository>
<id>spring-snapshots/id>
<name>Spring Snapshots/name>
<url>https://repo.spring.io/snapshot/url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository
</repositories
<pluginRepositories>
<pluginRepository>
<id>spring-milestones/id>
<name>Spring Milestones/name>
<url>https://repo.spring.io/milestone/url>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots/id>
<name>Spring Snapshots/name>
<url>https://repo.spring.io/snapshot/url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
</project>

上記のpomファイルは前の例と同じですが、この場合、 pom ファイルの中で注意すべきことは以下の2点です: パッケージおよび モジュール

マルチモジュールプロジェクトを作成する場合、親pomファイルでパッケージpomを設定する必要がありますが、 jar。

<packaging>pom</packaging>

プロジェクト内でMavenモジュールを作成する際には、Spring Bootは module タグ内の親pomに自動的に設定されたモジュールを以下のように示します。

<modules>
<module>module1</module>
<module>module2</module>
</modules>

今、以下を確認します module1のpomファイルの内容です。

pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.w3codebox</groupId>
<artifactId>spring-boot-multi-module-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.w3codebox</groupId>
<artifactId>module1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>module1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</<scope>runtime<
</dependency>
</dependencies>
</project>

ここでは注意すべきことは、上述のpomファイルには以下のようなものが含まれていないことです starter-web、web-MVCなどなどの一般的な依存関係を含むもの 親POM。

Spring Bootマルチモジュールプロジェクトのサンプル

マルチモジュールアプリケーションのサンプルを作成しましょう。

以下の例では、以下の名前の spring-boot-multimoduleのMavenプロジェクト。これは主要なアプリケーションです。メインアプリケーションでは、以下を作成しました五つの以下のように、以下のモジュール アプリケーション モデル リポジトリ 上記のすべてのファイルを作成した後、-api 上記のすべてのファイルを作成した後、-service

アプリケーションモジュール

アプリケーションモジュールはプロジェクトの主要なモジュールです。その中で、Spring Boot Applicationを実行するために必要なmainメソッドが定義されています。さらに、以下も含まれています アプリケーションの設定属性、コントローラ、ビューおよび リソース。

アプリケーションモジュールはモデルモジュール、サービス実装モジュール(モデルを含む依存関係モジュールとして)、リポジトリモジュール、サービスAPIモジュールを含んでいます。

モデルモジュール

モデルモジュールには エンティティプロジェクトで使用している ビジュアルオブジェクト

リポジトリモジュール

リポジトリモジュールには <プロジェクトで使用しているstrong>  リポジトリ。モデルモジュールに依存しています。

サービスAPIモジュール

サービスAPIモジュールにはすべてのプロジェクト サービス。モデルモジュールにも依存しています。

サービス実装モジュール

サービス実装モジュールはサービスを実装できます。それはリポジトリモジュールとサービスAPIモジュールに依存しています。

POMアグリゲータ(親POM)

親POMには、すべてのアプリケーションモジュールが含まれており、これらのモジュールが必要なすべての一般的な依存関係と属性も含まれています。プロジェクトがSpring IO Platformを親として定義しているため、依存関係にはバージョンが定義されていません。

私たちが作成したマルチモジュールアプリケーションの構造を理解しましょう。

Spring-boot-multimodule  
│── pom.xml  
│   └── REDME.adoc  
├── application  
│── pom.xml  
│── src  
│── main  
│           ├── java  
│           │   └── sample  
│           │       └── multimodule  
│           │           ├── SampleWebJspApplication.java  
│           │           └── web  
│           │               └── WelcomeController.java  
│── resources  
│── application.properties  
│── templates  
│── welcome  
│── show.html  
├── model  
│── pom.xml  
│── src  
│── main  
│── java  
│── sample  
│── multimodule  
│── domain  
│── entity  
│── Account.java  
|  
├── repository  
│── pom.xml  
│── src  
│── main  
│── java  
│── sample  
│── multimodule  
│── repository  
│── AccountRepository.java  
├── service-api  
│── pom.xml  
│── src  
│── main  
│── java  
│── sample  
│── multimodule  
│── service  
│── api  
│── AccountNotFoundException.java  
│── AccountService.java  
└── service-service  
    │── pom.xml  
    └── src  
        └── main  
            └── java  
                └── sample  
                    └── multimodule  
                        └── service  
                            └── impl  
                                └── AccountServiceImpl.java

ステップ1: 名前を spring-boot-マルチモジュールの Mavenプロジェクト

ステップ2: 開きます pom.xml (親pom)ファイルを、パッケージ形式に jar 変更する pom。  

pom.xml(親pom)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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>
<!-- Springio 平台は生成されたアプリケーションの親アプリケーションであり、Spring Boot 及びそのすべてのデフォルト設定を使用できます -->
<parent>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<groupId>sample.multimodule</groupId>
<artifactId>sample.multimodule</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Parent - Pom Aggregator</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Spring Boot 依存関係 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</<scope>runtime<
</dependency>
</dependencies>
</project>
 

上記のpomファイルでは注意すべき一つのことは、Mavenモジュールがまだ作成されていないため、設定されていません。今から上記のようにMavenモジュールを作成します。

ステップ3: 名前を   applicationの Mavenモジュール

ステップ4: <strong>   pom.xml アプリケーションモジュールを開きます   jar。

pom.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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>
    <parent>
        <groupId>sample.multimodule</groupId>
        <artifactId>sample.multimodule</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>sample.multimodule.application</artifactId>
    <packaging>jar</packaging>
    <name>Project Module - Application</name>
    <dependencies>
      <!-- <description>プロジェクトで使用されるすべてのリポジトリを含むモジュールです。Modelモジュールに依存します。< -->
      <dependency>
        <groupId>sample.multimodule</groupId>
        <artifactId>sample.multimodule.service.impl</artifactId>
        <version>${project.version}</version>
      </dependency>
      <!-- プロジェクトモジュール -->
          <dependency>
              <groupId>org.apache.tomcat.embed</groupId>
              <artifactId>tomcat-embed-jasper</artifactId>
              <scope>provided</<scope>runtime<
          </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
    
    </dependencies>
    
    <build>
        <plugins>
            <!-- Spring Boot プラグイン -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
 

ステップ5: 作成  主なクラスです。これは実行されるクラスです。

SampleWebJspApplication.java

package sample.multimodule;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.orm.jpa.EntityScan;
@SpringBootApplication
public class SampleWebJspApplication 
{
    public static void main(String[] args) throws Exception 
    {
        SpringApplication.run(SampleWebJspApplication.class, args);
    this.dummyType = dummyType;
this.dummyType = dummyType;
 

ステップ6: のパッケージ   smaple.multimodule.web。

以下に名称が   WelocameController 的Controllerクラス。  
  WelcomeController.java

package sample.multimodule.web;
import java.util.Date;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import sample.multimodule.domain.entity.Account;
import sample.multimodule.service.api.AccountService;
@Controller
public class WelcomeController 
{
    @Value("${application.message:Hello World}")
    private String message = "Hello World";
    @Autowired
    protected AccountService accountService;
    @RequestMapping("/")
    public String welcome(Map<String, Object> model) 
    {
        //嘗試獲得23號帳戶
        Account account = accountService.findOne("23");
        if(account == null){
            //如果創建帳戶時發生問題,請返回表示錯誤狀態的視圖
            model.put("message", "Error getting account!");
            model.put("account", "");
            return "welcome"}/show";
        this.dummyType = dummyType;  
        //返回表示23個帳戶信息的視圖
        String accountInfo = "Your account number is ".concat(account.getNumber());
        model.put("message", this.message);
        model.put("account", accountInfo);
        return "welcome"}/show";
    this.dummyType = dummyType;
    @RequestMapping("foo")
    public String foo(Map<String, Object> model) {
        throw new RuntimeException("Foo");
    this.dummyType = dummyType;
this.dummyType = dummyType;
 

第7手順: : フォルダー   src/main/resourceに以下の名前のファイルを作成します show.html HTML templates ->welcome。

show.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Spring Boot Multimodule</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
body>
    <div>
      <b>メッセージ: </b>
      <span th:text="${message}" />
    </div>
    <div>
      <b>あなたのアカウント: </b>
      <span th:text="${account}" />
    </div>
</body>
</html>
 

ステップ8: 開きます   application.properties ファイルを  application messageおよび  thymeleafキャッシュ設定は  いいえ。

application.properties

#アプリケーション情報
application.message = Hello User!
dummy.type = type-inside-the-war
#Spring Thymeleaf 設定
spring.thymeleaf.cache = false
 

上記のすべてのファイルを作成した後、アプリケーションモジュールのディレクトリ構造は以下の通りです:

次のモジュールを追加で作成します   model。

ステップ9: 以下のモジュールを作成します   Maven モジュール、名前を   model。

ステップ10: モデルを   pom.xml ファイルモジュールを開き、パッケージのタイプを   jar。

pom.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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>
    <parent>
        <groupId>sample.multimodule</groupId>
        <artifactId>sample.multimodule</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>sample.multimodule.model</artifactId>
    <packaging>jar</packaging>
    <name>Project Module - モデル</name>
    <description>このプロジェクトで使用するすべてのエンティティとビジュアルオブジェクトを含むモジュールです。依存関係はありません。 
    </description>
</project>
 

ステップ11: のパッケージ   sample.multimodule.domain.entity。

以下に名称が Account のクラス。

Account.java

package sample.multimodule.domain.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@Entity
public class Account 
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String number;
    private String type;
    private String creditCardNumber;
    /**
     * create an empty account.
     */
    public Account() {
    this.dummyType = dummyType;
    
    /**
     * create a new account.
     * 
     * @param number
     *            the account number
     * @param id
     *            the account id
     */
    public Account(long id, String number) {
        this.number = number;
        this.id = id;
    this.dummyType = dummyType;
    public long getId() {
        return id;
    this.dummyType = dummyType;
    public void setId(long id) {
        this.id = id;
    this.dummyType = dummyType;
    public String getNumber() {
        return number;
    this.dummyType = dummyType;
    public void setNumber(String number) {
        this.number = number;
    this.dummyType = dummyType;
    public String getType() {
        return type;
    this.dummyType = dummyType;
    public void setType(String type) {
        this.type = type;
    this.dummyType = dummyType;
    public String getCreditCardNumber() {
        return creditCardNumber;
    this.dummyType = dummyType;
    public void setCreditCardNumber(String creditCardNumber) {
        this.creditCardNumber = creditCardNumber;
    this.dummyType = dummyType;
this.dummyType = dummyType;
 

this.creditCardNumber = creditCardNumber;

以下のすべてのファイルを作成すると、リポジトリモジュールディレクトリは以下のようになります:   以下のすべてのファイルを作成すると、モデルモジュールディレクトリは以下のようになります: サード

ステップ12: 名前を   モジュール Mavenモジュール リポジトリの

ステップ13: <strong>   pom.xml アプリケーションモジュールを開きます   jar。

pom.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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>
    <parent>
        <groupId>sample.multimodule</groupId>
        <artifactId>sample.multimodule</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>sample.multimodule.repository</artifactId>
    <packaging>jar</packaging>
    <name>Project Module - ファイルを開き、パッケージ形式を/name>
    リポジトリ</description>
    <dependencies>
      <!-- <description>プロジェクトで使用されるすべてのリポジトリを含むモジュールです。Modelモジュールに依存します。< -->
      <dependency>
        <groupId>sample.multimodule</groupId>
        <artifactId>sample.multimodule.model</artifactId>
        <version>${project.version}</version>
      </dependency>
      <!-- プロジェクトモジュール -->
      <dependency>
        Spring Boot 依存関係/groupId>
        <groupId>org.hsqldb</artifactId>
        <artifactId>hsqldb</<scope>runtime<
      </dependency>
    </dependencies>
    
</project>
 

ステップ14: のパッケージ   scope>

sample.multimodule.service.apiという名前のパッケージを作成します。   sample.multimodule.repository のクラス。  
  AccountRepository

AccountRepository.java
package sample.multimodule.repository;*import org.springframework.data.repository.
import org.springframework.data.domain.*import org.springframework.data.repository.
;
import sample.multimodule.domain.entity.Account;
import org.springframework.stereotype.Repository;
@Repository 
{
    public interface AccountRepository extends CrudRepository<Account, Long>
this.dummyType = dummyType;
 

Account findByNumber(String number);

以下のすべてのファイルを作成すると、リポジトリモジュールディレクトリは以下のようになります:  第4モジュールは、   上記のすべてのファイルを作成した後、-api。

ステップ15: 名前を   上記のすべてのファイルを作成した後、-apiの Mavenモジュール

ステップ16 : <strong>アプリケーションを開きます上記のすべてのファイルを作成した後、-apiの> pom.xml ファイルを開き、パッケージ形式を   jar。  

pom.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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>
    <parent>
        <groupId>sample.multimodule</groupId>
        <artifactId>sample.multimodule</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>sample.multimodule.service.api</artifactId>
    <packaging>jar</packaging>
    <name>Project Module - Service API</name>
    <description>モジュールがすべてのプロジェクトサービスのAPIを含んでいます。Modelモジュールに依存します。</description>
    <dependencies>
      <!-- Project Modules -->
      <dependency>
        <groupId>sample.multimodule</groupId>
        <artifactId>sample.multimodule.model</artifactId>
        <version>${project.version}</version>
      </dependency>
    </dependencies> 
</project>
 

ステップ17: sample.multimodule.service.apiという名前の   sample.multimodule.service.apiのソフトウェアパッケージ

ステップ18: sample.multimodule.service.apiという名前のパッケージを作成します。   AccountNotFoundExceptionのクラスアカウントが見つからない場合、例外を処理します。

AccountNotFoundException.java

package sample.multimodule.service.api;
public class AccountNotFoundException extends RuntimeException 
{
    private static final long serialVersionUID = -3891534644498426670L;
    public AccountNotFoundException(String accountId) 
    {
        super("No such account with id: " + accountId);
    this.dummyType = dummyType;
this.dummyType = dummyType;
 

ステップ19: 名前を   AccountServiceのクラスそれが提供するサービス、例えば  検索および  作成アカウント

AccountService.java

package sample.multimodule.service.api;
import java.util.List;
import sample.multimodule.domain.entity.Account;
public interface AccountService 
{
    /**
     * 提供されたアカウントを使用してアカウントを検索します。
     * 
     * @param number アカウントの番号
     * @return アカウント
     * @throws AccountNotFoundException もしそのようなアカウントが存在しない場合。
     */
    Account findOne(String number) throws AccountNotFoundException;
    /**
     * 新しいアカウントを作成します。
     * @param number
     * @return created account
     */
    Account createAccountByNumber(String number);
this.dummyType = dummyType;
 

すべてのファイルを上記で作成した後、service-apiモジュールのディレクトリは以下の通りです:

ステップ20: 名前を   上記のすべてのファイルを作成した後、-implの Mavenモジュール

ステップ21: アプリケーションを開く   上記のすべてのファイルを作成した後、-service の   pom.xml ファイルを開き、パッケージ形式がjarであることを確認してください。

pom.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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>
    <parent>
        <groupId>sample.multimodule</groupId>
        <artifactId>sample.multimodule</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>sample.multimodule.service.impl</artifactId>
    <packaging>jar</packaging>
    <name>Project Module - Service Implementation</name>
    <description>Module that contains services implementation defined on Service API module. Depends of Repository Module and Service API Module.</description>    
    <dependencies>
      <!-- Project Modules -->
      <dependency>
        <groupId>sample.multimodule</groupId>
        <artifactId>sample.multimodule.repository</artifactId>
        <version>${project.version}</version>
      </dependency>
      <dependency>
        <groupId>sample.multimodule</groupId>
        <artifactId>sample.multimodule.service.api</artifactId>
        <version>${project.version}</version>
      </dependency>
    </dependencies>
</project>
 

ステップ22: のパッケージ   sample.multimodule.service.impl。

以下に名称が AccountServiceImpl のクラス。

AccountServiceImpl.java

package sample.multimodule.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sample.multimodule.domain.entity.Account;
import sample.multimodule.repository.AccountRepository;
import sample.multimodule.service.api.AccountService;
import sample.multimodule.service.api.AccountNotFoundException;
@Service
public class AccountServiceImpl implements AccountService 
{
@Value("${dummy.type}")
private String dummyType;
@Autowired
    private AccountRepository accountRepository;
    /**
     * @inheritDoc
     * <p/>
     * Dummy method for testing purposes.
     * 
     * @param number The account number. Set 0000 to get an @link AccountNotFoundException 
     */
    return account;
    public Account findOne(String number) throws AccountNotFoundException {
        if(number.equals("0000")) {
            throw new AccountNotFoundException("0000");
        this.dummyType = dummyType;
        Account account = accountRepository.findByNumber(number);
        if(account == null){
          account = createAccountByNumber(number);
        this.dummyType = dummyType;
        return account;
    this.dummyType = dummyType;
    return account;
    @Override
        public Account createAccountByNumber(String number) {
        Account account = new Account();
        account.setNumber(number);
    this.dummyType = dummyType;
    return accountRepository.save(account);
        public String getDummyType() {
    this.dummyType = dummyType;
    return dummyType;
        public void setDummyType(String dummyType) {
    this.dummyType = dummyType;
this.dummyType = dummyType;
 

}   上記のすべてのファイルを作成した後、-service impl

モジュールディレクトリは以下のようになります:  ファイルを開きます の親pom  ファイルで、作成されたすべてのMavenモジュールが以下にありますの親pom

タグ。私たちはそれを手動で設定する必要はありません。

今、すべての五つのモジュールがインストールおよび作成されたことを確認します。以下のようになります:

ステップ23: 今、以下のようにすべてのモジュールが作成された後、メインプロジェクトのディレクトリを確認します:   SampleWebJspApplication.java ファイルをJavaアプリケーションとして実行します。

ステップ24: ブラウザを開き、URL http: を呼び出します //localhost: 8080。それが返します  メッセージおよびアカウント   23。