Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review Default Profile section in the reference documentation #30319

Closed
manueljordan opened this issue Apr 12, 2023 · 8 comments
Closed

Review Default Profile section in the reference documentation #30319

manueljordan opened this issue Apr 12, 2023 · 8 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Milestone

Comments

@manueljordan
Copy link

In the current reference documentation exists the Default Profile section. It has the following content

Starting with a code:

@Configuration
@Profile("default")
public class DefaultDataConfig {

    @Bean
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder()
            .setType(EmbeddedDatabaseType.HSQL)
            .addScript("classpath:com/bank/config/sql/schema.sql")
            .build();
    }
}

and the following content/explanation:

If no profile is active, the dataSource is created. You can see this as a way to provide a default definition for one or more beans.
If any profile is enabled, the default profile does not apply.

You can change the name of the default profile by using setDefaultProfiles() on the Environment or,
declaratively, by using the spring.profiles.default property.

Therefore: Why from the beginning was not used directly spring.profiles.active? - it especially taking in consideration that is known that spring.profiles.active has more precedence and overrides by complete spring.profiles.default as indicated in bold above.

As summary:

  • Why does spring.profiles.default exist if the same approach can be accomplished through spring.profiles.active?

Is not clear when is mandatory use the former over the latter - and taking the special consideration about the behavior of overriding if the latter is declared.

Just in case, the @Profile javadoc does not contain some indication about this

I create a post on SO at:

But I think the explanation from the source (here) should be expanded. Thanks for your understanding

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 12, 2023
@rstoyanchev rstoyanchev added type: documentation A documentation task in: core Issues in core modules (aop, beans, core, context, expression) labels Apr 18, 2023
@quaff
Copy link
Contributor

quaff commented Jul 27, 2023

It's not clear what will happen if both spring.profiles.active and spring.profiles.default are provided, I suspect that spring.profiles.default is ignored if spring.profiles.active present.
And what's the differences between @Profile("default") and absence of @Profile?

@snicoll
Copy link
Member

snicoll commented Nov 29, 2023

I don't understand the problem to be honest.

spring.profile.default defines the name of the default profile(s). That doc states as much:

The name of the default profile is default. You can change the name of the default profile by using setDefaultProfiles() on the Environment or, declaratively, by using the spring.profiles.default property.

So if you set it, you just change the name of the default profile. This doesn't do anything else as described above.

if both spring.profiles.active and spring.profiles.default are provided, I suspect that spring.profiles.default is ignored if spring.profiles.active present.

If you customize the name of the default profile and the you active a profile explicitly, those two things are unrelated. As already stated in the doc, activating a profile means that the default profile fallback does not apply.

And what's the differences between @Profile("default") and absence of @Profile?

That's also unrelated. If you don't have a profile at all, beans are always created. If you have a profile that matches the name of the default profile, it's considered only if no profile is active.

I've polished the section a bit with an explicit link to what it means to activate a profile. If you want to refine, feel free to submit a PR and we can continue the conversation.

@snicoll snicoll removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 29, 2023
@snicoll snicoll self-assigned this Nov 29, 2023
@snicoll snicoll added this to the 6.1.2 milestone Nov 29, 2023
@snicoll snicoll changed the title Explain: Why "spring.profiles.default" exists if "spring.profiles.active" can do the same? Review Default Profile section in the reference documentation Nov 29, 2023
@quaff
Copy link
Contributor

quaff commented Nov 30, 2023

It seems that spring.profiles.default can do everything we need, why there is another spring.profiles.active?

@snicoll
Copy link
Member

snicoll commented Nov 30, 2023

Default only set a name to use as a fallback. It’s a mechanism to allow you to enable a profile if no other profile is active. Active predates support for default profile. These are not the same as, as soon as you want certain profiles to he enabled, the default won’t be.

I am sorry but I don’t know where the confusion comes from.

@quaff
Copy link
Contributor

quaff commented Nov 30, 2023

For example, if we defined profile foobar, then there are two profiles : foobar and implicit default, if we want to activate foobar, we could set spring.profiles.default=foobar, no need for spring.profiles.active=foobar, that means spring.profiles.active is not necessary.

@bclozel
Copy link
Member

bclozel commented Nov 30, 2023

I don't think this discussion is going anywhere.

Making the case that spring.profiles.active is useless in some cases and that spring.profiles.default is enough does not mean that it's not useful. You've just shown that sometimes, technically, it's not needed. We could also make the opposite argument: we could enforce that Spring Framework does not let you change the default profile (always set as "default") and you should only use spring.profiles.active.

Let's say a company ships a product "myproduct" with customizations for "customerA" and "customerB" that are enabled by Spring profiles. Beans for the vanilla product should be named "default" if we want them to be contributed to the app, unless a "customerA" or "customerB" profile is chosen by the user. It would be nice to have those named "myproduct" as "default" is really generic. If we remove this possibility, developers would rightfully complain that:

  • they can't change the name of the default profile to "myproduct" and have to use "default"
  • they don't like the fact that they would need to set "myproduct" as active profile by default as it's not a good getting started experience. It should only be required if you choose a particular customer.
  • this was working since Introduce a reserved default profile name [SPR-8203] #12852 and taking it away seems unfair given this has introduced zero bug nor maintenance issue

@quaff
Copy link
Contributor

quaff commented Nov 30, 2023

I'm not saying current design is problematic, I'm saying it confuse people like me.
I suggest the document should be revised to state that both spring.profiles.active and spring.profiles.default could activate profiles and the former is recommended, and share a real use case of spring.profiles.default.

@rossapi

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

7 participants