Maven的多模块坚持在业务模块复制数据源application.properties

问题描述 投票:2回答:1

我有春天启动的maven java的多模块结构。我的结构是:

product (parent pom module)
..product-data (child pom module)
..product-business (has dependency for product-data) 
..product-rest (has dependency for product-business) 
..product-entities (child pom module)

产品数据将实体对象返回产品业务和产品业务将实体对象返回到产品的休息和产品,其余的返回JSON对象。

产品数据运行良好。但只要我经营产品的企业,我得到错误“无法确定数据库类型无嵌入式数据库驱动程序类”。春季查找spring.datasource ....下我的产品的业务性质/ src目录/主/资源/ application.properties文件。如果我在这里定义的所有属性,然后错误消失,我得到从产品数据中的数据。

但!!我已经定义下的产品数据/ src目录/主/资源/ application.properties文件中的属性。我为什么要重复我的产品业务模块相同的属性?的整个目的是分开的层。产品数据负责获取数据,它应该找到spring.datasource ...下自己的结构性能。为什么它迫使我在重复业务模块的性能呢?我相信,我失去了一些东西。是否有人有任何线索?

我经历了这么多类似的问题了,但大多数都缺少的属性,以便不解决我的问题。我想我的POM文件不是犯罪嫌疑人,因为一旦我复制粘贴于产品的数据产品,业务,那么错误消失的特性。但是,柜面,如果你还是想看到我的POM:

父产品POM

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.owner</groupId>
    <artifactId>product</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <nopeasti.version>1.0.0-SNAPSHOT</nopeasti.version>
    </properties>

    <dependencies>

    </dependencies>

    <modules>
        <module>product-data</module>
        <module>product-business</module>
        <module>product-rest</module>
        <module>product-entities</module>
    </modules>
</project>

产品数据POM

<project>
    <artifactId>product-data</artifactId>
    <packaging>jar</packaging>
    <parent>
        <groupId>com.owner</groupId>
        <artifactId>product</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <dependencyManagement>
         <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.5.8.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.0.M6</version>
            </plugin>
        </plugins>
    </build>
</project>

产品业务POM

<project>
    <artifactId>product-business</artifactId>
    <packaging>jar</packaging>
    <parent>
        <groupId>com.owner</groupId>
        <artifactId>product</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <dependencyManagement>
         <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.5.8.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.owner</groupId>
            <artifactId>product-data</artifactId>
            <scope>compile</scope>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.0.M6</version>
            </plugin>
        </plugins>
    </build>
</project>
spring hibernate maven jpa multi-module
1个回答
0
投票

this

这是不可取把application.properties在库中,因为有可能是在使用它的应用程序运行时发生冲突(只有一个application.properties正在不断从classpath加载)

为了解决这个问题,仍然保持解耦的架构,我创建了另一个文件data.properties产品的数据模块的资源文件夹下,并在配置文件中指定@PropertySource注解。以下是产品的数据模块的配置文件。该spring.datasource性能在这个文件中定义。然后,就没有必要在其他2个模块spring.datasource性能。

@ComponentScan
@EnableAutoConfiguration
@SpringBootConfiguration
@PropertySource(value = "data.properties")
public class NopeastiDataConfig {
    // no main needed here as this is library consumed by business layer
}

这里进一步阅读:Externalized Configuration

© www.soinside.com 2019 - 2024. All rights reserved.