Spring Expression Language - set different value if Profile is set

JoeG :

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 :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=93388&siteId=1