Browse Source
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
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4334 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/merge
5 changed files with 165 additions and 72 deletions
@ -1,75 +1,108 @@
@@ -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 org.junit.Test; |
||||
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 |
||||
// -- tests relating to customizing reserved default profiles ----------------------
|
||||
|
||||
@Test |
||||
public void noop() { |
||||
public void control() { |
||||
Environment env = new AbstractEnvironment() { }; |
||||
assertThat(env.acceptsProfiles(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME), is(true)); |
||||
} |
||||
/* |
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
public |
||||
static @interface MyEnvironment { } |
||||
|
||||
/** |
||||
* 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 withNoReservedDefaultProfile() { |
||||
class CustomEnvironment extends AbstractEnvironment { |
||||
@Override |
||||
protected Set<String> getReservedDefaultProfiles() { |
||||
return Collections.emptySet(); |
||||
} |
||||
return super.accepts(object); |
||||
} |
||||
} |
||||
|
||||
@MyEnvironment |
||||
static class CandidateWithCustomAnnotation { } |
||||
|
||||
static class CandidateWithoutCustomAnnotation { } |
||||
|
||||
@Test |
||||
public void subclassOfAbstractEnvironment() { |
||||
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
|
||||
Environment env = new CustomEnvironment(); |
||||
assertThat(env.acceptsProfiles(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME), is(false)); |
||||
} |
||||
|
||||
static class CustomDefaultEnvironment extends DefaultEnvironment { |
||||
@Override |
||||
public boolean accepts(Object object) { |
||||
if (object instanceof Class<?>) { |
||||
return ((Class<?>)object).isAnnotationPresent(MyEnvironment.class); |
||||
@Test |
||||
public void withSingleCustomReservedDefaultProfile() { |
||||
class CustomEnvironment extends AbstractEnvironment { |
||||
@Override |
||||
protected Set<String> getReservedDefaultProfiles() { |
||||
return Collections.singleton("rd1"); |
||||
} |
||||
return super.accepts(object); |
||||
} |
||||
|
||||
Environment env = new CustomEnvironment(); |
||||
assertThat(env.acceptsProfiles(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME), is(false)); |
||||
assertThat(env.acceptsProfiles("rd1"), is(true)); |
||||
} |
||||
|
||||
@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
|
||||
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(); |
||||
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)); |
||||
} |
||||
*/ |
||||
|
||||
|
||||
// -- tests relating to customizing property sources -------------------------------
|
||||
} |
||||
|
||||
Loading…
Reference in new issue