Spring Boot Externalizing properties not working

I have looked at the below threads and followed things given there. Still my property override is not happening

  1. Spring Boot - Externalized properties
  2. Profile Specific Property Enablement
  3. Spring Boot External Config

I am on Tomcat 8.0.33 and Spring boot starter web and got this in my setenv.sh

export JAVA_OPTS="$JAVA_OPTS -Dlog.level=INFO -Dspring.config.location=file:/opt/jboss/apache-tomcat-8.0.33/overrides/ -Dspring.profiles.active=dev"

And in the overrides folder I got 2 files

1) application.properties 2) application-dev.properties

The application.properties has a single entry in it

spring.profiles.active=dev

I see that the proper log.level is fed to my code which means this command is working. Its just that I am clueless as to why my override is not happening as expected

I don't have any `PropertyPlaceholderConfigurer code in my workspace. I am not even sure if I need 1


Solution 1:

I don't use this method to externalise properties. First, I'll try a suggestion for your method and then I'll show you what I'm using.

The suggestion for your method is to use file:/// instead of file:/ as with Spring I found that when not passing the three slashes after the colon it didn't recognise the property.

I've created a sample project for you, available here with instructions.

Now for the method I use.

I define a Configuration file for each profile and I keep the application.properties file under src/main/resources.

Then I use the @Profile and @PropertySource annotations on each configuration file.

For example:

@Configuration
@Profile("dev")
@PropertySource("file:///${user.home}/.devopsbuddy/application-dev.properties")
public class DevelopmentConfig {

@Bean
public EmailService emailService() {
    return new MockEmailService();
}

@Bean
public ServletRegistrationBean h2ConsoleServletRegistration() {
    ServletRegistrationBean bean = new ServletRegistrationBean(new WebServlet());
    bean.addUrlMappings("/console/*");
    return bean;
}
}

And

@Configuration
@Profile("prod")
@PropertySource("file:///${user.home}/.devopsbuddy/application-prod.properties")
public class ProductionConfig {

@Bean
public EmailService emailService() {
    return new SmtpEmailService();
}
}

I have also got a Configuration file that is valid for all profiles, which I call ApplicationConfig, as follows:

@Configuration
@EnableJpaRepositories(basePackages = "com.devopsbuddy.backend.persistence.repositories")
@EntityScan(basePackages = "com.devopsbuddy.backend.persistence.domain.backend")
@EnableTransactionManagement
@PropertySource("file:///${user.home}/.devopsbuddy/application-common.properties")
public class ApplicationConfig {
}

My src/main/resources/application.properties file looks like the following:

spring.profiles.active=dev
[email protected]
token.expiration.length.minutes=120

Of course I could externalise the spring.profile.active property by passing it as a system property but for my case and for now it's fine.

When running the application, if I pass the "dev" profile, Spring will load all properties and Beans defined in the DevelopmentConfig class plus all those in ApplicationConfig. If I pass "prod", the ProductionConfig and ApplicationConfig properties will be loaded instead.

I'm completing a course on how to create a Spring Boot website with Security, Email, Data JPA, Amazon Web Services, Stripe and much more. If you want, you can register your interest here and you will get notified when the course is open for enrolment.