Spring Maven项目结构pom.xml

时间:2018-09-11 01:45:26

标签: java spring maven spring-boot

我想获得使用Spring Boot设置多模块Maven项目的帮助。

如果我错了,请纠正我,但我读到Spring-Boot会读取启动主应用程序(用@SpringBootApplication注释并用SpringApplication.run运行),并通过反射找到必要的类。这意味着它首先访问起始类,然后继续查找控制器,模型,存储库。如果是这样,如果我具有这样的项目结构,如何在每个模块的pom.xml中设置依赖项:

app
--src
--pom.xml

core
--pom.xml

--models
----/pom.xml

--controllers
----/pom.xml

--repositories
----/pom.xml

pom.xml

3 个答案:

答案 0 :(得分:2)

请参阅完整指南,了解如何在Spring Boot中创建多模块项目。

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

答案 1 :(得分:1)

Spring boot将从带有@SpringBootApplication注释的类的包中进行组件扫描。组件可识别性意味着它正在递归地浏览该包下的类,分析注释,并连接它可以识别的所有内容。这可以包括控制器,带有@Value批注的简单变量,带有@Autowired的成员以及许多其他东西。

实际上,您可以跳至@SpringBootApplication注释的来源,并看到它扩展到许多其他注释,@ComponentScan是其中之一。

如果所有模块均从此处明智地位于子层次结构包中,则无论如何都将对其进行正确扫描。通常,子模块将处于稍微不同的包层次结构中。在这种情况下,您可以在代码中显式指定@ComponentScan(),并在()内列出要从中进行组件扫描的基本软件包。

在这一点上,是否它的子模块无关紧要;就像扫描您所包含的任何其他库中的类一样。

一般建议

此外,仅供参考-多模块项目可能会有些难以管理(从众多单独的经验中得出)。如果使用得当,它们会非常好。不过,如果您是Maven的初学者,那么明智的做法是,保留具有适当发布周期的独立,定义明确的项目,然后将其作为常规依赖项导入。

所以,我不赞成也不反对他们,但请确保您对他们的理解很好:)。

答案 2 :(得分:1)

我有一个GitHub项目,其中配置了一个多模块maven项目:

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

这是示例项目maven模块结构:

enter image description here

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

主模块应该这样配置(Spring mvc rest层):

@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(资源库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 {

}

完成Service 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 {

}