Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inherit application.properties in Spring?

If I create a commons library having an application.properties defining common configurations. Like:

spring.main.banner-mode=off

How can I inherit these properties into another project where I include those 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-commons to my-core?

like image 339
membersound Avatar asked Jun 23 '17 10:06

membersound


People also ask

How do I get a list of spring boot application properties?

To see all properties in your Spring Boot application, enable the Actuator endpoint called env . This enables an HTTP endpoint which shows all the properties of your application's environment. You can do this by setting the property management.

How do you change application properties at runtime spring boot?

To change properties in a file during runtime, we should place that file somewhere outside the jar. Then we tell Spring where it is with the command-line parameter –spring. config. location=file://{path to file}.

Where does spring look for application properties?

Spring Boot Framework comes with a built-in mechanism for application configuration using a file called application. properties. It is located inside the src/main/resources folder, as shown in the following figure.

How do I get environment properties in spring boot?

Environment-Specific Properties File. If we need to target different environments, there's a built-in mechanism for that in Boot. We can simply define an application-environment. properties file in the src/main/resources directory, and then set a Spring profile with the same environment name.


1 Answers

Solution is to include the shared properties using a different name, here application-shared.properties

In shared library:

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

In main app:

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

}

This way the commons/shared config gets loaded, but is though able to be overridden in application.properties of main app.

It does not work with the spring.main.banner-mode property (don't know why), but with all other properties it worked well.

like image 197
membersound Avatar answered Oct 13 '22 16:10

membersound