Skip to content
Advertisement

spring boot not loading default profile while using spring.profile.default in application.properties

FYI : I swear there is no activating profile configuration such as -D or run configuration

Goal

When application is booted up without any activating profile, the dev profile is activated as a default.

Problem

I’ve set spring.profile.default = dev , and I would expect that the dev profile is activated. But it is not.

What I did

Run Environment

Spring-Boot-version : 2.1.2 Release

What I'm referred

1) How to use profiles in Spring Boot Application – https://www.javacodegeeks.com/2019/07/profiles-spring-boot-application.html#respond

Here’s Code What I did

/resources/application.properties

spring.profiles.default= dev

application.environment=This is a "Default" Environment

/resources/application-dev.properties

application.environment=This is a "dev" Environment
server.port= 8082

/ProfileController.java

@RestController
@RequestMapping("/v1")
public class ProfileController {

    @Value("${application.environment}")
    private String applicationEnv;

    @GetMapping
    public String getApplicationEnv(){
        return applicationEnv;
    }
}

Result

localhost/v1 => This is a “Default” Environment

And

I’ve found out the default profile is set up as dev correctly.

This is my spring-boot log.

2019-10-16 23:17:02.926 INFO 64099 — [ main] c.e.s.SpringbootdemoappApplication : No active profile set, falling back to default profiles: dev

Add another log for server port

2019-10-17 00:25:03.837 INFO 68318 — [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8083 (http)

The reason that I add is it doesn’t seem a problem related with Injection.

Time to Close this question

The first goal that I want to achieve is change default profile. From spring docs(https://docs.spring.io/spring/docs/4.2.0.RELEASE/spring-framework-reference/htmlsingle/#beans-definition-profiles-default), default profile can be changed as setting spring.profiles.default in application.properties.

But it seems kind of bug (Thanks @Antoniossss). Even if I’ve set that in application.properties and the console showed No active profile set, falling back to default profiles: dev. However still dev profile wasn’t activated.

The thing that I’ve found out is changing default profile should be done before loading application.properties.

It means if changing default profile is described in application.properties, it’s too late.(IDK why though, I can’t figure out because there are so many layers in Spring …) If défaut profile is set up using -Dspring.default.profile = dev , it’s working properly.

From https://github.com/spring-projects/spring-boot/issues/1219:

You can’t change the default profile by declaring it in a config file. It has to be in place before the config files are read.

Advertisement

Answer

The problem is you are passing this properties after application is loaded, you need to provide this property while application is booting up

Pass it as JVM args

java -jar -Dspring.profiles.default=dev myproject.jar

Pass as environment variable

java -jar myproject.jar --spring.profiles.default=dev

System variable

SPRING_PROFILES_DEFAULT=dev
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement