Minwoo Kang
Minwoo Kang

Reputation: 85

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.

Upvotes: 6

Views: 26361

Answers (4)

Subhash Gaikwad
Subhash Gaikwad

Reputation: 51

Please go through with following link -

https://medium.com/@khandkesoham7/profiling-with-spring-boot-408b33f8a25f

that guy has explained well

and to verify the or command for generate the build follow the following command

mvn -Ppreprod package -DskipTests clean verify

Upvotes: 0

Ryuzaki L
Ryuzaki L

Reputation: 40008

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

Upvotes: 5

Haagenti
Haagenti

Reputation: 7387

You have only configured the default profile. You need to set an active profile like spring.profiles.active=dev in your app properties. For injecting current active profile,

@Value("${spring.profiles.active}")
private String currentActiveEnv;

Upvotes: 0

Hasan Can Saral
Hasan Can Saral

Reputation: 3288

You set your default profile fallback to dev with spring.profiles.default=dev. If you had spring.profiles.active=someOtherProfile, you would have someOtherProfile activated.

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

This log says that dev profile is the active one, since you don't have any explicit profile activated.

Upvotes: 0

Related Questions