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
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -25,36 +25,44 @@ import org.springframework.core.env.AbstractEnvironment;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
|
||||
/**
|
||||
* Indicates that a component is eligible for registration when one or more {@linkplain #value
|
||||
* specified profiles} are active.
|
||||
* Indicates that a component is eligible for registration when one or more {@linkplain
|
||||
* #value specified profiles} are active.
|
||||
*
|
||||
* <p>A <em>profile</em> is a named logical grouping that may be activated programatically via
|
||||
* {@link ConfigurableEnvironment#setActiveProfiles} or declaratively through setting the
|
||||
* {@link AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME spring.profiles.active} property,
|
||||
* usually through JVM system properties, as an environment variable, or for web applications
|
||||
* as a Servlet context parameter in {@code web.xml}.
|
||||
* <p>A <em>profile</em> is a named logical grouping that may be activated
|
||||
* programmatically via {@link ConfigurableEnvironment#setActiveProfiles} or declaratively
|
||||
* through setting the {@link AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
|
||||
* spring.profiles.active} property, usually through JVM system properties, as an
|
||||
* environment variable, or for web applications as a Servlet context parameter in
|
||||
* {@code web.xml}.
|
||||
*
|
||||
* <p>The {@code @Profile} annotation may be used in any of the following ways:
|
||||
* <ul>
|
||||
* <li>as a type-level annotation on any class directly or indirectly annotated with
|
||||
* {@code @Component}, including {@link Configuration @Configuration} classes
|
||||
* <li>as a meta-annotation, for the purpose of composing custom stereotype annotations
|
||||
* <li>as a type-level annotation on any class directly or indirectly annotated with
|
||||
* {@code @Component}, including {@link Configuration @Configuration} classes
|
||||
* <li>as a meta-annotation, for the purpose of composing custom stereotype annotations
|
||||
* </ul>
|
||||
*
|
||||
* <p>If a {@code @Configuration} class is marked with {@code @Profile}, all of the
|
||||
* {@code @Bean} methods and {@link Import @Import} annotations associated with that class will
|
||||
* be bypassed unless one or more the specified profiles are active. This is very similar to
|
||||
* the behavior in Spring XML: if the {@code profile} attribute of the {@code beans} element is
|
||||
* supplied e.g., {@code <beans profile="p1,p2">}, the {@code beans} element will not be parsed unless
|
||||
* profiles 'p1' and/or 'p2' have been activated. Likewise, if a {@code @Component} or
|
||||
* {@code @Configuration} class is marked with <code>@Profile({"p1", "p2"})</code>, that class will
|
||||
* not be registered/processed unless profiles 'p1' and/or 'p2' have been activated.
|
||||
* {@code @Bean} methods and {@link Import @Import} annotations associated with that class
|
||||
* will be bypassed unless one or more the specified profiles are active. This is very
|
||||
* similar to the behavior in Spring XML: if the {@code profile} attribute of the
|
||||
* {@code beans} element is supplied e.g., {@code <beans profile="p1,p2">}, the
|
||||
* {@code beans} element will not be parsed unless profiles 'p1' and/or 'p2' have been
|
||||
* activated. Likewise, if a {@code @Component} or {@code @Configuration} class is marked
|
||||
* with {@code @Profile({"p1", "p2"})}, that class will not be registered/processed unless
|
||||
* profiles 'p1' and/or 'p2' have been activated.
|
||||
*
|
||||
* <p>If the {@code @Profile} annotation is omitted, registration will occur, regardless of which,
|
||||
* if any, profiles are active.
|
||||
* <p>If a given profile is prefixed with the NOT operator ({@code !}), the annotated
|
||||
* will be registered if the profile is <em>not</em> active. e.g., for
|
||||
* {@code @Profile({"p1", "!p2"})}, registration will occur if profile 'p1' is active or
|
||||
* if profile 'p2' is not active.
|
||||
*
|
||||
* <p>When defining Spring beans via XML, the {@code "profile"} attribute of the {@code <beans>}
|
||||
* element may be used. See the documentation in {@code spring-beans-3.1.xsd} for details.
|
||||
* <p>If the {@code @Profile} annotation is omitted, registration will occur, regardless
|
||||
* of which (if any) profiles are active.
|
||||
*
|
||||
* <p>When defining Spring beans via XML, the {@code "profile"} attribute of the
|
||||
* {@code <beans>} element may be used. See the documentation in
|
||||
* {@code spring-beans} XSD (version 3.1 or greater) for details.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @since 3.1
|
||||
|
||||
Reference in New Issue
Block a user