Java Code Examples for org.springframework.context.ConfigurableApplicationContext#setEnvironment()
The following examples show how to use
org.springframework.context.ConfigurableApplicationContext#setEnvironment() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EnvironmentSystemIntegrationTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void fileSystemXmlApplicationContext() throws IOException { ClassPathResource xml = new ClassPathResource(XML_PATH); File tmpFile = File.createTempFile("test", "xml"); FileCopyUtils.copy(xml.getFile(), tmpFile); // strange - FSXAC strips leading '/' unless prefixed with 'file:' ConfigurableApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] {"file:" + tmpFile.getPath()}, false); ctx.setEnvironment(prodEnv); ctx.refresh(); assertEnvironmentBeanRegistered(ctx); assertHasEnvironment(ctx, prodEnv); assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment()); assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); }
Example 2
Source File: EnvironmentSystemIntegrationTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void fileSystemXmlApplicationContext() throws IOException { ClassPathResource xml = new ClassPathResource(XML_PATH); File tmpFile = File.createTempFile("test", "xml"); FileCopyUtils.copy(xml.getFile(), tmpFile); // strange - FSXAC strips leading '/' unless prefixed with 'file:' ConfigurableApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] {"file:" + tmpFile.getPath()}, false); ctx.setEnvironment(prodEnv); ctx.refresh(); assertEnvironmentBeanRegistered(ctx); assertHasEnvironment(ctx, prodEnv); assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment()); assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); }
Example 3
Source File: SpringApplication.java From spring-javaformat with Apache License 2.0 | 6 votes |
private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) { context.setEnvironment(environment); postProcessApplicationContext(context); applyInitializers(context); listeners.contextPrepared(context); if (this.logStartupInfo) { logStartupInfo(context.getParent() == null); logStartupProfileInfo(context); } // Add boot specific singleton beans context.getBeanFactory().registerSingleton("springApplicationArguments", applicationArguments); if (printedBanner != null) { context.getBeanFactory().registerSingleton("springBootBanner", printedBanner); } // Load the sources Set<Object> sources = getAllSources(); Assert.notEmpty(sources, "Sources must not be empty"); load(context, sources.toArray(new Object[0])); listeners.contextLoaded(context); }
Example 4
Source File: EnvironmentSystemIntegrationTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void fileSystemXmlApplicationContext() throws IOException { ClassPathResource xml = new ClassPathResource(XML_PATH); File tmpFile = File.createTempFile("test", "xml"); FileCopyUtils.copy(xml.getFile(), tmpFile); // strange - FSXAC strips leading '/' unless prefixed with 'file:' ConfigurableApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] {"file:" + tmpFile.getPath()}, false); ctx.setEnvironment(prodEnv); ctx.refresh(); assertEnvironmentBeanRegistered(ctx); assertHasEnvironment(ctx, prodEnv); assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment()); assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); }
Example 5
Source File: EnvironmentSystemIntegrationTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void classPathXmlApplicationContext() { ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(XML_PATH); ctx.setEnvironment(prodEnv); ctx.refresh(); assertEnvironmentBeanRegistered(ctx); assertHasEnvironment(ctx, prodEnv); assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment()); assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); }
Example 6
Source File: EnvironmentSystemIntegrationTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void classPathXmlApplicationContext() { ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(XML_PATH); ctx.setEnvironment(prodEnv); ctx.refresh(); assertEnvironmentBeanRegistered(ctx); assertHasEnvironment(ctx, prodEnv); assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment()); assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); }
Example 7
Source File: NetstrapBootApplication.java From netstrap with Apache License 2.0 | 5 votes |
/** * 装配SpringContext * 设置环境,初始化调用,设置监听器 */ private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment) { context.setEnvironment(environment); applyInitializer(context); for (ApplicationListener listener : listeners) { context.addApplicationListener(listener); } }
Example 8
Source File: EnvironmentSystemIntegrationTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void classPathXmlApplicationContext() { ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(XML_PATH); ctx.setEnvironment(prodEnv); ctx.refresh(); assertEnvironmentBeanRegistered(ctx); assertHasEnvironment(ctx, prodEnv); assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment()); assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); }
Example 9
Source File: IndexControllerConfigActionWithEnvironmentVariablesTest.java From SMSC with Apache License 2.0 | 5 votes |
@Override public void initialize(ConfigurableApplicationContext applicationContext) { MockEnvironment environment = new MockEnvironment(); environment.setProperty("admin.api.url", "/admin"); environment.setProperty("admin.i18n.path", "/admin/i18n"); environment.setProperty("admin.debug", "true"); applicationContext.setEnvironment(environment); }
Example 10
Source File: MockExtendedBinderConfiguration.java From spring-cloud-stream with Apache License 2.0 | 5 votes |
@SuppressWarnings("rawtypes") @Bean public Binder<?, ?, ?> extendedPropertiesBinder() { Binder mock = Mockito.mock(Binder.class, Mockito.withSettings().defaultAnswer(Mockito.RETURNS_MOCKS) .extraInterfaces(ExtendedPropertiesBinder.class)); ConfigurableEnvironment environment = new StandardEnvironment(); Map<String, Object> propertiesToAdd = new HashMap<>(); propertiesToAdd.put("spring.cloud.stream.foo.default.consumer.extendedProperty", "someFancyExtension"); propertiesToAdd.put("spring.cloud.stream.foo.default.producer.extendedProperty", "someFancyExtension"); environment.getPropertySources() .addLast(new MapPropertySource("extPropertiesConfig", propertiesToAdd)); ConfigurableApplicationContext applicationContext = new GenericApplicationContext(); applicationContext.setEnvironment(environment); FooExtendedBindingProperties fooExtendedBindingProperties = new FooExtendedBindingProperties(); fooExtendedBindingProperties.setApplicationContext(applicationContext); final FooConsumerProperties fooConsumerProperties = fooExtendedBindingProperties .getExtendedConsumerProperties("input"); final FooProducerProperties fooProducerProperties = fooExtendedBindingProperties .getExtendedProducerProperties("output"); when(((ExtendedPropertiesBinder) mock).getExtendedConsumerProperties("input")) .thenReturn(fooConsumerProperties); when(((ExtendedPropertiesBinder) mock).getExtendedProducerProperties("output")) .thenReturn(fooProducerProperties); return mock; }
Example 11
Source File: PropertyMaskingContextInitializer.java From spring-cloud-services-connector with Apache License 2.0 | 5 votes |
@Override public void initialize(ConfigurableApplicationContext applicationContext) { ConfigurableEnvironment environment = applicationContext.getEnvironment(); MutablePropertySources propertySources = environment.getPropertySources(); String[] defaultKeys = {"password", "secret", "key", "token", ".*credentials.*", "vcap_services"}; Set<String> propertiesToSanitize = Stream.of(defaultKeys) .collect(Collectors.toSet()); PropertySource<?> bootstrapProperties = propertySources.get(BOOTSTRAP_PROPERTY_SOURCE_NAME); Set<PropertySource<?>> bootstrapNestedPropertySources = new HashSet<>(); Set<PropertySource<?>> configServiceNestedPropertySources = new HashSet<>(); if (bootstrapProperties != null && bootstrapProperties instanceof CompositePropertySource) { bootstrapNestedPropertySources.addAll(((CompositePropertySource) bootstrapProperties).getPropertySources()); } for (PropertySource<?> nestedProperty : bootstrapNestedPropertySources) { if (nestedProperty.getName().equals(CONFIG_SERVICE_PROPERTY_SOURCE_NAME)) { configServiceNestedPropertySources.addAll(((CompositePropertySource) nestedProperty).getPropertySources()); } } Stream<String> vaultKeyNameStream = configServiceNestedPropertySources.stream() .filter(ps -> ps instanceof EnumerablePropertySource) .filter(ps -> ps.getName().startsWith(VAULT_PROPERTY_PATTERN) || ps.getName().startsWith(CREDHUB_PROPERTY_PATTERN)) .map(ps -> ((EnumerablePropertySource) ps).getPropertyNames()) .flatMap(Arrays::<String>stream); propertiesToSanitize.addAll(vaultKeyNameStream.collect(Collectors.toSet())); PropertiesPropertySource envKeysToSanitize = new PropertiesPropertySource( SANITIZE_ENV_KEY, mergeClientProperties(propertySources, propertiesToSanitize)); environment.getPropertySources().addFirst(envKeysToSanitize); applicationContext.setEnvironment(environment); }
Example 12
Source File: PropertyMaskingContextInitializer.java From spring-cloud-services-starters with Apache License 2.0 | 5 votes |
@Override public void initialize(ConfigurableApplicationContext applicationContext) { ConfigurableEnvironment environment = applicationContext.getEnvironment(); MutablePropertySources propertySources = environment.getPropertySources(); String[] defaultKeys = { "password", "secret", "key", "token", ".*credentials.*", "vcap_services" }; Set<String> propertiesToSanitize = Stream.of(defaultKeys).collect(Collectors.toSet()); PropertySource<?> bootstrapProperties = propertySources.get(BOOTSTRAP_PROPERTY_SOURCE_NAME); Set<PropertySource<?>> bootstrapNestedPropertySources = new HashSet<>(); Set<PropertySource<?>> configServiceNestedPropertySources = new HashSet<>(); if (bootstrapProperties != null && bootstrapProperties instanceof CompositePropertySource) { bootstrapNestedPropertySources.addAll(((CompositePropertySource) bootstrapProperties).getPropertySources()); } for (PropertySource<?> nestedProperty : bootstrapNestedPropertySources) { if (nestedProperty.getName().equals(CONFIG_SERVICE_PROPERTY_SOURCE_NAME)) { configServiceNestedPropertySources .addAll(((CompositePropertySource) nestedProperty).getPropertySources()); } } Stream<String> vaultKeyNameStream = configServiceNestedPropertySources.stream() .filter(ps -> ps instanceof EnumerablePropertySource) .filter(ps -> ps.getName().startsWith(VAULT_PROPERTY_PATTERN) || ps.getName().startsWith(CREDHUB_PROPERTY_PATTERN)) .map(ps -> ((EnumerablePropertySource) ps).getPropertyNames()).flatMap(Arrays::<String>stream); propertiesToSanitize.addAll(vaultKeyNameStream.collect(Collectors.toSet())); PropertiesPropertySource envKeysToSanitize = new PropertiesPropertySource(SANITIZE_ENV_KEY, mergeClientProperties(propertySources, propertiesToSanitize)); environment.getPropertySources().addFirst(envKeysToSanitize); applicationContext.setEnvironment(environment); }