mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
Revise documentation for @ActiveProfiles and ActiveProfilesResolver
See gh-36269
See gh-36600
(cherry picked from commit 99b78adce3)
This commit is contained in:
+1
-1
@@ -74,7 +74,7 @@ and registering it by using the `resolver` attribute of `@ActiveProfiles`.
|
||||
|
||||
NOTE: When `@ActiveProfiles` is declared on a test class, the `spring.profiles.active`
|
||||
property (whether configured as a JVM system property or environment variable) is not
|
||||
taken into account by the Test Context Framework when determining active profiles. If
|
||||
taken into account by the TestContext Framework when determining active profiles. If
|
||||
you need to allow `spring.profiles.active` to override the profiles configured via
|
||||
`@ActiveProfiles`, you can implement a custom `ActiveProfilesResolver` as described in
|
||||
xref:testing/testcontext-framework/ctx-management/env-profiles.adoc[Context Configuration with Environment Profiles].
|
||||
|
||||
+25
-15
@@ -545,8 +545,8 @@ Kotlin::
|
||||
|
||||
The following example demonstrates how to implement and register a custom
|
||||
`SystemPropertyOverrideActiveProfilesResolver` that allows the `spring.profiles.active`
|
||||
property (whether configured as a JVM system property or environment variable) to
|
||||
override profiles configured via `@ActiveProfiles`:
|
||||
property (when configured as a JVM system property) to override profiles configured via
|
||||
`@ActiveProfiles`:
|
||||
|
||||
[tabs]
|
||||
======
|
||||
@@ -583,35 +583,45 @@ Kotlin::
|
||||
======
|
||||
Java::
|
||||
+
|
||||
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
||||
[source,java,indent=0,subs="verbatim,quotes",role="primary",fold="-imports"]
|
||||
----
|
||||
public class SystemPropertyOverrideActiveProfilesResolver implements ActiveProfilesResolver {
|
||||
import org.springframework.core.env.AbstractEnvironment;
|
||||
import org.springframework.test.context.support.DefaultActiveProfilesResolver;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class SystemPropertyOverrideActiveProfilesResolver extends DefaultActiveProfilesResolver {
|
||||
|
||||
@Override
|
||||
public String[] resolve(Class<?> testClass) {
|
||||
String profiles = System.getProperty("spring.profiles.active");
|
||||
if (profiles != null && !profiles.isBlank()) {
|
||||
return profiles.split(",");
|
||||
String profiles = System.getProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME);
|
||||
if (StringUtils.hasText(profiles)) {
|
||||
return StringUtils.commaDelimitedListToStringArray(
|
||||
StringUtils.trimAllWhitespace(profiles));
|
||||
}
|
||||
return new DefaultActiveProfilesResolver().resolve(testClass);
|
||||
return super.resolve(testClass);
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
Kotlin::
|
||||
+
|
||||
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
||||
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary",fold="-imports"]
|
||||
----
|
||||
class SystemPropertyOverrideActiveProfilesResolver : ActiveProfilesResolver {
|
||||
import org.springframework.core.env.AbstractEnvironment
|
||||
import org.springframework.test.context.support.DefaultActiveProfilesResolver
|
||||
import org.springframework.util.StringUtils
|
||||
|
||||
class SystemPropertyOverrideActiveProfilesResolver : DefaultActiveProfilesResolver() {
|
||||
|
||||
override fun resolve(testClass: Class<*>): Array<String> {
|
||||
val profiles = System.getProperty("spring.profiles.active")
|
||||
if (!profiles.isNullOrBlank()) {
|
||||
return profiles.split(",").toTypedArray()
|
||||
val profiles = System.getProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME)
|
||||
if (StringUtils.hasText(profiles)) {
|
||||
return StringUtils.commaDelimitedListToStringArray(
|
||||
StringUtils.trimAllWhitespace(profiles)
|
||||
)
|
||||
}
|
||||
return DefaultActiveProfilesResolver().resolve(testClass)
|
||||
return super.resolve(testClass)
|
||||
}
|
||||
}
|
||||
----
|
||||
======
|
||||
|
||||
|
||||
@@ -38,8 +38,9 @@ import org.springframework.core.annotation.AliasFor;
|
||||
* See {@link NestedTestConfiguration @NestedTestConfiguration} for details.
|
||||
*
|
||||
* <p>Note that when {@code @ActiveProfiles} is declared on a test class, the
|
||||
* {@code spring.profiles.active} property (whether configured as a JVM system
|
||||
* property or environment variable) is not taken into account by the Test Context
|
||||
* {@link org.springframework.core.env.AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
|
||||
* spring.profiles.active} property (whether configured as a JVM system property
|
||||
* or environment variable) is not taken into account by the Spring TestContext
|
||||
* Framework when determining active profiles. If you need to allow
|
||||
* {@code spring.profiles.active} to override the profiles configured via
|
||||
* {@code @ActiveProfiles}, you can implement a custom {@link ActiveProfilesResolver}
|
||||
@@ -81,6 +82,9 @@ public @interface ActiveProfiles {
|
||||
/**
|
||||
* The type of {@link ActiveProfilesResolver} to use for resolving the active
|
||||
* bean definition profiles programmatically.
|
||||
* <p>If not specified, the
|
||||
* {@link org.springframework.test.context.support.DefaultActiveProfilesResolver
|
||||
* DefaultActiveProfilesResolver} will be used.
|
||||
* @since 4.0
|
||||
* @see ActiveProfilesResolver
|
||||
*/
|
||||
|
||||
+3
-2
@@ -30,7 +30,8 @@ package org.springframework.test.context;
|
||||
* @author Sam Brannen
|
||||
* @author Michail Nikolaev
|
||||
* @since 4.0
|
||||
* @see ActiveProfiles
|
||||
* @see ActiveProfiles @ActiveProfiles
|
||||
* @see org.springframework.test.context.support.DefaultActiveProfilesResolver DefaultActiveProfilesResolver
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ActiveProfilesResolver {
|
||||
@@ -41,7 +42,7 @@ public interface ActiveProfilesResolver {
|
||||
* @param testClass the test class for which the profiles should be resolved;
|
||||
* never {@code null}
|
||||
* @return the bean definition profiles to use when loading the
|
||||
* {@code ApplicationContext}; never {@code null}
|
||||
* {@code ApplicationContext}; never {@code null} but potentially empty
|
||||
* @see ActiveProfiles#resolver
|
||||
* @see ActiveProfiles#inheritProfiles
|
||||
*/
|
||||
|
||||
+6
-4
@@ -32,10 +32,12 @@ import static org.springframework.test.context.TestContextAnnotationUtils.findAn
|
||||
* configured declaratively via {@link ActiveProfiles#profiles} or
|
||||
* {@link ActiveProfiles#value}.
|
||||
*
|
||||
* <p>Note that the {@code spring.profiles.active} property (whether configured
|
||||
* as a JVM system property or environment variable) is not taken into account by
|
||||
* this resolver. If you need to allow {@code spring.profiles.active} to override
|
||||
* profiles configured via {@link ActiveProfiles}, you can implement a custom
|
||||
* <p>Note that the
|
||||
* {@link org.springframework.core.env.AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
|
||||
* spring.profiles.active} property (whether configured as a JVM system property
|
||||
* or environment variable) is not taken into account by this resolver. If you need
|
||||
* to allow {@code spring.profiles.active} to override profiles configured via
|
||||
* {@link ActiveProfiles @ActiveProfiles}, you can implement a custom
|
||||
* {@link ActiveProfilesResolver} and register it via {@link ActiveProfiles#resolver}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
|
||||
Reference in New Issue
Block a user