Recommended Package Structure of a Spring Boot Project – 推荐的Spring Boot项目的包结构

最后修改: 2020年 4月 17日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

When building a new Spring Boot project, there’s a high degree of flexibility on how we can organize our classes.

在构建一个新的Spring Boot项目时,我们可以高度灵活地组织我们的类。

Still, there are some recommendations that we need to keep in mind.

不过,还是有一些建议需要我们牢记。

2. No Default Package

2.无默认包

Given the fact that Spring Boot annotations like @ComponentScan, @EntityScan, @ConfigurationPropertiesScan and @SpringBootApplication use packages to define scanning locations, it’s recommended that we avoid using the default package — that is, we should always declare the package in our classes.

鉴于像@ComponentScan@EntityScan、@ConfigurationPropertiesScan@SpringBootApplication这样的Spring Boot注解使用包来定义扫描位置,建议我们避免使用默认包–也就是说,我们应该始终在类中声明该包

3. Main Class

3.主课

The @SpringBootApplication annotation triggers component scanning for the current package and its sub-packages. Therefore, a solid way to go is to have the main class of the project reside in the base package.

@SpringBootApplication注解会触发对当前包及其子包的组件扫描。因此,一个可靠的方法是让项目的主类驻留在基础包中

This is configurable, and we can still locate it elsewhere by specifying the base package manually. However, in most cases, this option is certainly simpler.

这是可以配置的,我们仍然可以通过手动指定基础包来在其他地方找到它。然而,在大多数情况下,这个选项肯定更简单。

Even more, a JPA-based project would need to have a few additional annotations on the main class:

甚至更多,一个基于JPA的项目需要在主类上有一些额外的注解。

@SpringBootApplication(scanBasePackages = "example.baeldung.com")
@EnableJpaRepositories("example.baeldung.com")
@EntityScan("example.baeldung.com")

Also, be aware that extra configuration might be needed.

另外,请注意可能需要额外的配置。

4. Design

4.设计

The design of the package structure is independent of Spring Boot. Therefore, it should be imposed by the requirements of our project.

包结构的设计是独立于Spring Boot的。因此,它应该由我们项目的要求来施加。

One popular strategy is package-by-feature, which enhances modularity and enables package-private visibility inside sub-packages.

一种流行的策略是按功能打包,它增强了模块化,并在子包内实现了包的隐私可见性。

Let’s take, for example, the PetClinic project. This project was built by Spring developers to illustrate their view on how a common Spring Boot project should be structured.

让我们以PetClinic项目为例。这个项目是由Spring开发人员建立的,以说明他们对普通Spring Boot项目应如何结构的看法。

It’s organized in a package-by-feature manner. Hence, we have the main package, org.springframework.samples.petclinic, and 5 sub-packages:

它的组织方式是以包为单位的。因此,我们有主包,org.springframework.samples.petclinic,和5个子包。

  • org.springframework.samples.petclinic.model
  • org.springframework.samples.petclinic.owner
  • org.springframework.samples.petclinic.system
  • org.springframework.samples.petclinic.vet
  • org.springframework.samples.petclinic.visit

Each of them represents a domain or a feature of the application, grouping highly-coupled classes inside and enabling high cohesion.

它们中的每一个都代表了应用程序的一个领域或一个特征,在内部将高度耦合的类分组,并实现高内聚力

5. Conclusion

5.总结

In this small article, we had a look at some recommendations we need to keep in mind when building a Spring Boot project – and learned about how we can design the package structure.

在这篇小文章中,我们看了一下在构建Spring Boot项目时需要注意的一些建议–并了解了我们如何设计包的结构。