簡體   English   中英

Spring Maven項目結構pom.xml

[英]Spring Maven project structure pom.xml

我想幫助使用Spring Boot設置一個多模塊Maven項目。

糾正我,如果我錯了,但我讀過,彈簧引導讀取啟動主應用程序(標注有@SpringBootApplication與跑SpringApplication.run ),並通過反射找到必要的類。 這意味着它首先訪問啟動類,然后繼續查找控制器,模型,存儲庫。 如果是這樣,如果我有這樣的項目結構,如何在每個模塊的pom.xml中設置依賴項:

app
--src
--pom.xml

core
--pom.xml

--models
----/pom.xml

--controllers
----/pom.xml

--repositories
----/pom.xml

pom.xml

請查看如何在spring boot中創建多模塊項目的完整指南。

https://spring.io/guides/gs/multi-module/

Spring引導將從使用@SpringBootApplication注釋的類的包中進行組件掃描。 組件scannign意味着它以遞歸方式查看該包下的類,分析注釋並連接它識別的任何內容。 這可以包括控制器,帶有@Value注釋的簡單變量,帶有@Autowired成員以及許多其他東西。

您實際上可以跳轉到@SpringBootApplication注釋的源代碼,並看到它擴展為許多其他注釋, @ComponentScan就是其中之一。

如果所有模塊都是從那里開始的子層次結構,那么無論如何它們都將被正確掃描。 但通常,子模塊的包層次結構會略有不同。 在這種情況下,您可以在代碼中明確指定@ComponentScan() ,在()可以列出要從組件掃描的基礎包。

此時子模塊是否無關緊要; 它就像您在其他任何庫中掃描類一樣。

一般建議

此外,僅供參考 - 多模塊項目可能有點難以管理(從眾多單獨的經驗中說話)。 如果使用得當,它們可以非常好。 如果你是Maven的初學者,那么保持separte,定義良好的項目具有適當的發布周期並將它們作為普通依賴項導入可能更明智。

所以,我不贊成或反對他們,但只是確保你理解他們正在進行:)。

我有一個GitHub項目,我在其中配置了一個多模塊maven項目:

https://github.com/cristianprofile/spring-boot-mvc-complete-example

這是示例項目maven模塊結構:

在此輸入圖像描述

 Spring mvc rest maven module ---> service maven module  ---> repository maven module

主模塊應該像這樣配置(Spring mvc rest layer):

@SpringBootConfiguration
@EnableAutoConfiguration
//spring mvc module auto scan only its package
@ComponentScan(basePackageClasses = HelloWorldController.class)
//It needs Service bean so it will import ConfigurationService.class from
// Service maven module
@Import({ConfigurationService.class})

完整課程:

https://github.com/cristianprofile/spring-boot-mvc-complete-example/blob/develop/spring-boot-mvc-rest/src/main/java/com/mylab/cromero/controller/Application.java

它只掃描其包裝:

HelloWorldController.class  -->  com.mylab.cromero.controller;

此Rest層使用服務maven模塊,因此需要添加依賴項:

    <dependency>
        <groupId>com.mylab.cromero.core</groupId>
        <artifactId>mylab-core-service-impl</artifactId>
        <version>0.0.2-SNAPSHOT</version>
    </dependency>

完整的pom文件:

https://github.com/cristianprofile/spring-boot-mvc-complete-example/blob/develop/spring-boot-mvc-rest/pom.xml#L16

來自服務maven模塊的ConfigurationService.class自動掃描其包,它將導入ConfigurationRepository.class(Repository maven模塊)

@Configuration
//It needs repository's bean so it will import ConfigurationRepository.class from
// Repository maven module
@Import(ConfigurationRepository.class)
//service layer module auto scan only its package
@ComponentScan(basePackageClasses = ConfigurationService.class)
public class ConfigurationService {

}

完整服務maven模塊代碼:

https://github.com/cristianprofile/spring-boot-mvc-complete-example/blob/develop/mylab-core/mylab-core-service-impl/src/main/java/com/mylab/cromero/service/ ConfigurationService.java#L12

服務maven模塊層與maven存儲庫模塊具有依賴關系:

https://github.com/cristianprofile/spring-boot-mvc-complete-example/blob/develop/mylab-core/mylab-core-service-impl/pom.xml#L38

存儲庫模塊將自動配置jpa和域分類:

@Configuration
@EnableJpaRepositories(basePackages = "com.mylab.cromero.repository")
@EntityScan(basePackageClasses=Base.class)
@ComponentScan(basePackageClasses = BaseRepository.class)
public class ConfigurationRepository {

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM