Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Inherit application.properties from dependency

Let's say I have 5 Spring Boot Projects. All of them have a Maven dependency on a Spring Boot project No 6 with some shared/common classes. 5 independent projects have a lot of common properties assigned at each application.properties, which I'd like to abstract and move them to common project. Overall it looks like this:

                                            Project 1 (app.properties) Common Project (app-common.properties) <--- Project 2 (app.properties)                                             Project 3 (app.properties)... 

Current problem is that app-common.properties is inside project1.jar/lib/common-project.jar and app-common.properties apparently do not load upon startup.

Is there a way to extend it from a dependency?

CommonProject Main class looks like this:

@SpringBootApplication public class CommonApplication extends SpringBootServletInitializer {      protected static void run(SpringApplication application, String[] args) {         application.run(args);     } } 

Project1 Main class looks like this:

public class Project1 extends CommonApplication {      public static void main(String[] args) {         run(new SpringApplication(Project1.class), args);     } } 
like image 608
Mikhail Kholodkov Avatar asked Feb 26 '16 23:02

Mikhail Kholodkov


People also ask

How do I reference an application property in Spring boot?

Another way to read application properties in the Spring Boot application is to use the @ConfigurationProperties annotation. To do that, we will need to create a Plain Old Java Object where each class field matches the name of the key in a property file.

How do I get property properties from Spring boot?

Another method to access values defined in Spring Boot is by autowiring the Environment object and calling the getProperty() method to access the value of a property file.

How do I override application properties in Spring boot?

To override your Spring Boot application properties when it's running on Kubernetes, just set environment variables on the container. To set an environment variable on a container, first, initialise a ConfigMap containing the environment variables that you want to override.

How read properties file from external folder in Spring boot?

Actually, the most easiest way is to put application. properties and your. jar into the same directory, and just java -jar your. jar will automatically load this external config file.


2 Answers

Use PropertySource annotation and provide two sources for your app:

@PropertySources({         @PropertySource("classpath:app-common.properties"),         @PropertySource("classpath:app.properties")     }) 

more details can be found there https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

like image 106
Anton Avatar answered Nov 10 '22 08:11

Anton


Currently spring boot doesn't provide inheriting properties.

A spring boot application supports multiple property sources but the convention (read: built-in logic) for xxx.properties is to resolve the last xxx.properties if there are multiple properties files having the same file name.

There are many solution to this.

One possible solution is to

  1. apply a custom profile to the dependency
  2. include inheritable settings in the application-customprofile.properties
  3. have the dependent(s) set spring.profiles.include=customprofile in application[-{profile}].properties (note: if set in application.properties, it applies for all profiles)

Another possible solution is to use a unique custom filename for the properties.

  • i.e. instead of using the default application.properties, use common.properties
like image 43
Mathew Avatar answered Nov 10 '22 07:11

Mathew