简体   繁体   English

在spring boot中从依赖项目访问parent的application.properties

[英]Access parent's application.properties from dependent project in spring boot

I am developing a multi-module project in Spring Boot where the project structure is as follows: 我正在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)

The pom.xml file for the parent project is: 项目的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>

The pom.xml file for the core project is: 核心项目的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>

The pom.xml for the rest project is: 其余项目的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>

The entry point for the app-core project is: app-core项目的切入点是:

@SpringBootApplication
public class CoreApplication {

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

The entry point for the app-rest project looks exactly the same. app-rest项目的入口点看起来完全一样。

I have created an application.properties file within core/src/main/resources/ which contains database config etc. and I can compile/test the app-core project just fine. 我在core/src/main/resources/创建了一个application.properties文件,其中包含数据库配置等,我可以编译/测试app-core项目。 However, when I try to run or test the app-rest project I get errors related to the absence of application.properties file 但是,当我尝试运行或测试app-rest项目时,我会收到与缺少application.properties文件相关的错误

Cannot determine embedded database driver class for database type NONE. 无法确定数据库类型为NONE的嵌入式数据库驱动程序类。 If you want an embedded database please put a supported one on the classpath. 如果你想要一个嵌入式数据库,请在类路径上放置一个受支持的数据库。

How do I get the child project to load the parent's application.properties file? 如何让子项目加载父的application.properties文件? Do I have to create a symlink to the parent file, or do I expressly load the parent's property file via @PropertySource ? 我是否必须创建父文件的符号链接,还是通过@PropertySource明确加载父的属性文件? What do others do in this scenario? 其他人在这种情况下做了什么?

Create another module for configurations (let's say config) and put your application.properties in config/src/main/resources/. 为配置创建另一个模块(比如说config)并将你的application.properties放在config / src / main / resources /中。

Add the config module as dependency to any module which use the same configs and there you go. 将配置模块作为依赖项添加到任何使用相同配置的模块,然后就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot - 从依赖的 jar 加载 application.properties/yml - Spring Boot - Load application.properties/yml from dependent jar Spring 引导无法使用依赖的应用程序。属性 - Spring Boot not working with dependent application.properties Spring 引导项目未绑定来自 application.properties 的 @Value - Spring Boot project not binding @Value from application.properties 来自application.properties的Spring Boot配置 - Spring Boot configuration from application.properties Spring 引导应用程序没有从依赖 jar 获取 application.properties - Spring Boot App not picking up application.properties from dependent jar 在Tomcat中为Spring Boot应用程序访问externalize application.properties? - access externalize application.properties in Tomcat for Spring boot application? 访问外部 Spring 启动 Java 项目中的属性 pf application.properties - Access property pf application.properties in outer Spring Boot Java Project Spring Boot 项目中的 application.properties 文件在哪里? - Where is the application.properties file in a Spring Boot project? Spring-boot - 参数化测试 - 访问MethodSource中的Application.properties - Spring-boot - Parameterized Test - Access Application.properties in MethodSource 如何在 Spring Boot 中访问 application.properties 文件中定义的值 - How to access a value defined in the application.properties file in Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM