Spring Boot 創(chuàng)建多模塊項(xiàng)目

本文使用IDEA作為開發(fā)工具。

1、構(gòu)建骨架

新建一個普通的Spring Boot項(xiàng)目,名稱為springboot-multi-module。
可以參考IDEA新建Spring Boot項(xiàng)目

image.png

2、新建一個web子模塊

右鍵項(xiàng)目>>New>>Module


image.png

然后在彈出的子界面中選擇的左側(cè)選擇Maven,然后Next


image.png

輸入子模塊的名稱,然后Next


image.png

最后Finish


image.png

此時查看原來父類的pom.xml文件,發(fā)現(xiàn)packing變?yōu)榱藀om,并且增加了一個modules節(jié)點(diǎn),里面有一個剛添加的web子模塊

    <!--新增了modules節(jié)點(diǎn)-->
    <modules>
        <module>web</module>
    </modules>
    <!--由原來的jar變?yōu)閜om-->
    <packaging>pom</packaging>
image.png

3、移動和刪除原來父模塊的文件

在web模塊main>java和test>java 下新建原父模塊同名的包,這里是com.xiaozhao

image.png

然后開始移動文件,把父工程的SpringbootMultiModuleApplication.java、SpringbootMultiModuleApplicationTests.java 、application.properties移動對web子模塊下對應(yīng)的目錄中。用鼠標(biāo)按住文件拖動即可。

image.png

拖動完畢后


image.png

然后刪除src文件夾,最后工程的目錄為


image.png

4、新建service子模塊

然后按照建立web子模塊的方式再新建一個service的子模塊

image.png

然后在service子模塊中新建一個包 com.xiaozhao.service,然后在此包新建一個HelloService的類,內(nèi)容如下:

/**
 * @author xiaozhao
 * @date 2018/10/26上午11:13
 */
@Service
public class HelloService {
    public String hello(String name) {
        return "Hello " + name;
    }
}

5、在web子模塊中引用service子模塊的服務(wù)

首先在web子模塊的pom.xml文件中增加對service模塊的依賴,此處注意artifactId的寫法,為service模塊中pom.xml中的獨(dú)立的artifactId

image.png
    <dependencies>
        <dependency>
            <groupId>com.xiaozhao</groupId>
            <artifactId>service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

新建一個HelloController,內(nèi)容如下:

/**
 * @author xiaozhao
 * @date 2018/10/26上午11:14
 */
@RestController
@RequestMapping("/user")
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/hello/{name}")
    public String sayHello(@PathVariable String name) {
        return helloService.hello(name);
    }
}

最后運(yùn)行項(xiàng)目,在地址欄輸入 http://localhost:8080/user/hello/Kobe

會看到如下的輸出

*Hello Kobe

關(guān)于打包

1、打jar包

經(jīng)過上述步驟之后,可以直接在IDEA開發(fā)工具中直接運(yùn)行,但是執(zhí)行maven的打包命令時,卻會打包失敗,提示找不到mainClass,這個時候在web模塊的pom.xml文件下增加一個build節(jié)點(diǎn),其中mainClass的值就是web子模塊下的SpringbootMultiModuleApplication

 <build>
        <finalName>${artifactId}-${version}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.xiaozhao.SpringbootMultiModuleApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

然后刪除父類pom.xml文件中的build節(jié)點(diǎn)


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

此時再打包就可以成功了


image.png

但是這種情況下執(zhí)行 mvn spring-boot:run 依然會提示找不到主類。

2、打war包

也可以打包為war包,放到獨(dú)立Tomcat下執(zhí)行。在上面打jar包的基礎(chǔ)上做如下的修改:

2.1 首先將web子模塊的pom.xml文件中新增一個packaging節(jié)點(diǎn)

 <!--增加節(jié)點(diǎn)-->
    <packaging>war</packaging>

2.2 在最外層pom.xml文件中排除內(nèi)嵌的Tomcat依賴


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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--排除tomcat的依賴干擾-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>

2.3 修改web子模塊下的 SpringbootMultiModuleApplication

繼承SpringBootServletInitializer,然后再覆蓋其中的configure方法

@SpringBootApplication
public class SpringbootMultiModuleApplication extends SpringBootServletInitializer {

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SpringbootMultiModuleApplication.class);
    }
}

最后打成war包即可

image.png

完整代碼鏈接

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容