@Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication springApplication) { Map<String, Object> map = new LinkedHashMap<>(); String uri = environment.getProperty("vcap.services.stats-index.credentials.uri"); if (StringUtils.hasText(uri)) { map.put("initializr.stats.elastic.uri", uri); addOrReplace(environment.getPropertySources(), map); } }
protected void applySpringProfiles(ConfigurableEnvironment environment, ServletContext servletContext) { String systemProfiles = System.getProperty("spring.profiles.active"); environment.setDefaultProfiles(new String[0]); if (environment.containsProperty("spring_profiles")) { String profiles = environment.getProperty("spring_profiles"); servletContext.log("Setting active profiles: " + profiles); environment.setActiveProfiles(StringUtils.tokenizeToStringArray(profiles, ",", true, true)); } else { if (isEmpty(systemProfiles)) { environment.setActiveProfiles("hsqldb"); } else { environment.setActiveProfiles(commaDelimitedListToStringArray(systemProfiles)); } } }
@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); } } } }
private void addProfileToEnvironment(String profile) { for (String activeProfile : this.environment.getActiveProfiles()) { if (activeProfile.equals(profile)) { return; } } this.environment.addActiveProfile(profile); }
@Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { ConfigurableEnvironment environment = event.getEnvironment(); if (environment.getProperty(STANDALONE_MODE_PROPERTY_NAME, boolean.class, false)) { environment.addActiveProfile(STANDALONE_SPRING_PROFILE); } if (logger.isInfoEnabled()) { logger.info("Spring Environment's active profiles : {} in standalone mode : {}", Arrays.asList(environment.getActiveProfiles()), STANDALONE_MODE ); } }
private String getApplicationId(ConfigurableEnvironment environment) { String name = environment.getProperty("spring.application.name"); return StringUtils.hasText(name) ? name : "application"; }
public static void main(String[] args) { List<String> languages = Arrays.asList(new String[]{"groovy","ruby","javascript","python"}); if (args.length != 1) { usage(); } String lang = args[0]; if (!StringUtils.hasText(lang)){ usage(); } lang = lang.toLowerCase(); if (!languages.contains(lang)){ usage(); } /* * Create an application context and set the active profile to configure the * corresponding scripting implementation */ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(); ctx.getEnvironment().setActiveProfiles(lang); ctx.setConfigLocation("/META-INF/spring/integration/cafeDemo.xml"); ctx.refresh(); }
@Override public void postProcessEnvironment(ConfigurableEnvironment env, SpringApplication application) { env.getPropertySources().addFirst(new MapPropertySource("gateway-properties", Collections.singletonMap("spring.webflux.hiddenmethod.filter.enabled", "false"))); } }
public static GenericXmlApplicationContext setupContext() { final GenericXmlApplicationContext context = new GenericXmlApplicationContext(); if (System.getProperty(AVAILABLE_SERVER_SOCKET) == null) { System.out.print("Detect open server socket..."); int availableServerSocket = SocketUtils.findAvailableTcpPort(5678); final Map<String, Object> sockets = new HashMap<>(); sockets.put(AVAILABLE_SERVER_SOCKET, availableServerSocket); final MapPropertySource propertySource = new MapPropertySource("sockets", sockets); context.getEnvironment().getPropertySources().addLast(propertySource); } System.out.println("using port " + context.getEnvironment().getProperty(AVAILABLE_SERVER_SOCKET)); context.load("classpath:META-INF/spring/integration/tcpClientServerDemo-context.xml"); context.registerShutdownHook(); context.refresh(); return context; }
@Override protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) { int availableServerSocket = SocketUtils.findAvailableTcpPort(5678); final Map<String, Object> sockets = new HashMap<String, Object>(); sockets.put("availableServerSocket", availableServerSocket); if (LOGGER.isInfoEnabled()) { LOGGER.info("Available Server Socket: " + availableServerSocket); } final MapPropertySource propertySource = new MapPropertySource("sockets", sockets); context.getEnvironment().getPropertySources().addLast(propertySource); super.loadBeanDefinitions(context, mergedConfig); }
/** * Add, remove or re-order any {@link PropertySource}s in this application's * environment. * @param environment this application's environment * @param args arguments passed to the {@code run} method * @see #configureEnvironment(ConfigurableEnvironment, String[]) */ protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) { MutablePropertySources sources = environment.getPropertySources(); if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) { sources.addLast( new MapPropertySource("defaultProperties", this.defaultProperties)); } if (this.addCommandLineProperties && args.length > 0) { String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME; if (sources.contains(name)) { PropertySource<?> source = sources.get(name); CompositePropertySource composite = new CompositePropertySource(name); composite.addPropertySource(new SimpleCommandLinePropertySource( "springApplicationCommandLineArgs", args)); composite.addPropertySource(source); sources.replace(name, composite); } else { sources.addFirst(new SimpleCommandLinePropertySource(args)); } } }
private List<Class<?>> getInitializerClasses(ConfigurableEnvironment env) { String classNames = env.getProperty(PROPERTY_NAME); List<Class<?>> classes = new ArrayList<>(); if (StringUtils.hasLength(classNames)) { for (String className : StringUtils.tokenizeToStringArray(classNames, ",")) { classes.add(getInitializerClass(className)); } } return classes; }
@Test public void registerServletParamPropertySources_GenericWebApplicationContext() { MockServletContext servletContext = new MockServletContext(); servletContext.addInitParameter("pCommon", "pCommonContextValue"); servletContext.addInitParameter("pContext1", "pContext1Value"); GenericWebApplicationContext ctx = new GenericWebApplicationContext(); ctx.setServletContext(servletContext); ctx.refresh(); ConfigurableEnvironment environment = ctx.getEnvironment(); assertThat(environment, instanceOf(StandardServletEnvironment.class)); MutablePropertySources propertySources = environment.getPropertySources(); assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true)); // ServletContext params are available assertThat(environment.getProperty("pCommon"), is("pCommonContextValue")); assertThat(environment.getProperty("pContext1"), is("pContext1Value")); // Servlet* PropertySources have precedence over System* PropertySources assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)))); // Replace system properties with a mock property source for convenience MockPropertySource mockSystemProperties = new MockPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME); mockSystemProperties.setProperty("pCommon", "pCommonSysPropsValue"); mockSystemProperties.setProperty("pSysProps1", "pSysProps1Value"); propertySources.replace(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties); // assert that servletcontext init params resolve with higher precedence than sysprops assertThat(environment.getProperty("pCommon"), is("pCommonContextValue")); assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value")); }
environment.getPropertySources().get(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME); if (ps == null) { ps = new MapPropertySource(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME, new LinkedHashMap<>()); environment.getPropertySources().addFirst(ps); ps.getSource().putAll(convertInlinedPropertiesToMap(inlinedProperties));
@SuppressWarnings("unchecked") private void setPortProperty(ConfigurableEnvironment environment, String propertyName, int port) { MutablePropertySources sources = environment.getPropertySources(); PropertySource<?> source = sources.get("server.ports"); if (source == null) { source = new MapPropertySource("server.ports", new HashMap<>()); sources.addFirst(source); } ((Map<String, Object>) source.getSource()).put(propertyName, port); }
@Test public void merge() { ConfigurableEnvironment child = new StandardEnvironment(); child.setActiveProfiles("c1", "c2"); child.getPropertySources().addLast( new MockPropertySource("childMock") .withProperty("childKey", "childVal") parent.setActiveProfiles("p1", "p2"); parent.getPropertySources().addLast( new MockPropertySource("parentMock") .withProperty("parentKey", "parentVal") .withProperty("bothKey", "parentBothVal")); assertThat(child.getProperty("childKey"), is("childVal")); assertThat(child.getProperty("parentKey"), nullValue()); assertThat(child.getProperty("bothKey"), is("childBothVal")); assertThat(parent.getProperty("childKey"), nullValue()); assertThat(parent.getProperty("parentKey"), is("parentVal")); assertThat(parent.getProperty("bothKey"), is("parentBothVal")); assertThat(child.getActiveProfiles(), equalTo(new String[]{"c1","c2"})); assertThat(parent.getActiveProfiles(), equalTo(new String[]{"p1","p2"})); child.merge(parent); assertThat(child.getProperty("childKey"), is("childVal")); assertThat(child.getProperty("parentKey"), is("parentVal")); assertThat(child.getProperty("bothKey"), is("childBothVal"));
@Override public void initialize(ConfigurableApplicationContext applicationContext) { ConfigurableEnvironment environment = applicationContext.getEnvironment(); environment.getPropertySources().addFirst(new PropertySource<Object>("testPropertySource") { @Override public Object getProperty(String key) { return "name".equals(key) ? "testName" : null; } }); } }
@Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { ConfigurableEnvironment environment = event.getEnvironment(); if (!environment.resolvePlaceholders("${spring.cloud.config.enabled:false}") .equalsIgnoreCase("true")) { if (!environment.getPropertySources().contains(this.propertySource.getName())) { environment.getPropertySources().addLast(this.propertySource); } } }
@Test public void addActiveProfile_whenActiveProfilesPropertyIsAlreadySet() { ConfigurableEnvironment env = new StandardEnvironment(); assertThat(env.getProperty(ACTIVE_PROFILES_PROPERTY_NAME), nullValue()); env.getPropertySources().addFirst(new MockPropertySource().withProperty(ACTIVE_PROFILES_PROPERTY_NAME, "p1")); assertThat(env.getProperty(ACTIVE_PROFILES_PROPERTY_NAME), equalTo("p1")); env.addActiveProfile("p2"); assertThat(env.getActiveProfiles(), arrayContaining("p1", "p2")); }
private void addPropertySource(PropertySource<?> propertySource) { String name = propertySource.getName(); MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources(); PropertySource<?> existing = propertySources.get(name); if (existing != null) { PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ? composite.addPropertySource(newSource); composite.addPropertySource(existing); propertySources.replace(name, composite); propertySources.addLast(propertySource);