Support default profile (SPR-7508, SPR-7778)

'default' is now a reserved profile name, indicating
that any beans defined within that profile will be registered
unless another profile or profiles have been activated.

Examples below are expressed in XML, but apply equally when
using the @Profile annotation.

EXAMPLE 1:

        <beans>
            <beans profile="default">
                <bean id="foo" class="com.acme.EmbeddedFooImpl"/>
            </beans>
            <beans profile="production">
                <bean id="foo" class="com.acme.ProdFooImpl"/>
            </beans>
        </beans>

    In the case above, the EmbeddedFooImpl 'foo' bean will be
    registered if:
        a) no profile is active
        b) the 'default' profile has explicitly been made active

    The ProdFooImpl 'foo' bean will be registered if the 'production'
    profile is active.

EXAMPLE 2:

        <beans profile="default,xyz">
            <bean id="foo" class="java.lang.String"/>
        </beans>

    Bean 'foo' will be registered if any of the following are true:
        a) no profile is active
        b) 'xyz' profile is active
        c) 'default' profile has explicitly been made active
        d) both (b) and (c) are true

Note that the default profile is not to be confused with specifying no
profile at all.  When the default profile is specified, beans are
registered only if no other profiles are active; whereas when no profile
is specified, bean definitions are always registered regardless of which
profiles are active.

The default profile may be configured programmatically:

    environmnent.setDefaultProfile("embedded");

or declaratively through any registered PropertySource, e.g. system properties:

    -DdefaultSpringProfile=embedded

Assuming either of the above, example 1 could be rewritten as follows:

        <beans>
            <beans profile="embedded">
                <bean id="foo" class="com.acme.EmbeddedFooImpl"/>
            </beans>
            <beans profile="production">
                <bean id="foo" class="com.acme.ProdFooImpl"/>
            </beans>
        </beans>

It is unlikely that use of the default profile will make sense in
conjunction with a statically specified 'springProfiles' property.
For example, if 'springProfiles' is specified as a web.xml context
param, that profile will always be active for that application,
negating the possibility of default profile bean definitions ever
being registered.

The default profile is most useful for ensuring that a valid set of
bean definitions will always be registered without forcing users
to explictly specify active profiles.  In the embedded vs. production
examples above, it is assumed that the application JVM will be started
with -DspringProfiles=production when the application is in fact in
a production environment.  Otherwise, the embedded/default profile bean
definitions will always be registered.
This commit is contained in:
Chris Beams
2010-12-01 09:01:58 +00:00
parent b33da670e5
commit 5062dc31af
15 changed files with 249 additions and 129 deletions

View File

@@ -29,6 +29,7 @@ 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;
@@ -215,8 +216,44 @@ public class ClassPathScanningCandidateComponentProviderTests {
assertThat(ctx.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(false));
}
private boolean containsBeanClass(Set<BeanDefinition> candidates, Class beanClass) {
for (Iterator it = candidates.iterator(); it.hasNext();) {
@Test
public void testIntegrationWithAnnotationConfigApplicationContext_defaultProfile() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
// no active profiles are set
ctx.register(DefaultProfileAnnotatedComponent.class);
ctx.refresh();
assertThat(ctx.containsBean(DefaultProfileAnnotatedComponent.BEAN_NAME), is(true));
}
@Test
public void testIntegrationWithAnnotationConfigApplicationContext_defaultAndDevProfile() {
Class<?> beanClass = DefaultAndDevProfileAnnotatedComponent.class;
String beanName = DefaultAndDevProfileAnnotatedComponent.BEAN_NAME;
{
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
// no active profiles are set
ctx.register(beanClass);
ctx.refresh();
assertThat(ctx.containsBean(beanName), is(true));
}
{
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles("dev");
ctx.register(beanClass);
ctx.refresh();
assertThat(ctx.containsBean(beanName), is(true));
}
{
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles("other");
ctx.register(beanClass);
ctx.refresh();
assertThat(ctx.containsBean(beanName), is(false));
}
}
private boolean containsBeanClass(Set<BeanDefinition> candidates, Class<?> beanClass) {
for (Iterator<BeanDefinition> it = candidates.iterator(); it.hasNext();) {
ScannedGenericBeanDefinition definition = (ScannedGenericBeanDefinition) it.next();
if (beanClass.getName().equals(definition.getBeanClassName())) {
return true;
@@ -225,4 +262,16 @@ public class ClassPathScanningCandidateComponentProviderTests {
return false;
}
@Profile(AbstractEnvironment.DEFAULT_PROFILE_NAME)
@Component(DefaultProfileAnnotatedComponent.BEAN_NAME)
private static class DefaultProfileAnnotatedComponent {
static final String BEAN_NAME = "defaultProfileAnnotatedComponent";
}
@Profile({AbstractEnvironment.DEFAULT_PROFILE_NAME,"dev"})
@Component(DefaultAndDevProfileAnnotatedComponent.BEAN_NAME)
private static class DefaultAndDevProfileAnnotatedComponent {
static final String BEAN_NAME = "defaultAndDevProfileAnnotatedComponent";
}
}