簡體   English   中英

Spring 啟動多模塊工程與通用數據源

[英]Spring boot multi module project with common dataSource

我有一個 spring-boot 多模塊項目,其中我有不同的模塊,如 controller 模塊、服務模塊和 dao 模塊。 我有一個要求,比如我需要定義一個點來定義數據源,它可能位於 dao 層或任何其他單獨的模塊中,只是為了建立數據庫連接。 由於不可能在 n 個 controller 模塊(那些 war 文件)中擁有數據源,這可能會導致多個不必要的連接。 到目前為止,它僅在我在 controller 模塊中定義數據源時才有效。 提前致謝 !!!!

如果您想擁有一個多模塊spring-boot應用程序,其中有一個戰爭和一組 jars(我們稱之為庫),您可以通過以下方式配置它。

父 pom ( parent.pom ):

<!--group, artifact and so on-->
<modules>
    <module>my-war</module>
    <module>my-library</module>
</modules>

戰爭 pom:使用 scope import , package pom ,這樣你就可以從spring-boot-dependencies繼承所有依賴項,從技術上講,你可以擁有某種類型的許多父母。

<parent>
    <!--parent group id artifact and version (parent.pom)-->
</parent>
<!--group, artifact and so on-->
<packaging>war</packaging>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.3.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

jar pom 我們的庫,例如 dao:

<parent>
    <!--parent group id artifact and version (parent.pom)-->
</parent>

<!--group, artifact and so on-->
<packaging>jar</packaging>

現在在您的庫模塊中添加application.properties與您的datasource連接和配置 class 讓我們稱之為BusinessConfig

@Configuration
@ComponentScan(basePackages = {"your.package"})
@EnableJpaRepositories(basePackages = {"your.package"})
@EntityScan(basePackages = {"your.package"})
public class BusinessConfig {
}

並在您的war導入您的BusinessConfig

@Configuration
@Import(value = {BusinessConfig.class})
public class Config {
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM