3

I have a class like this:

public AbstractConfig() {
    super(DataConfig.MGR_NAME);
}

Inside DataConfig there is:

    public final String MGR_NAME = "theManager";

I find SpEL documentation confusing. Is there a way I can change a value if a Spring Profile is set? That is, if I have the Profile "AlternateManager" use theManagerAlt, but default to theManager otherwise?

While I made up this notation to get an active profile, I am hoping there exists some syntax like that below to make this work:

    @Value("#PROFILE['AlternateManager'] ? 'theManagerAlt' : 'theManager' ")
    public final String MGR_NAME;
JoeG
  • 7,191
  • 10
  • 60
  • 105

3 Answers3

4

ANSWER:

After a lot of googling and playing around, finally found it. Hopefully, this will be of use to others! This works:

@Value("#{environment.acceptsProfiles('AlternateManager') ? 'theManagerAlt' : 'theManager' }")

The String can not be final, but I can live with that.

Seems a shame to me that this is not documented well.
This OLD ticket: https://jira.spring.io/browse/SPR-9037 linked me to an old SO question which gave me the answer.

That ticket is essentially a request for better documentation.
IMO, everyone should vote for that OPEN, 6-year-old ticket.

catch23
  • 17,519
  • 42
  • 144
  • 217
JoeG
  • 7,191
  • 10
  • 60
  • 105
0

If AlternateManager is set use it otherwise use the manager(default)

@Value("#{PROFILE['AlternateManager'] ?: 'theManager' }")
public final String MGR_NAME; 
Khwaja Sanjari
  • 455
  • 5
  • 17
  • If it wasn't clear, I think I made up that notation of "PROFILE" - it is not in the docs anywhere I could find. I tried your suggested syntax anyway and it blows up when trying to process "PROFILE" – JoeG Oct 15 '18 at 17:26
  • Please refer https://www.baeldung.com/spring-value-defaults. 5.Using SpEL – Khwaja Sanjari Oct 15 '18 at 17:30
  • Yes, that tells how to get a value from any system properties that were set. My question is can you use the setting of a Spring profile as a boolean (i.e. whether or not a given profile is 'active') within a SpEL expression. I can't figure out how... – JoeG Oct 15 '18 at 17:48
  • Based on what your trying to do I suggest looking into @ ConditionalOnProperty or @ ConditionalOnExpression or defining custom Conditions to set the property. – Khwaja Sanjari Oct 15 '18 at 18:04
0

If AbstractConfig is a Spring bean, you can write a Configuration class in which you initialize the bean with the expected parameter depending on whether your 'AlternateManager' profile is active or not :

@Configuration
public class ExampleConfiguration {

  // If AlternateManager profile is enabled, Spring Bean will be initialized with "theManagerAlt"
  @Bean
  @Profile("AlternateManager")
  public AbstractConfig getDevDataSource() {
    return new AbstractConfig("theManagerAlt");
  }

  // if AlternateManager profile is not enabled, Spring Bean will be initialized with "theManager"
  @Bean
  @Profile("!AlternateManager")
  public AbstractConfig getProdDataSource() {
      return new AbstractConfig("theManager");
  }
}
Sébastien Helbert
  • 2,185
  • 13
  • 22
  • Gave you the bounty because I didn't want it to go to waste - it would have simply expired otherwise! Your answer pointed me towards some better searching that I think helped me find the answer in the end! – JoeG Oct 24 '18 at 19:09
  • Glad to know that I could help you. And thank you for the bounty! – Sébastien Helbert Oct 26 '18 at 11:53