gpt4 book ai didi

java - 对于使用 spring-boot 的多模块 Maven 项目,通用模型和实用程序的正确位置

转载 作者:行者123 更新时间:2023-12-02 09:10:27 24 4
gpt4 key购买 nike

有一个多模型maven项目,目录布局如下:

  xxx-parent/      common/      common-web/      member-api/      admin-api/      market-service/      ...

正如你所看到的,有一个父pom,和多个子模块,commoncommon-xxx模块是commons部分,这将是依赖项其他非通用模块,用于代码复用。

<小时/>

问题

  • A。您将把 model 类放在哪里,这些类通常是带有私有(private)字段和 getter/setter 的 POJO。
    这些模型可能会被多个非通用子模块重用(例如 market-serviceadmin-api)。
  • B。对于某些api,这些模型可以在返回的json中重用,但只需要部分字段,您是否会创建一个单独的POJO(通常我称之为实体)那件事呢?而且,如果是这样,您可能会将其放入特定的子模块(例如 admin-api),而不是公共(public)项目,对吧?
  • C。如果这是一个基于 spring-boot 的项目,并且它使用 Redis,我们编写了一个 redis util,它可以重用于大多数 redis 操作。
    问题是,如果我们将redis util及其maven依赖项放入common中,那么它将在应用程序启动时自动连接到redis,但某些子模块不需要redis。
    我正在考虑的解决方案是:
    • 为 util 创建一个单独的子模块 common-redis
      但也会有其他实用程序(例如common-jdbc),并且似乎有太多通用模块。
    • 通过@EnableAutoConfiguration禁用 sprint-boot 针对特定功能(例如 redis)的自动配置,这样它就不会在启动时尝试连接到 redis。

最佳答案

你的maven多模块结构:

xxx-parent/
common/
common-web/
member-api/
admin-api/
market-service/
...

让我单独回答一下问题。

A. Where would you put the model classes, which are usually POJO, with private fields, and getter/setters. These models might be reused by multiple non-common sub modules, (e.g both market-service and admin-api).

通常,您将在父项目下名为 xxx-entitiesxxx-defs(defs 代表定义)的单独模块下拥有模型类 xxx-父。我不确定 common 模块包含什么,但有些项目使用单词 xxx-commons 来定义通用模型(/POJOs/Entities)。这是您将作为依赖项提供给其他模块的模块。示例:如果您有一个单独的持久层,想要对模型类进行操作,并且您希望将其与您为服务编写的 REST 客户端库分开。

B. For some api, those model could be reused in the returned json, but only need part of the fields, would you create a separate POJO (usually I call it entity) for that case? And, if so, you would probably put it in the specific sub module (e.g admin-api), not the common projects, right?

您可以重复使用我之前提到的相同模型库。如果只需要部分字段,则仅设置这些字段(其他字段保持为空)并返回。大多数 REST 库都有序列化设置,您可以将其设置为在从对象生成 JSON 时忽略空值。示例:Ignore null from Json using Jackson.

C. If this is a spring-boot based project, and it use Redis, and we wrote a redis util, which could be reused for most redis operations. The problem is if we put the redis util and its maven dependencies into common, then it will connect to redis on application start automatically, but some of the sub modules don't need redis.

你的方法听起来不错。您可以拥有一个类似 data-utils 的库,它可以包含与数据库(Redis、RDBMS 等)相关的操作的包装器。并且您可以使用 init 类方法,这将确保它们不会自动加载,除非由客户端显式调用。应注意确保应从包含此 data-utils 客户端的父模块加载 Redis 主机名、端口等配置,并且该库不能将此类配置作为其一部分.

关于java - 对于使用 spring-boot 的多模块 Maven 项目,通用模型和实用程序的正确位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59462422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com