Maven Multi Module坚持在业务模块中复制数据源application.properties [英] Maven Multi Module insists on duplicating datasource application.properties in business module

查看:58
本文介绍了Maven Multi Module坚持在业务模块中复制数据源application.properties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有Spring Boot Maven Java多模块结构.我的结构是:

I have spring boot maven java multi module structure. My structure is:

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)

product-data将实体对象返回到product-business,而product-business将实体对象返回到product-rest,而product-rest将返回json对象.

product-data will return entity object to product-business and product-business will return entity object to product-rest and product-rest returns json object.

产品数据运行正常.但是,一旦我运行产品业务,就会出现错误无法确定数据库类型为NONE的嵌入式数据库驱动程序类" . Spring在我的product-business/src/main/resources/application.properties文件下寻找spring.datasource ....属性.如果我在此处定义所有属性,那么错误就会消失,并且我会从产品数据中获取数据.

product-data runs fine. But as soon as I run product-business, I get error "Cannot determine embedded database driver class for database type NONE". Spring looks for spring.datasource.... properties under my product-business/src/main/resources/application.properties file. If I define all the properties here then error goes away and I get the data from product-data.

但是!!我已经在product-data/src/main/resources/application.properties文件下定义了属性. 为什么我必须在产品/业务模块中重复相同的属性?整个目的是分隔各个层.产品数据负责获取数据,并且应该在其自己的结构下找到spring.datasource ...属性.为什么它会迫使我也复制业务模块中的属性?我确定我想念一些东西.有人有任何线索吗?

But!! I have already defined the properties under product-data/src/main/resources/ application.properties file. Why do I have to duplicate the same properties in my product-business module? The whole purpose is to separate layers. product-data is responsible for fetching the data and it should find the spring.datasource... properties under its own structure. Why does it forces me to duplicate the properties in business module too? I am sure I am missing something. Does someone has any clue?

我在SO上经历了许多类似的问题,但大多数问题都缺少属性,因此无法解决我的问题.我认为我的pom文件不是可疑文件,因为一旦我将产品数据中的属性复制粘贴到产品业务中,错误就会消失.但是如果您仍然想看到我的pom:

I have gone through many similar questions on SO but most of them were missing the properties so that doesn't solves my problem. I assume my pom files are not the suspect because once I copy paste the properties from product-data to product-business then error goes away. But incase if you still want to see my pom:

父产品POM

Parent product 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

product-data 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

product-business 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>

推荐答案

根据

建议不要将application.properties放在库中,因为使用它的应用程序在运行时可能会发生冲突(从类路径中仅加载过一个application.properties)

It is not advisable to put application.properties in a library because there might be a clash at runtime in the application that uses it (only one application.properties is ever loaded from the classpath)

为解决此问题并仍然保持解耦的体系结构,我在 product-data 模块的资源文件夹下创建了另一个文件 data.properties ,并指定了 @PropertySource 批注.这是产品数据模块的配置文件. spring.datasource属性在此文件中定义.然后,在其他2个模块中就不需要spring.datasource属性.

To solve this and still maintain decoupled architecture, I created another file data.properties under resources folder of product-data module and specified @PropertySource annotation in the config file. Here is the config file of product-data module. The spring.datasource properties were defined in this file. Then there was no need for spring.datasource properties in other 2 modules.

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

在此处进一步阅读:外部配置

这篇关于Maven Multi Module坚持在业务模块中复制数据源application.properties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