Introduce reserved default profile support

AbstractEnvironment and subclasses now register a reserved default
profile named literally 'default' such that with no action on the part
of the user, beans defined against the 'default' profile will be
registered - if no other profiles are explicitly activated.

For example, given the following three files a.xml, b.xml and c.xml:

    a.xml
    -----
    <beans> <!-- no 'profile' attribute -->
        <bean id="a" class="com.acme.A"/>
    </beans>

    b.xml
    -----
    <beans profile="default">
        <bean id="b" class="com.acme.B"/>
    </beans>

    c.xml
    -----
    <beans profile="custom">
        <bean id="c" class="com.acme.C"/>
    </beans>

bootstrapping all of the files in a Spring ApplicationContext as
follows will result in beans 'a' and 'b', but not 'c' being registered:

    ApplicationContext ctx = new GenericXmlApplicationContext();
    ctx.load("a.xml");
    ctx.load("b.xml");
    ctx.load("c.xml");
    ctx.refresh();
    ctx.containsBean("a"); // true
    ctx.containsBean("b"); // true
    ctx.containsBean("c"); // false

whereas activating the 'custom' profile will result in beans 'a' and
'c', but not 'b' being registered:

    ApplicationContext ctx = new GenericXmlApplicationContext();
    ctx.load("a.xml");
    ctx.load("b.xml");
    ctx.load("c.xml");
    ctx.getEnvironment().setActiveProfiles("custom");
    ctx.refresh();
    ctx.containsBean("a"); // true
    ctx.containsBean("b"); // false
    ctx.containsBean("c"); // true

that is, once the 'custom' profile is activated, beans defined against
the the reserved default profile are no longer registered. Beans not
defined against any profile ('a') are always registered regardless of
which profiles are active, and of course beans registered
against specific active profiles ('c') are registered.

The reserved default profile is, in practice, just another 'default
profile', as might be added through calling env.setDefaultProfiles() or
via the 'spring.profiles.default' property.  The only difference is that
the reserved default is added automatically by AbstractEnvironment
implementations.  As such, any call to setDefaultProfiles() or value set
for the 'spring.profiles.default' will override the reserved default
profile.  If a user wishes to add their own default profile while
keeping the reserved default profile as well, it will need to be
explicitly redeclared, e.g.:

    env.addDefaultProfiles("my-default-profile", "default")

The reserved default profile(s) are determined by the value returned
from AbstractEnvironment#getReservedDefaultProfiles().  This protected
method may be overridden by subclasses in order to customize the
set of reserved default profiles.

Issue: SPR-8203
This commit is contained in:
Chris Beams
2011-05-20 03:49:15 +00:00
parent 415057c184
commit c4a13507f0
5 changed files with 167 additions and 74 deletions

View File

@@ -1,75 +1,108 @@
/*
* Copyright 2002-2011 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.env;
import org.junit.Ignore;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
/**
* Unit tests covering the extensibility of AbstractEnvironment
* Unit tests covering the extensibility of {@link AbstractEnvironment}.
*
* @author Chris Beams
* @since 3.1
*/
public class CustomEnvironmentTests {
@Ignore
@Test
public void noop() {
}
/*
@Retention(RetentionPolicy.RUNTIME)
public
static @interface MyEnvironment { }
// -- tests relating to customizing reserved default profiles ----------------------
/**
* A custom {@link Environment} that evaluates class literals
* for the presence of a custom annotation.
* /
static class CustomEnvironment extends AbstractEnvironment {
@Override
public boolean accepts(Object object) {
if (object instanceof Class<?>) {
return ((Class<?>)object).isAnnotationPresent(MyEnvironment.class);
@Test
public void control() {
Environment env = new AbstractEnvironment() { };
assertThat(env.acceptsProfiles(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME), is(true));
}
@Test
public void withNoReservedDefaultProfile() {
class CustomEnvironment extends AbstractEnvironment {
@Override
protected Set<String> getReservedDefaultProfiles() {
return Collections.emptySet();
}
return super.accepts(object);
}
Environment env = new CustomEnvironment();
assertThat(env.acceptsProfiles(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME), is(false));
}
@MyEnvironment
static class CandidateWithCustomAnnotation { }
@Test
public void withSingleCustomReservedDefaultProfile() {
class CustomEnvironment extends AbstractEnvironment {
@Override
protected Set<String> getReservedDefaultProfiles() {
return Collections.singleton("rd1");
}
}
static class CandidateWithoutCustomAnnotation { }
Environment env = new CustomEnvironment();
assertThat(env.acceptsProfiles(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME), is(false));
assertThat(env.acceptsProfiles("rd1"), is(true));
}
@Test
public void subclassOfAbstractEnvironment() {
public void withMultiCustomReservedDefaultProfile() {
class CustomEnvironment extends AbstractEnvironment {
@Override
@SuppressWarnings("serial")
protected Set<String> getReservedDefaultProfiles() {
return new HashSet<String>() {{ add("rd1"); add("rd2"); }};
}
}
ConfigurableEnvironment env = new CustomEnvironment();
env.setActiveProfiles("test");
assertThat(env.accepts(CandidateWithCustomAnnotation.class), is(true));
assertThat(env.accepts(CandidateWithoutCustomAnnotation.class), is(false));
assertThat(env.accepts("test"), is(true)); // AbstractEnvironment always returns true
assertThat(env.accepts(new Object()), is(true)); // AbstractEnvironment always returns true
assertThat(env.acceptsProfiles(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME), is(false));
assertThat(env.acceptsProfiles("rd1", "rd2"), is(true));
// finally, issue additional assertions to cover all combinations of calling these
// methods, however unlikely.
env.setDefaultProfiles("d1");
assertThat(env.acceptsProfiles("rd1", "rd2"), is(false));
assertThat(env.acceptsProfiles("d1"), is(true));
env.setActiveProfiles("a1", "a2");
assertThat(env.acceptsProfiles("d1"), is(false));
assertThat(env.acceptsProfiles("a1", "a2"), is(true));
env.setActiveProfiles();
assertThat(env.acceptsProfiles("d1"), is(true));
assertThat(env.acceptsProfiles("a1", "a2"), is(false));
env.setDefaultProfiles();
assertThat(env.acceptsProfiles(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME), is(false));
assertThat(env.acceptsProfiles("rd1", "rd2"), is(false));
assertThat(env.acceptsProfiles("d1"), is(false));
assertThat(env.acceptsProfiles("a1", "a2"), is(false));
}
static class CustomDefaultEnvironment extends DefaultEnvironment {
@Override
public boolean accepts(Object object) {
if (object instanceof Class<?>) {
return ((Class<?>)object).isAnnotationPresent(MyEnvironment.class);
}
return super.accepts(object);
}
}
@Test
public void subclassOfDefaultEnvironment() {
ConfigurableEnvironment env = new CustomDefaultEnvironment();
env.setActiveProfiles("test");
assertThat(env.accepts(CandidateWithCustomAnnotation.class), is(true));
assertThat(env.accepts(CandidateWithoutCustomAnnotation.class), is(false));
assertThat(env.accepts("test"), is(true)); // delegates to DefaultEnvironment
assertThat(env.accepts("bogus"), is(false)); // delegates to DefaultEnvironment
assertThat(env.accepts(new Object()), is(false)); // delegates to DefaultEnvironment
}
*/
// -- tests relating to customizing property sources -------------------------------
}

View File

@@ -30,6 +30,7 @@ import static org.junit.matchers.JUnitMatchers.hasItem;
import static org.junit.matchers.JUnitMatchers.hasItems;
import static org.springframework.core.env.AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME;
import static org.springframework.core.env.AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME;
import static org.springframework.core.env.AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME;
import java.lang.reflect.Field;
import java.security.AccessControlException;
@@ -79,6 +80,16 @@ public class EnvironmentTests {
assertThat(activeProfiles.length, is(2));
}
@Test
public void reservedDefaultProfile() {
assertThat(environment.getDefaultProfiles(), equalTo(new String[]{RESERVED_DEFAULT_PROFILE_NAME}));
System.setProperty(DEFAULT_PROFILES_PROPERTY_NAME, "d0");
assertThat(environment.getDefaultProfiles(), equalTo(new String[]{"d0"}));
environment.setDefaultProfiles("d1", "d2");
assertThat(environment.getDefaultProfiles(), equalTo(new String[]{"d1","d2"}));
System.getProperties().remove(DEFAULT_PROFILES_PROPERTY_NAME);
}
@Test
public void getActiveProfiles_systemPropertiesEmpty() {
assertThat(environment.getActiveProfiles().length, is(0));
@@ -113,7 +124,7 @@ public class EnvironmentTests {
@Test
public void getDefaultProfiles() {
assertThat(environment.getDefaultProfiles().length, is(0));
assertThat(environment.getDefaultProfiles(), equalTo(new String[] {RESERVED_DEFAULT_PROFILE_NAME}));
environment.getPropertySources().addFirst(new MockPropertySource().withProperty(DEFAULT_PROFILES_PROPERTY_NAME, "pd1"));
assertThat(environment.getDefaultProfiles().length, is(1));
assertThat(Arrays.asList(environment.getDefaultProfiles()), hasItem("pd1"));