【问题标题】:Maven Multi Module insists on duplicating datasource application.properties in business moduleMaven Multi Module坚持在业务模块中复制datasource application.properties
【发布时间】:2018-04-29 06:49:05
【问题描述】:

我有 spring boot 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)

product-data 将实体对象返回给 product-business,product-business 将实体对象返回给 product-rest,product-rest 返回 json 对象。

产品数据运行良好。但是,一旦我运行产品业务,就会收到错误 “无法确定数据库类型 NONE 的嵌入式数据库驱动程序类”。 Spring 在我的 product-business/src/main/resources/application.properties 文件下查找 spring.datasource.... 属性。如果我在这里定义所有属性,那么错误就会消失,我会从产品数据中获取数据。

但是!!我已经在 product-data/src/main/resources/application.properties 文件下定义了属性。 为什么我必须在我的产品-业务模块中复制相同的属性?整个目的是分离层。 product-data 负责获取数据,它应该在自己的结构下找到 spring.datasource... 属性。为什么它也强迫我复制业务模块中的属性?我确定我错过了一些东西。有人知道吗?

我在 SO 上遇到了许多类似的问题,但其中大多数都缺少属性,所以这并不能解决我的问题。我认为我的 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>

【问题讨论】:

  • 我得到的错误:APPLICATION FAILED TO START 描述:无法确定数据库类型的嵌入式数据库驱动程序类 NONE 操作:如果您想要一个嵌入式数据库,请在类路径中放置一个受支持的数据库。如果您有要从特定配置文件加载的数据库设置,您可能需要激活它(当前没有配置文件处于活动状态)。
  • 你应该使用与导入的spring boot依赖相同版本的spring-boot-maven-plugin...不适合...
  • product-data 和 product-business 都使用相同版本的 spring boot
  • @suomi-dev 你找到解决问题的方法了吗?我也面临同样的问题

标签: spring hibernate maven jpa multi-module


【解决方案1】:

根据this

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

为了解决这个问题并仍然保持解耦架构,我在 product-data 模块的资源文件夹下创建了另一个文件 data.properties 并指定了 @PropertySource 配置文件中的注释。这是 product-data 模块的配置文件。 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

【讨论】:

    【解决方案2】:

    您可以使用spring.config.name 属性覆盖默认值application 并在其中放置更多属性文件名。

    例如:

    spring.config.name=entities,business,application

    在这种情况下,您可以在实体模块中拥有entities.properties,在业务模块中拥有business.properties,在rest 或app 模块中拥有application.properties。最后一个配置名称(应用程序)具有最高优先级,第一个配置名称(实体)具有最低优先级。

    【讨论】:

      猜你喜欢
      • 2018-06-04
      • 1970-01-01
      • 2012-12-02
      • 2022-01-12
      • 1970-01-01
      • 2014-06-02
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多