多个 Spring Data JPA 模块(非 Spring Boot)的依赖在 Spring Boot 应用中如何处理?

huangapple go评论84阅读模式
英文:

Multiple spring data jpa modules(non spring boot) dependencies in a Spring boot app?

问题

我们正在构建独立的可重用的 Spring Data JPA 模块,无需使用 Spring Boot。我们将其称为 db 模块。这些模块将被导入另一个 Spring Boot 应用程序。在 db 模块中,我们包含了以下依赖:

<dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-jpa</artifactId>
   <scope>provided</scope>
</dependency>

然而,在主 Spring Boot 应用程序中,我们包含了以下依赖:

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

db 模块中的所有 Spring 依赖项的作用范围都是 provided,预期在运行时由主 Spring Boot 应用程序提供。这样做的方式是否正确?

此外,每个 db 模块是否应该拥有自己的数据库连接属性文件,还是应该使用主 Spring Boot 应用程序的属性文件?所有 db 模块都连接到同一个数据库。这些模块代表了应用程序中的不同领域。

英文:

We are building independent reusable spring data jpa modules without spring boot. Let's call them db modules. These modules will be imported in another spring boot app. In the db modules, we are including

&lt;dependency&gt;
   &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
   &lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
   &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;

However, in the main spring boot have we have included

&lt;dependency&gt;
     &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
     &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&lt;/dependency&gt;

All spring dependencies in db module are of scope provided and expected to be present during runtime provided by main spring boot app. Is this the right way to do it?

Also, should each db modules have it's own properties file for db connection or should they reply the main spring boot app. All db modules connect to the same database. These modules represent different domains in the application.

答案1

得分: 1

不,使用<scope>provided</scope>是不正确的。

正如Maven 文档 所述:

> provided
> 这与compile非常相似,但表示您希望JDK或容器在运行时提供依赖关系。例如,当构建Java企业版的Web应用程序时,您会将对Servlet API和相关的Java EE API的依赖关系范围设置为provided,因为Web容器提供了这些类。具有此范围的依赖项会添加到用于编译和测试的类路径中,但不会添加到运行时类路径中。它不是传递性的。

换句话说,在部署装配之外,它由运行时“提供”。

如果在打包代码以部署时必须包含该依赖项,则肯定不希望它为您“提供”。

请记住,您的库需要告诉构建系统依赖于您的库的内容,以及需要包括其他库。这就是传递性依赖关系的全部目的,因此,通过不将spring-data-jpa设为传递性依赖关系,您是错误的。

英文:

No, using &lt;scope&gt;provided&lt;/scope&gt; is not correct.

As the Maven documentation says:

> provided
> This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. A dependency with this scope is added to the classpath used for compilation and test, but not the runtime classpath. It is not transitive.

Said another way, it is "provided" by the runtime, outside of your deployment assembly.

If the dependency must be included when packaging your code for deployment, then you are certainly not expecting it to be provided for you.

Remember, your library need to tell the build system that depends on your library, what it needs to include other libraries as well as your library. That is the entire purpose of transitive dependencies, so by not making spring-data-jpa a transitive dependency, you're doing it wrong.

huangapple
  • 本文由 发表于 2020年9月15日 04:33:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63891476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定