How to inherit application.properties in spring?

If I create a regular library that has one application.propertiesthat defines common configurations. How:

spring.main.banner-mode=off

How can I inherit these properties in another project where I include this commons library?

Maven:

<project ...>
    <groupId>de.mydomain</groupId>
    <artifactId>my-core</artifactId>
    <version>1.0.0</version>

    <dependencies>
        <dependency>
            <!-- this one holds the common application.properties -->
            <groupId>my.domain</groupId>
            <artifactId>my-commons</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
</project>

How can I inherit the configuration from my-commonsto my-core?

+6
source share
3 answers

The solution is to enable common properties using a different name, here application-shared.properties

In the shared library:

@SpringBootApplication
@PropertySource(ResourceUtils.CLASSPATH_URL_PREFIX + "application-shared.properties") //can be overridden by application.properties
public class SharedAutoConfiguration {
}

In the main application:

@SpringBootApplication
@Import(SharedAutoConfiguration.class)
public class MainAppConfiguration extends SpringBootServletInitializer {

}

Thus, the shared configuration shared is loaded, but can be overridden in the application.properties of the main application.

spring.main.banner-mode ( ), .

+2

, ( , "" - : , "my-core" ).

JAR "my-commons" src/main/resources/application.properties, application.properties "my-core" , my-core/src/main/resources

0

Spring.

src/main/resources/application.properties , . classpath , .

Then with annotation @PropertySourcein other projects that depend on a common module:

@Configuration
@PropertySource("classpath*:META-INF/spring/properties/*.properties")
public class Config {
  ...
}

Or with the XML configuration:

<context:property-placeholder location="classpath*:META-INF/spring/properties/*.properties"/>

It should import all configuration files into the transfer class directory.

In addition, you should know that you cannot have two identical files in the classpath. You will have a conflict.

For example, this situation will create a conflict :

Project A (depends on project B):

  • src/main/resources/application.properties

Project B:

  • src/main/resources/application.properties

You will need to rename the file application.propertiesor put it in another directory.

0
source

All Articles