Skip to content

Commit bcd44f3

Browse files
committed
Support not (!) operator for profile selection
The following syntax is now supported <beans profile="p1,!p2"> @Profile("p1", "!p2") indicating that the <beans> element or annotated component should be processed only if profile 'p1' is active or profile 'p2' is not active. Issue: SPR-8728
1 parent e72c49f commit bcd44f3

File tree

6 files changed

+115
-47
lines changed

6 files changed

+115
-47
lines changed

spring-beans/src/main/resources/org/springframework/beans/factory/xml/spring-beans-3.1.xsd

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,28 @@
8282
<xsd:attribute name="profile" use="optional" type="xsd:string">
8383
<xsd:annotation>
8484
<xsd:documentation><![CDATA[
85-
The set of profiles for which this <beans> element may be parsed. Multiple profiles can be
86-
separated by any number of spaces, commas, or semi-colons (or indeed any mixture of the three).
87-
If one or more of the specified profiles are active at time of parsing, the <beans> element
88-
will be parsed, and all of its <bean> elements registered, &lt;import&gt; elements followed,
89-
etc. If none of the specified profiles are active at time of parsing, then the entire element
90-
and its contents will be ignored.
85+
The set of profiles for which this <beans> element should be parsed. Multiple profiles
86+
can be separated by spaces, commas, or semi-colons.
87+
88+
If one or more of the specified profiles are active at time of parsing, the <beans>
89+
element will be parsed, and all of its <bean> elements registered, &lt;import&gt;
90+
elements followed, etc. If none of the specified profiles are active at time of
91+
parsing, then the entire element and its contents will be ignored.
92+
93+
If a profile is prefixed with the NOT operator '!', e.g.
94+
95+
<beans profile="p1,!p2">
96+
97+
indicates that the <beans> element should be parsed if profile "p1" is active or
98+
if profile "p2" is not active.
9199
92100
Profiles are activated in one of two ways:
93101
Programmatic:
94102
ConfigurableEnvironment#setActiveProfiles(String...)
95103
ConfigurableEnvironment#setDefaultProfiles(String...)
96104
97-
Properties (typically through -D system properties, environment variables, or servlet context init params):
105+
Properties (typically through -D system properties, environment variables, or
106+
servlet context init params):
98107
spring.profiles.active=p1,p2
99108
spring.profiles.default=p1,p2
100109
]]></xsd:documentation>

spring-beans/src/main/resources/org/springframework/beans/factory/xml/spring-beans-3.2.xsd

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,28 @@
8282
<xsd:attribute name="profile" use="optional" type="xsd:string">
8383
<xsd:annotation>
8484
<xsd:documentation><![CDATA[
85-
The set of profiles for which this <beans> element may be parsed. Multiple profiles can be
86-
separated by any number of spaces, commas, or semi-colons (or indeed any mixture of the three).
87-
If one or more of the specified profiles are active at time of parsing, the <beans> element
88-
will be parsed, and all of its <bean> elements registered, &lt;import&gt; elements followed,
89-
etc. If none of the specified profiles are active at time of parsing, then the entire element
90-
and its contents will be ignored.
85+
The set of profiles for which this <beans> element should be parsed. Multiple profiles
86+
can be separated by spaces, commas, or semi-colons.
87+
88+
If one or more of the specified profiles are active at time of parsing, the <beans>
89+
element will be parsed, and all of its <bean> elements registered, &lt;import&gt;
90+
elements followed, etc. If none of the specified profiles are active at time of
91+
parsing, then the entire element and its contents will be ignored.
92+
93+
If a profile is prefixed with the NOT operator '!', e.g.
94+
95+
<beans profile="p1,!p2">
96+
97+
indicates that the <beans> element should be parsed if profile "p1" is active or
98+
if profile "p2" is not active.
9199
92100
Profiles are activated in one of two ways:
93101
Programmatic:
94102
ConfigurableEnvironment#setActiveProfiles(String...)
95103
ConfigurableEnvironment#setDefaultProfiles(String...)
96104
97-
Properties (typically through -D system properties, environment variables, or servlet context init params):
105+
Properties (typically through -D system properties, environment variables, or
106+
servlet context init params):
98107
spring.profiles.active=p1,p2
99108
spring.profiles.default=p1,p2
100109
]]></xsd:documentation>

spring-context/src/main/java/org/springframework/context/annotation/Profile.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,36 +25,44 @@
2525
import org.springframework.core.env.ConfigurableEnvironment;
2626

2727
/**
28-
* Indicates that a component is eligible for registration when one or more {@linkplain #value
29-
* specified profiles} are active.
28+
* Indicates that a component is eligible for registration when one or more {@linkplain
29+
* #value specified profiles} are active.
3030
*
31-
* <p>A <em>profile</em> is a named logical grouping that may be activated programatically via
32-
* {@link ConfigurableEnvironment#setActiveProfiles} or declaratively through setting the
33-
* {@link AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME spring.profiles.active} property,
34-
* usually through JVM system properties, as an environment variable, or for web applications
35-
* as a Servlet context parameter in {@code web.xml}.
31+
* <p>A <em>profile</em> is a named logical grouping that may be activated
32+
* programmatically via {@link ConfigurableEnvironment#setActiveProfiles} or declaratively
33+
* through setting the {@link AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
34+
* spring.profiles.active} property, usually through JVM system properties, as an
35+
* environment variable, or for web applications as a Servlet context parameter in
36+
* {@code web.xml}.
3637
*
3738
* <p>The {@code @Profile} annotation may be used in any of the following ways:
3839
* <ul>
39-
* <li>as a type-level annotation on any class directly or indirectly annotated with
40-
* {@code @Component}, including {@link Configuration @Configuration} classes
41-
* <li>as a meta-annotation, for the purpose of composing custom stereotype annotations
40+
* <li>as a type-level annotation on any class directly or indirectly annotated with
41+
* {@code @Component}, including {@link Configuration @Configuration} classes
42+
* <li>as a meta-annotation, for the purpose of composing custom stereotype annotations
4243
* </ul>
4344
*
4445
* <p>If a {@code @Configuration} class is marked with {@code @Profile}, all of the
45-
* {@code @Bean} methods and {@link Import @Import} annotations associated with that class will
46-
* be bypassed unless one or more the specified profiles are active. This is very similar to
47-
* the behavior in Spring XML: if the {@code profile} attribute of the {@code beans} element is
48-
* supplied e.g., {@code <beans profile="p1,p2">}, the {@code beans} element will not be parsed unless
49-
* profiles 'p1' and/or 'p2' have been activated. Likewise, if a {@code @Component} or
50-
* {@code @Configuration} class is marked with <code>@Profile({"p1", "p2"})</code>, that class will
51-
* not be registered/processed unless profiles 'p1' and/or 'p2' have been activated.
46+
* {@code @Bean} methods and {@link Import @Import} annotations associated with that class
47+
* will be bypassed unless one or more the specified profiles are active. This is very
48+
* similar to the behavior in Spring XML: if the {@code profile} attribute of the
49+
* {@code beans} element is supplied e.g., {@code <beans profile="p1,p2">}, the
50+
* {@code beans} element will not be parsed unless profiles 'p1' and/or 'p2' have been
51+
* activated. Likewise, if a {@code @Component} or {@code @Configuration} class is marked
52+
* with {@code @Profile({"p1", "p2"})}, that class will not be registered/processed unless
53+
* profiles 'p1' and/or 'p2' have been activated.
5254
*
53-
* <p>If the {@code @Profile} annotation is omitted, registration will occur, regardless of which,
54-
* if any, profiles are active.
55+
* <p>If a given profile is prefixed with the NOT operator ({@code !}), the annotated
56+
* will be registered if the profile is <em>not</em> active. e.g., for
57+
* {@code @Profile({"p1", "!p2"})}, registration will occur if profile 'p1' is active or
58+
* if profile 'p2' is not active.
5559
*
56-
* <p>When defining Spring beans via XML, the {@code "profile"} attribute of the {@code <beans>}
57-
* element may be used. See the documentation in {@code spring-beans-3.1.xsd} for details.
60+
* <p>If the {@code @Profile} annotation is omitted, registration will occur, regardless
61+
* of which (if any) profiles are active.
62+
*
63+
* <p>When defining Spring beans via XML, the {@code "profile"} attribute of the
64+
* {@code <beans>} element may be used. See the documentation in
65+
* {@code spring-beans} XSD (version 3.1 or greater) for details.
5866
*
5967
* @author Chris Beams
6068
* @since 3.1

spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.util.StringUtils;
3232

3333
import static java.lang.String.*;
34+
3435
import static org.springframework.util.StringUtils.*;
3536

3637
/**
@@ -300,31 +301,43 @@ public void setDefaultProfiles(String... profiles) {
300301

301302
public boolean acceptsProfiles(String... profiles) {
302303
Assert.notEmpty(profiles, "Must specify at least one profile");
303-
boolean activeProfileFound = false;
304-
Set<String> activeProfiles = this.doGetActiveProfiles();
305-
Set<String> defaultProfiles = this.doGetDefaultProfiles();
306304
for (String profile : profiles) {
307-
this.validateProfile(profile);
308-
if (activeProfiles.contains(profile)
309-
|| (activeProfiles.isEmpty() && defaultProfiles.contains(profile))) {
310-
activeProfileFound = true;
311-
break;
305+
if (profile != null && profile.length() > 0 && profile.charAt(0) == '!') {
306+
return !this.isProfileActive(profile.substring(1));
307+
}
308+
if (this.isProfileActive(profile)) {
309+
return true;
312310
}
313311
}
314-
return activeProfileFound;
312+
return false;
313+
}
314+
315+
/**
316+
* Return whether the given profile is active, or if active profiles are empty
317+
* whether the profile should be active by default.
318+
* @throws IllegalArgumentException per {@link #validateProfile(String)}
319+
* @since 3.2
320+
*/
321+
protected boolean isProfileActive(String profile) {
322+
this.validateProfile(profile);
323+
return this.doGetActiveProfiles().contains(profile)
324+
|| (this.doGetActiveProfiles().isEmpty() && this.doGetDefaultProfiles().contains(profile));
315325
}
316326

