You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support AND operator in @Profile annotation [SPR-12458] #17063
has: votes-jiraIssues migrated from JIRA with more than 10 votes at the time of importin: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement
As of v4.1.2 @Profile annotation does not support AND operator, being OR by definition.
This significantly limits it's applicability in our project since we have profiles: PROD, TEST, CONFIG1, CONFIG2, etc. And we want to have beans autowired based in PROD && CONFIG1 style to be able to setup config1 in production and TEST && CONFIG1 to setup same profile for test.
The good example would be config being MQENABLED profile. Which in case of production will be set on @Configuration class with RabbitTemplate bean configuration, but for test we want to have our own implementation of AmqpTemplate (instead of RabbitTemplate).
We are developing a spring boot application to run in WebSphere, with our test suite running in Jetty using @WebIntegrationTest.
We would like to be able to specify some beans (eg transaction managers) to be different between environments that run in Websphere, and those running on Jetty.
As the set of environments that are not running on WebSphere is the smaller and more static of the two, the neatest way to do this would be something like the following, which is currently not possible.
@Bean
@Profile({"!lcl && !ci"})
public JtaTransactionManager transactionManager() {
return new WebSphereUowTransactionManager();
}
Indeed, implementing a custom condition along those lines is what I would recommend at this point.
As for 4.3, it's primarily a design question: How to express 'and' semantics in @Profile itself? Custom parsing of expressions would go a bit far... It might be more appropriate to introduce an enum on the annotation, indicating 'and' vs 'or' semantics for multiple profiles specified. In combination with making @Profile repeatable, this would deliver quite an amount of expressiveness already. Everything beyond that I would rather leave to custom programmatic condition implementations.
FWIW, @Profile can already be repeated when declared as a meta-annotation on multiple composed annotations (even though it's not technically a repeatable annotation).
Spring Boot assumes AND semantics are desired when negated profiles are combined with non-negated profiles:
If both negated and non-negated profiles are specified for a single document, at least one non-negated profile must match and no negated profiles may match.
We have a similar requirement and our solution was to have a @Repeatable(Profiles.class) @Profile which otherwise has the same implementation as that of org.springframework.context.annotation.Profile
Then @Profiles does an AND while the default behavior for @Profile remains OR .
FWIW the multiple @Profile approach won't be much use for spring-projects/spring-boot#12469. That may or may not sway the design but I'd personally prefer it if the same syntax could be used in the @Profile annotation and the application.properties file.
I was thinking something like a simple + might do the trick. e.g:
@Profile("p1", "p2+p3")
Of course, that could break back compatibility if an existing profile happened to have a + in it.
FWIW, I don't like the repeatable approach because its semantic isn't as explicit as a profile expression. I wonder how likely + is going to be used in a profile name.
@snicoll I'm just thinking the presence of more complicated boolean operators in the context of YAML loading makes the properties file unintelligible. While I'm happy to use that in @Profile (typically on a single bean), I worry about YAML files like:
What is the value of someProp in the above? The more profiles you add the worse this can be. And you have to remember that the last matching set of profiles wins in terms of the property being set.
Less flexible to be sure, but I think a bit more readable when you have to scan a document full of these and try to reason about what properties are going to be loaded for a given set of profiles. Regardless of your implementation with @Profile I'm hoping Boot opts for a lesser version of that functionality for the sake of a simpler usability story with regards to the YAML feature.
I've had a crack at solving this in my branch here. I know that Juergen Hoeller commented that "Custom parsing of expressions would go a bit far..." but I think it might be the cleanest way to support it in the end. I've tried to keep the parser really simple. It supports &| and ! for local and, or and not expressions. There's no support operator precedence, so you can't do "A & B | C" but you can use parenthesis to make more complex expressions. e.g. "(A & B) | C". The parser itself is only 150 lines or so.
I've also hidden the parser behind a Profiles interface so people can plug in their own parsing if they wish.
The nice thing about the parser approach is we automatically some consistency. You can use it in an annotation (@Profile("A & B")) or in XML <beans profiles="A & B">. With a small change to Spring Boot we can support the same syntax in application.yml.
Taylor S. Wicksell, I know you were originally looking for some different properties to keep the YAML sane but I think I actually the expression syntax personally. I also think that spring.profiles.any and spring.profiles.all might be too restrictive. With the expression you can do things like this:
—
spring.profiles: production & (us-east | us-west)
...
—
spring.profiles: production & (eu-central | eu-west)
...
—
spring.profiles: !producton
...
What do people think? Is it adding too much complexity? Does it go far enough?
Another element I hadn't noticed so far is that the XML parser uses spaces, commas or semi-colons to separate profiles (i.e. profile="prod,dev"> means that either prod or dev should be active). This is a problem when you use an expression the way it is described at the moment as the space is interpreted as a separate profile, so prod & dev is parsed as prod or & or dev.
To avoid the espace of the logical AND operator, I've renamed it to + (as already discussed above). As for the XML parsing issue, I've updated the XSD to mention that expressions must not have spaces in them.
We had this plan of checking if a profile had the same name as an expression for backward compatibility reason. Looking at the current arrangement, that would probably increase the complexity a lot. However, not being able to parse profiles the same way as before (with only ! handling) can be problematic for those willing to keep this simple arrangement for the time being.
I'll do one more pass with Juergen and report here.
So we've decided to not support profile expressions in XML, leaving acceptProfiles(String...) as it is in a deprecated fashion. This means the format as originaly proposed will be available in a 5.1 snapshot in half an hour or so.
has: votes-jiraIssues migrated from JIRA with more than 10 votes at the time of importin: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement
Activity
spring-projects-issues commentedon Sep 18, 2015
Vladimir Rozhkov commented
+1 for this. Had to write simple condition matcher to get job done:
It will be nice to have expressions support in
@Profileannotation.spring-projects-issues commentedon Oct 7, 2015
Stephen Bellchester commented
We are developing a spring boot application to run in WebSphere, with our test suite running in Jetty using
@WebIntegrationTest.We would like to be able to specify some beans (eg transaction managers) to be different between environments that run in Websphere, and those running on Jetty.
As the set of environments that are not running on WebSphere is the smaller and more static of the two, the neatest way to do this would be something like the following, which is currently not possible.
spring-projects-issues commentedon Feb 25, 2016
Martin Wegner commented
I would use the acceptsProfiles() method for the condition implementation:
spring-projects-issues commentedon Feb 25, 2016
Juergen Hoeller commented
Indeed, implementing a custom condition along those lines is what I would recommend at this point.
As for 4.3, it's primarily a design question: How to express 'and' semantics in
@Profileitself? Custom parsing of expressions would go a bit far... It might be more appropriate to introduce an enum on the annotation, indicating 'and' vs 'or' semantics for multiple profiles specified. In combination with making@Profilerepeatable, this would deliver quite an amount of expressiveness already. Everything beyond that I would rather leave to custom programmatic condition implementations.Juergen
spring-projects-issues commentedon Feb 26, 2016
Sam Brannen commented
FWIW,
@Profilecan already be repeated when declared as a meta-annotation on multiple composed annotations (even though it's not technically a repeatable annotation).spring-projects-issues commentedon Mar 18, 2016
Juergen Hoeller commented
In the meantime, I'll rather leave this up to 5.0, along with other condition-related fine tuning there.
spring-projects-issues commentedon Oct 12, 2016
Jared Jacobs commented
Spring Boot assumes AND semantics are desired when negated profiles are combined with non-negated profiles:
— Spring Boot Reference Guide 24.6.3
I wish
@Profilewould behave the same way. This is the only situation where I’ve ever needed AND semantics.spring-projects-issues commentedon Jan 26, 2017
Juergen Hoeller commented
Stéphane Nicoll, assigning this to you for further consideration... in particular in alignment with Boot 2.0.
spring-projects-issues commentedon Mar 7, 2018
DJ Kulkarni commented
We have a similar requirement and our solution was to have a
@Repeatable(Profiles.class) @Profilewhich otherwise has the same implementation as that oforg.springframework.context.annotation.ProfileThen
@Profilesdoes an AND while the default behavior for@Profileremains OR .So now we can do-
Which translates to-
p1 AND (p2 OR p3)
spring-projects-issues commentedon Mar 7, 2018
Sam Brannen commented
That sounds like an elegant custom solution, DJ Kulkarni.
spring-projects-issues commentedon Mar 7, 2018
Juergen Hoeller commented
Indeed... Let's consider rolling a variant of this into 5.1.
spring-projects-issues commentedon Mar 7, 2018
Eric Deandrea commented
Let me see if our company rules allow us to contribute our solution back (DJ works with me...).
spring-projects-issues commentedon Mar 13, 2018
Phil Webb commented
See also spring-projects/spring-boot#12469 in case we want to align solutions.
spring-projects-issues commentedon Mar 27, 2018
Phil Webb commented
FWIW the multiple
@Profileapproach won't be much use for spring-projects/spring-boot#12469. That may or may not sway the design but I'd personally prefer it if the same syntax could be used in the@Profileannotation and theapplication.propertiesfile.I was thinking something like a simple
+might do the trick. e.g:Of course, that could break back compatibility if an existing profile happened to have a
+in it.spring-projects-issues commentedon Mar 28, 2018
Stéphane Nicoll commented
FWIW, I don't like the repeatable approach because its semantic isn't as explicit as a profile expression. I wonder how likely
+is going to be used in a profile name.7 remaining items
spring-projects-issues commentedon Jun 12, 2018
Taylor S. Wicksell commented
@snicollI'm just thinking the presence of more complicated boolean operators in the context of YAML loading makes the properties file unintelligible. While I'm happy to use that in@Profile(typically on a single bean), I worry about YAML files like:What is the value of someProp in the above? The more profiles you add the worse this can be. And you have to remember that the last matching set of profiles wins in terms of the property being set.
What I do hope to see from Boot is:
Less flexible to be sure, but I think a bit more readable when you have to scan a document full of these and try to reason about what properties are going to be loaded for a given set of profiles. Regardless of your implementation with
@ProfileI'm hoping Boot opts for a lesser version of that functionality for the sake of a simpler usability story with regards to the YAML feature.spring-projects-issues commentedon Jun 13, 2018
Phil Webb commented
I've had a crack at solving this in my branch here. I know that Juergen Hoeller commented that "Custom parsing of expressions would go a bit far..." but I think it might be the cleanest way to support it in the end. I've tried to keep the parser really simple. It supports
&|and!for localand,orandnotexpressions. There's no support operator precedence, so you can't do"A & B | C"but you can use parenthesis to make more complex expressions. e.g."(A & B) | C". The parser itself is only 150 lines or so.I've also hidden the parser behind a
Profilesinterface so people can plug in their own parsing if they wish.The nice thing about the parser approach is we automatically some consistency. You can use it in an annotation (
@Profile("A & B")) or in XML<beans profiles="A & B">. With a small change to Spring Boot we can support the same syntax inapplication.yml.Taylor S. Wicksell, I know you were originally looking for some different properties to keep the YAML sane but I think I actually the expression syntax personally. I also think that
spring.profiles.anyandspring.profiles.allmight be too restrictive. With the expression you can do things like this:What do people think? Is it adding too much complexity? Does it go far enough?
spring-projects-issues commentedon Jun 13, 2018
David Harrigan commented
Thank you Phil, I like it :)
spring-projects-issues commentedon Jun 13, 2018
Eric Deandrea commented
I like it! Something we've been waiting for now for a long time! Nice job!
spring-projects-issues commentedon Jun 13, 2018
Stéphane Nicoll commented
Phil Webb thank you very much, I like it and this addresses a significant amount of my concerns and raise one I hadn't identified so far.
I've polished your initial submission in e168c35 and improved the support in fa83bcbf.
&is a reserved keyword in XML so the profile expression there is quite weirdAnother element I hadn't noticed so far is that the XML parser uses spaces, commas or semi-colons to separate profiles (i.e.
profile="prod,dev">means that eitherprodordevshould be active). This is a problem when you use an expression the way it is described at the moment as the space is interpreted as a separate profile, soprod & devis parsed asprodor&ordev.To avoid the espace of the logical AND operator, I've renamed it to
+(as already discussed above). As for the XML parsing issue, I've updated the XSD to mention that expressions must not have spaces in them.We had this plan of checking if a profile had the same name as an expression for backward compatibility reason. Looking at the current arrangement, that would probably increase the complexity a lot. However, not being able to parse profiles the same way as before (with only
!handling) can be problematic for those willing to keep this simple arrangement for the time being.I'll do one more pass with Juergen and report here.
spring-projects-issues commentedon Jun 15, 2018
Stéphane Nicoll commented
So we've decided to not support profile expressions in XML, leaving
acceptProfiles(String...)as it is in a deprecated fashion. This means the format as originaly proposed will be available in a 5.1 snapshot in half an hour or so.Thanks a lot for the help on this Phil!
Introduce Environment.matchesProfiles() for profile expressions
Introduce Environment.matchesProfiles() for profile expressions