Eliminate reserved 'default' profile (SPR-7778)

There is no longer a reserved default profile named 'default'. Rather,
users must explicitly specify a default profile or profiles via

    ConfigurableEnvironment.setDefaultProfiles(String...)
        - or -
    spring.profile.default="pD1,pD2"

Per above, the setDefaultProfile(String) method now accepts a variable
number of profile names (one or more).  This is symmetrical with the
existing setActiveProfiles(String...) method.

A typical scenario might involve setting both a default profile as a
servlet context property in web.xml and then setting an active profile
when deploying to production.
This commit is contained in:
Chris Beams
2010-12-08 07:59:25 +00:00
parent e693d9fa58
commit b3e36a335d
8 changed files with 103 additions and 82 deletions

View File

@@ -27,12 +27,13 @@ import java.lang.annotation.Target;
* TODO SPR-7508: document
*
* Components not @Profile-annotated will always be registered
* @Profile("default") means that beans will be registered unless other profile(s) are active
* @Profile({"xyz,default"}) means that beans will be registered if 'xyz' is active or if no profile is active
* ConfigurableEnvironment.setDefaultProfileName(String) customizes the name of the default profile
* 'spring.profile.default' property customizes the name of the default profile (usually for use as a servlet context/init param)
* ConfigurableEnvironment.setActiveProfiles(String...) sets which profiles are active
* 'spring.profile.active' sets which profiles are active (typically as a -D system property)
servlet context/init param)
* ConfigurableEnvironment.setDefaultProfiles(String...) or 'spring.profile.default' property specifies one
or more default profiles, e.g., 'default'
* if 'default' is specified as a default profile, @Profile({"xyz,default}) means that beans will be
registered if 'xyz' is active or if no profile is active
*
* @author Chris Beams
* @since 3.1

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 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.
@@ -29,7 +29,6 @@ import java.util.regex.Pattern;
import org.aspectj.lang.annotation.Aspect;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.DefaultEnvironment;
import org.springframework.core.type.filter.AnnotationTypeFilter;
@@ -59,6 +58,7 @@ public class ClassPathScanningCandidateComponentProviderTests {
private static final String TEST_BASE_PACKAGE = "example.scannable";
private static final String TEST_PROFILE_PACKAGE = "example.profilescan";
private static final String TEST_DEFAULT_PROFILE_NAME = "testDefault";
@Test
@@ -219,6 +219,7 @@ public class ClassPathScanningCandidateComponentProviderTests {
@Test
public void testIntegrationWithAnnotationConfigApplicationContext_defaultProfile() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setDefaultProfiles(TEST_DEFAULT_PROFILE_NAME);
// no active profiles are set
ctx.register(DefaultProfileAnnotatedComponent.class);
ctx.refresh();
@@ -231,6 +232,7 @@ public class ClassPathScanningCandidateComponentProviderTests {
String beanName = DefaultAndDevProfileAnnotatedComponent.BEAN_NAME;
{
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setDefaultProfiles(TEST_DEFAULT_PROFILE_NAME);
// no active profiles are set
ctx.register(beanClass);
ctx.refresh();
@@ -238,6 +240,7 @@ public class ClassPathScanningCandidateComponentProviderTests {
}
{
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setDefaultProfiles(TEST_DEFAULT_PROFILE_NAME);
ctx.getEnvironment().setActiveProfiles("dev");
ctx.register(beanClass);
ctx.refresh();
@@ -245,6 +248,7 @@ public class ClassPathScanningCandidateComponentProviderTests {
}
{
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setDefaultProfiles(TEST_DEFAULT_PROFILE_NAME);
ctx.getEnvironment().setActiveProfiles("other");
ctx.register(beanClass);
ctx.refresh();
@@ -263,13 +267,13 @@ public class ClassPathScanningCandidateComponentProviderTests {
}
@Profile(AbstractEnvironment.DEFAULT_PROFILE_NAME)
@Profile(TEST_DEFAULT_PROFILE_NAME)
@Component(DefaultProfileAnnotatedComponent.BEAN_NAME)
private static class DefaultProfileAnnotatedComponent {
static final String BEAN_NAME = "defaultProfileAnnotatedComponent";
}
@Profile({AbstractEnvironment.DEFAULT_PROFILE_NAME,"dev"})
@Profile({TEST_DEFAULT_PROFILE_NAME,"dev"})
@Component(DefaultAndDevProfileAnnotatedComponent.BEAN_NAME)
private static class DefaultAndDevProfileAnnotatedComponent {
static final String BEAN_NAME = "defaultAndDevProfileAnnotatedComponent";