317327
/**
318328
* Validate the given profile, called internally prior to adding to the set of
319329
* active or default profiles.
320330
* <p>Subclasses may override to impose further restrictions on profile syntax.
321-
* @throws IllegalArgumentException if the profile is null, empty or whitespace-only
331+
* @throws IllegalArgumentException if the profile is null, empty, whitespace-only or
332+
* begins with the profile NOT operator (!).
322333
* @see #acceptsProfiles
323334
* @see #addActiveProfile
324335
* @see #setDefaultProfiles
325336
*/
326337
protected void validateProfile(String profile) {
327338
Assert.hasText(profile, "Invalid profile [" + profile + "]: must contain text");
339+
Assert.isTrue(profile.charAt(0) != '!',
340+
"Invalid profile [" + profile + "]: must not begin with the ! operator");
328341
}
329342

330343
public MutablePropertySources getPropertySources() {

spring-core/src/main/java/org/springframework/core/env/Environment.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ public interface Environment extends PropertyResolver {
100100
/**
101101
* Return whether one or more of the given profiles is active or, in the case of no
102102
* explicit active profiles, whether one or more of the given profiles is included in
103-
* the set of default profiles
103+
* the set of default profiles. If a profile begins with '!' the logic is inverted,
104+
* i.e. the method will return true if the given profile is <em>not</em> active. For
105+
* example, {@code env.acceptsProfiles("p1", "!p2")} will return true if profile 'p1'
106+
* is active or 'p2' is not active.
104107
* @throws IllegalArgumentException if called with zero arguments
105108
* @throws IllegalArgumentException if any profile is null, empty or whitespace-only
106109
* @see #getActiveProfiles

spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package org.springframework.core.env;
1818

1919
import java.lang.reflect.Field;
20+
2021
import java.security.AccessControlException;
2122
import java.security.Permission;
23+
2224
import java.util.Arrays;
2325
import java.util.Collections;
2426
import java.util.Map;
@@ -147,6 +149,11 @@ public void setActiveProfiles_withEmptyProfile() {
147149
environment.setActiveProfiles("");
148150
}
149151

152+
@Test(expected=IllegalArgumentException.class)
153+
public void setActiveProfiles_withNotOperator() {
154+
environment.setActiveProfiles("p1", "!p2");
155+
}
156+
150157
@Test(expected=IllegalArgumentException.class)
151158
public void setDefaultProfiles_withNullProfileArray() {
152159
environment.setDefaultProfiles((String[])null);
@@ -162,6 +169,11 @@ public void setDefaultProfiles_withEmptyProfile() {
162169
environment.setDefaultProfiles("");
163170
}
164171

172+
@Test(expected=IllegalArgumentException.class)
173+
public void setDefaultProfiles_withNotOperator() {
174+
environment.setDefaultProfiles("d1", "!d2");
175+
}
176+
165177
@Test
166178
public void addActiveProfile() {
167179
assertThat(environment.getActiveProfiles().length, is(0));
@@ -284,6 +296,20 @@ public void acceptsProfiles_defaultProfile() {
284296
assertThat(environment.acceptsProfiles("p1"), is(true));
285297
}
286298

299+
@Test
300+
public void acceptsProfiles_withNotOperator() {
301+
assertThat(environment.acceptsProfiles("p1"), is(false));
302+
assertThat(environment.acceptsProfiles("!p1"), is(true));
303+
environment.addActiveProfile("p1");
304+
assertThat(environment.acceptsProfiles("p1"), is(true));
305+
assertThat(environment.acceptsProfiles("!p1"), is(false));
306+
}
307+
308+
@Test(expected=IllegalArgumentException.class)
309+
public void acceptsProfiles_withInvalidNotOperator() {
310+
environment.acceptsProfiles("p1", "!");
311+
}
312+
287313
@Test
288314
public void environmentSubclass_withCustomProfileValidation() {
289315
ConfigurableEnvironment env = new AbstractEnvironment() {

0 commit comments

Comments
 (0)