【问题标题】:Access parent's application.properties from dependent project in spring boot在 Spring Boot 中从依赖项目访问父级的 application.properties
【发布时间】:2017-10-20 14:05:53
【问题描述】:

我正在Spring Boot中开发一个多模块项目,项目结构如下:

com.app.parent  <- parent pom with version numbers and common dependencies (POM)
com.app.core    <- repository and service layer, models, DTOs (JAR)
com.app.rest    <- rest API (WAR)
com.app.soap    <- soap API (WAR)

项目的pom.xml文件是:

<artifactId>app-parent</artifactId>
<packaging>pom</packaging>
<name>app-parent</name>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
    <relativePath/>
</parent>

<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>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

core 项目的pom.xml 文件是:

<artifactId>app-core</artifactId>
<packaging>jar</packaging>
<name>app-core</name>

<parent>
    <groupId>com.app</groupId>
    <artifactId>app-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../app-parent/pom.xml</relativePath>
</parent>

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
</dependencies>

rest 项目的pom.xml 是:

<artifactId>app-rest</artifactId>
<packaging>war</packaging>
<name>app-rest</name>

<parent>
    <groupId>com.app</groupId>
    <artifactId>app-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../app-parent/pom.xml</relativePath>
</parent>

<dependencies>
    <dependency>
        <groupId>com.app</groupId>
        <artifactId>app-core</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

app-core 项目的入口点是:

@SpringBootApplication
public class CoreApplication {

    public static void main(String[] args) {
        SpringApplication.run(CoreApplication.class, args);
    }
}

app-rest 项目的入口点看起来完全一样。

我在core/src/main/resources/ 中创建了一个application.properties 文件,其中包含数据库配置等,我可以很好地编译/测试app-core 项目。但是,当我尝试运行或测试 app-rest 项目时,我收到与缺少 application.properties 文件相关的错误

无法确定数据库类型 NONE 的嵌入式数据库驱动程序类。如果您想要一个嵌入式数据库,请在类路径中放置一个受支持的数据库。

如何让子项目加载父项目的application.properties 文件?我必须创建指向父文件的符号链接,还是通过@PropertySource 明确加载父文件的属性文件?在这种情况下其他人会怎么做?

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    为配置创建另一个模块(比如说 config)并将您的 application.properties 放在 config/src/main/resources/ 中。

    将配置模块添加为任何使用相同配置的模块的依赖项,然后就可以了。

    【讨论】:

      猜你喜欢
      • 2016-06-10
      • 2020-07-08
      • 2017-06-18
      • 2020-02-28
      • 2020-03-18
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 2018-05-07
      相关资源
      最近更新 更多