简体   繁体   English

打包结构化Spring项目的最佳实践是什么?

[英]what is best practice to package structuring Spring project?

i have spring cloud project and per microservice i think i have many packages. 我有春季云项目和每个微服务,我想我有很多软件包。 also i have model package witch contains entity-s and custom request response objects . 我也有模型包女巫包含实体和自定义请求响应对象。

question 1 : what must be entity classes package name ? 问题1:实体类包名称必须是什么? entity or model ? 实体还是模型?

question 2: where must save Searchrequest & Searchresponse and other request response classes ? 问题2:必须在哪里保存Searchrequest和Searchresponse以及其他请求响应类? in model package or i must create other package ? 在模型包装中还是我必须创建其他包装?

question 3 : exits some Standard for package structuring ? 问题3:是否退出了一些包装结构标准? (pleas give me link ) (请给我链接)

在此处输入图片说明

There is no specific standard structure, It will vary according to your need. 没有特定的标准结构,它会根据您的需要而有所不同。

  1. Entity classes should be under domain package, which does not to be appended with the class name. 实体类应位于域包下,该域包不应附加到类名之后。 It can be like Book.class, School.class, etc.., 可以像Book.class,School.class等。
  2. Search request and response is the request and response class, It should come under the controller package. 搜索请求和响应是请求和响应类,它应位于控制器包下。 These are all service end points. 这些都是服务端点。
  3. check the link 检查链接

Mostly the SERVICE package will have all the service classes which have business logic and to connect with DAO classes, Controller will have all the rest end points, Model package will contain the pojo classes. 通常,SERVICE包将具有所有具有业务逻辑并与DAO类连接的服务类,Controller将具有所有其余端点,Model包将包含pojo类。

  1. The package name for the database table should be entity. 数据库表的包名称应为实体。

    Here is why? 这是为什么呢?

  2. for Q2 and Q3 Packaging Structure 适用于Q2和Q3 包装结构

Hope this helps,Let me know:) 希望对您有所帮助,让我知道:)

There is not strict rules. 没有严格的规定。 You have to use a naming that is self explanatory (not needed to dig into the classes content to understand its meaning). 您必须使用一个易于解释的命名(无需深入了解类的内容即可理解其含义)。 That is right for micro-service, web application, batch and any kind of applications. 这是微服务,Web应用程序,批处理和任何类型的应用程序的正确选择。

I think i have many packages 我想我有很多包裹

The main role of packages is to sort things in a human readable way. 包的主要作用是以人类可读的方式对事物进行排序。
For example if you have 2 or 3 classes by package, you can wonder on the relevance of them. 例如,如果您按包提供2或3个类,则您可能会怀疑它们的相关性。
Similarly if you have 50 classes in a package, you can also wonder if you should not split them in subpackages. 同样,如果一个程序包中有50个类,您还可以考虑是否不应该将它们拆分为子程序包。
Note that the package has also a role in terms of accessibility : indeed the package private access level allows to set/reduce the accessibility of a class or a class member to the classes of the current package. 请注意,程序包在可访问性方面也起着作用:实际上, package private访问级别允许设置/减少类或类成员对当前程序包的类的可访问性。 It may also be very useful in terms of design and of isolation. 就设计和隔离而言,它也可能非常有用。

About your 3 questions : 关于您的3个问题:

question 1 : what must be entity classes package name ? 问题1:实体类包名称必须是什么? entity or model ? 实体还是模型?

In Java, entity refers generally to JPA entity (overall in a Spring project). 在Java中, entity通常指的是JPA实体(在Spring项目中是整体)。 While model refers to a more general concept that includes entity but not only since the data model can also be the DTO objects and any data specialization of your model. 虽然model指的是更笼统的概念,其中包括entity但不仅因为数据模型还可以是DTO对象以及模型的任何数据专用化。
If you use a single data objects layer (JPA entities that are also used as JSON representation) , using the model term makes completely sense. 如果您使用单个数据对象层(也用作JSON表示形式的JPA实体),则使用model术语完全有意义。 If you use several level of abstraction, a package by abstraction level makes more sense. 如果您使用多个抽象级别,则按抽象级别的包更有意义。

question 2: where must save Searchrequest & Searchresponse and other request response classes ? 问题2:必须在哪里保存Searchrequest和Searchresponse以及其他请求响应类? in model package or i must create other package ? 在模型包装中还是我必须创建其他包装?

If these objects go through every layer : in model , otherwise probably elsewhere. 如果这些对象遍历model每一层:否则可能在其他地方。

question 3 : exits some Standard for package structuring ? 问题3:是否退出了一些包装结构标准? (please give me link ) (请给我链接)

3) Not really but as a rule of thumb, it should be self explanatory, very clear and shared by the team of the projects to favor consistency between projects. 3)并非完全如此,但是根据经验,它应该是自我解释的,非常清楚的并且由项目团队共享,以促进项目之间的一致性。

Create package is free, so don't worry to create many packages. 创建软件包是免费的,因此不必担心创建许多软件包。 The objective of creating packages is organize your code and delimite some responsibilities or contexts. 创建软件包的目的是组织代码并划定一些职责或上下文。

question 1 : what must be entity classes package name ? 问题1:实体类包名称必须是什么? entity or model ? 实体还是模型?

You need to understand the difference between these two before choose the better name: 在选择更好的名称之前,您需要了解这两者之间的区别

A model object represents data in the MVC (Model View Controller) pattern. 模型对象以MVC(模型视图控制器)模式表示数据。 An entity object represents data in the ORM (Object Relational Mapping) pattern. 实体对象以ORM(对象关系映射)模式表示数据。 They could be the same object. 它们可能是同一对象。 Or not. 或不。

So, you can use the Entity as a Model class, even I understand this as a bad practice. 因此,您可以将Entity用作Model类,即使我理解这也是一种不好的做法。 If it's your case, maybe you prefer to use model as the package name. 如果是您的情况,也许您更喜欢使用model作为包名。 If not, entity is the right choice. 如果不是,那么entity是正确的选择。

question 2: where must save Searchrequest & Searchresponse and other request response classes ? 问题2:必须在哪里保存Searchrequest和Searchresponse以及其他请求响应类? in model package or i must create other package ? 在模型包装中还是我必须创建其他包装?

This classes are very correlated with the controller package. 此类与控制器包非常相关。 So, you can create these two packages and put the request and response there, each one in his package: 因此,您可以创建这两个包并将请求和响应放在其中,每个都放在他的包中:

controller.response
controller.request

question 3 : exits some Standard for package structuring ? 问题3:是否退出了一些包装结构标准? (pleas give me link ) (请给我链接)

See this answer: Are there best practices for (Java) package organization? 查看此答案: (Java)包组织是否有最佳实践?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM