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

@@ -84,13 +84,13 @@
TODO:SPR-7508: Document profile annotation:
* may be comma-delimited
* empty profile means beans 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
* 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
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>

View File

@@ -86,21 +86,11 @@ public class ProfileXmlBeanDefinitionTests {
@Test
public void testDefaultProfile() {
assertThat(beanFactoryFor(DEFAULT_ELIGIBLE_XML, NONE_ACTIVE), containsTargetBean());
assertThat(beanFactoryFor(DEFAULT_ELIGIBLE_XML, "other"), not(containsTargetBean()));
assertThat(beanFactoryFor(DEFAULT_AND_DEV_ELIGIBLE_XML, DEV_ACTIVE), containsTargetBean());
assertThat(beanFactoryFor(DEFAULT_AND_DEV_ELIGIBLE_XML, NONE_ACTIVE), containsTargetBean());
assertThat(beanFactoryFor(DEFAULT_AND_DEV_ELIGIBLE_XML, PROD_ACTIVE), not(containsTargetBean()));
}
@Test
public void testCustomDefaultProfile() {
{
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
ConfigurableEnvironment env = new DefaultEnvironment();
env.setDefaultProfile("custom-default");
env.setDefaultProfiles("custom-default");
reader.setEnvironment(env);
reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_ELIGIBLE_XML, getClass()));
@@ -110,7 +100,7 @@ public class ProfileXmlBeanDefinitionTests {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
ConfigurableEnvironment env = new DefaultEnvironment();
env.setDefaultProfile("custom-default");
env.setDefaultProfiles("custom-default");
reader.setEnvironment(env);
reader.loadBeanDefinitions(new ClassPathResource(CUSTOM_DEFAULT_ELIGIBLE_XML, getClass()));
@@ -118,6 +108,44 @@ public class ProfileXmlBeanDefinitionTests {
}
}
@Test
public void testDefaultAndNonDefaultProfile() {
assertThat(beanFactoryFor(DEFAULT_ELIGIBLE_XML, NONE_ACTIVE), not(containsTargetBean()));
assertThat(beanFactoryFor(DEFAULT_ELIGIBLE_XML, "other"), not(containsTargetBean()));
{
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
ConfigurableEnvironment env = new DefaultEnvironment();
env.setActiveProfiles(DEV_ACTIVE);
env.setDefaultProfiles("default");
reader.setEnvironment(env);
reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_AND_DEV_ELIGIBLE_XML, getClass()));
assertThat(beanFactory, containsTargetBean());
}
{
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
ConfigurableEnvironment env = new DefaultEnvironment();
// env.setActiveProfiles(DEV_ACTIVE);
env.setDefaultProfiles("default");
reader.setEnvironment(env);
reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_AND_DEV_ELIGIBLE_XML, getClass()));
assertThat(beanFactory, containsTargetBean());
}
{
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
ConfigurableEnvironment env = new DefaultEnvironment();
// env.setActiveProfiles(DEV_ACTIVE);
//env.setDefaultProfiles("default");
reader.setEnvironment(env);
reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_AND_DEV_ELIGIBLE_XML, getClass()));
assertThat(beanFactory, not(containsTargetBean()));
}
}
private BeanDefinitionRegistry beanFactoryFor(String xmlName, String... activeProfiles) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);