@Override public void merge(ConfigurableEnvironment parent) { for (PropertySource<?> ps : parent.getPropertySources()) { if (!this.propertySources.contains(ps.getName())) { this.propertySources.addLast(ps); } } String[] parentActiveProfiles = parent.getActiveProfiles(); if (!ObjectUtils.isEmpty(parentActiveProfiles)) { synchronized (this.activeProfiles) { for (String profile : parentActiveProfiles) { this.activeProfiles.add(profile); } } } String[] parentDefaultProfiles = parent.getDefaultProfiles(); if (!ObjectUtils.isEmpty(parentDefaultProfiles)) { synchronized (this.defaultProfiles) { this.defaultProfiles.remove(RESERVED_DEFAULT_PROFILE_NAME); for (String profile : parentDefaultProfiles) { this.defaultProfiles.add(profile); } } } }
@Test(expected = IllegalArgumentException.class) public void defaultProfileWithCircularPlaceholder() { System.setProperty(DEFAULT_PROFILES_PROPERTY_NAME, "${spring.profiles.default}"); try { environment.getDefaultProfiles(); } finally { System.getProperties().remove(DEFAULT_PROFILES_PROPERTY_NAME); } }
@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 defaultProfilesContainsDefaultProfileByDefault() { assertThat(environment.getDefaultProfiles().length, is(1)); assertThat(environment.getDefaultProfiles()[0], equalTo("default")); }
@Test public void setDefaultProfiles() { environment.setDefaultProfiles(); assertThat(environment.getDefaultProfiles().length, is(0)); environment.setDefaultProfiles("pd1"); assertThat(Arrays.asList(environment.getDefaultProfiles()), hasItem("pd1")); environment.setDefaultProfiles("pd2", "pd3"); assertThat(Arrays.asList(environment.getDefaultProfiles()), not(hasItem("pd1"))); assertThat(Arrays.asList(environment.getDefaultProfiles()), hasItems("pd2", "pd3")); }
@Override public void merge(ConfigurableEnvironment parent) { for (PropertySource<?> ps : parent.getPropertySources()) { if (!this.propertySources.contains(ps.getName())) { this.propertySources.addLast(ps); } } String[] parentActiveProfiles = parent.getActiveProfiles(); if (!ObjectUtils.isEmpty(parentActiveProfiles)) { synchronized (this.activeProfiles) { for (String profile : parentActiveProfiles) { this.activeProfiles.add(profile); } } } String[] parentDefaultProfiles = parent.getDefaultProfiles(); if (!ObjectUtils.isEmpty(parentDefaultProfiles)) { synchronized (this.defaultProfiles) { this.defaultProfiles.remove(RESERVED_DEFAULT_PROFILE_NAME); for (String profile : parentDefaultProfiles) { this.defaultProfiles.add(profile); } } } }
@Test public void getDefaultProfiles() { 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")); }
/** * Initialize profile information from both the {@link Environment} active * profiles and any {@code spring.profiles.active}/{@code spring.profiles.include} * properties that are already set. */ private void initializeProfiles() { // The default profile for these purposes is represented as null. We add it // first so that it is processed first and has lowest priority. this.profiles.add(null); Set<Profile> activatedViaProperty = getProfilesActivatedViaProperty(); this.profiles.addAll(getOtherActiveProfiles(activatedViaProperty)); // Any pre-existing active profiles set via property sources (e.g. // System properties) take precedence over those added in config files. addActiveProfiles(activatedViaProperty); if (this.profiles.size() == 1) { // only has null profile for (String defaultProfileName : this.environment.getDefaultProfiles()) { Profile defaultProfile = new Profile(defaultProfileName, true); this.profiles.add(defaultProfile); } } }
/** * Called to log active profile information. * @param context the application context */ protected void logStartupProfileInfo(ConfigurableApplicationContext context) { Log log = getApplicationLog(); if (log.isInfoEnabled()) { String[] activeProfiles = context.getEnvironment().getActiveProfiles(); if (ObjectUtils.isEmpty(activeProfiles)) { String[] defaultProfiles = context.getEnvironment().getDefaultProfiles(); log.info("No active profile set, falling back to default profiles: " + StringUtils.arrayToCommaDelimitedString(defaultProfiles)); } else { log.info("The following profiles are active: " + StringUtils.arrayToCommaDelimitedString(activeProfiles)); } } }
.append(System.lineSeparator()); for (String profileName : applicationContext.getEnvironment().getDefaultProfiles()) { messageBuilder .append(" - ")
String[] defaultProfiles = env.getDefaultProfiles();
@Override public String[] getDefaultProfiles() { return delegate.getDefaultProfiles(); }
public void merge(ConfigurableEnvironment parent) { for (PropertySource<?> ps : parent.getPropertySources()) { if (!this.propertySources.contains(ps.getName())) { this.propertySources.addLast(ps); } } for (String profile : parent.getActiveProfiles()) { this.activeProfiles.add(profile); } for (String profile : parent.getDefaultProfiles()) { this.defaultProfiles.add(profile); } }
private StandardEnvironment copyEnvironment(ConfigurableEnvironment input) { StandardEnvironment environment = new StandardEnvironment(); MutablePropertySources capturedPropertySources = environment.getPropertySources(); // Only copy the default property source(s) and the profiles over from the main // environment (everything else should be pristine, just like it was on startup). for (String name : DEFAULT_PROPERTY_SOURCES) { if (input.getPropertySources().contains(name)) { if (capturedPropertySources.contains(name)) { capturedPropertySources.replace(name, input.getPropertySources().get(name)); } else { capturedPropertySources.addLast(input.getPropertySources().get(name)); } } } environment.setActiveProfiles(input.getActiveProfiles()); environment.setDefaultProfiles(input.getDefaultProfiles()); Map<String, Object> map = new HashMap<String, Object>(); map.put("spring.jmx.enabled", false); map.put("spring.main.sources", ""); capturedPropertySources .addFirst(new MapPropertySource(REFRESH_ARGS_PROPERTY_SOURCE, map)); return environment; }
@Override public String[] getDefaultProfiles() { return delegate.getDefaultProfiles(); }
@Override public void merge(ConfigurableEnvironment parent) { for (PropertySource<?> ps : parent.getPropertySources()) { if (!this.propertySources.contains(ps.getName())) { this.propertySources.addLast(ps); } } String[] parentActiveProfiles = parent.getActiveProfiles(); if (!ObjectUtils.isEmpty(parentActiveProfiles)) { synchronized (this.activeProfiles) { for (String profile : parentActiveProfiles) { this.activeProfiles.add(profile); } } } String[] parentDefaultProfiles = parent.getDefaultProfiles(); if (!ObjectUtils.isEmpty(parentDefaultProfiles)) { synchronized (this.defaultProfiles) { this.defaultProfiles.remove(RESERVED_DEFAULT_PROFILE_NAME); for (String profile : parentDefaultProfiles) { this.defaultProfiles.add(profile); } } } }
@Override public void merge(ConfigurableEnvironment parent) { for (PropertySource<?> ps : parent.getPropertySources()) { if (!this.propertySources.contains(ps.getName())) { this.propertySources.addLast(ps); } } String[] parentActiveProfiles = parent.getActiveProfiles(); if (!ObjectUtils.isEmpty(parentActiveProfiles)) { synchronized (this.activeProfiles) { for (String profile : parentActiveProfiles) { this.activeProfiles.add(profile); } } } String[] parentDefaultProfiles = parent.getDefaultProfiles(); if (!ObjectUtils.isEmpty(parentDefaultProfiles)) { synchronized (this.defaultProfiles) { this.defaultProfiles.remove(RESERVED_DEFAULT_PROFILE_NAME); for (String profile : parentDefaultProfiles) { this.defaultProfiles.add(profile); } } } }
.append(System.lineSeparator()); for (String profileName : applicationContext.getEnvironment().getDefaultProfiles()) { messageBuilder .append(" - ")
private StandardEnvironment copyEnvironment(ConfigurableEnvironment input) { StandardEnvironment environment = new StandardEnvironment(); MutablePropertySources capturedPropertySources = environment.getPropertySources(); // Only copy the default property source(s) and the profiles over from the main // environment (everything else should be pristine, just like it was on startup). for (String name : DEFAULT_PROPERTY_SOURCES) { if (input.getPropertySources().contains(name)) { if (capturedPropertySources.contains(name)) { capturedPropertySources.replace(name, input.getPropertySources().get(name)); } else { capturedPropertySources.addLast(input.getPropertySources().get(name)); } } } environment.setActiveProfiles(input.getActiveProfiles()); environment.setDefaultProfiles(input.getDefaultProfiles()); Map<String, Object> map = new HashMap<String, Object>(); map.put("spring.jmx.enabled", false); map.put("spring.main.sources", ""); capturedPropertySources .addFirst(new MapPropertySource(REFRESH_ARGS_PROPERTY_SOURCE, map)); return environment; }