@Override public void initialize(GenericApplicationContext applicationContext) { applicationContext.getEnvironment().setActiveProfiles(PROFILE_TWO); } }
@Override public void initialize(GenericApplicationContext applicationContext) { applicationContext.getEnvironment().setActiveProfiles("dev"); } }
@Override public void initialize(GenericApplicationContext applicationContext) { applicationContext.getEnvironment().setActiveProfiles(PROFILE_ONE); }
@Test @SuppressWarnings("all") public void braceAccess() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "#{environment['my.name']}") .getBeanDefinition()); GenericApplicationContext ctx = new GenericApplicationContext(bf); ctx.getEnvironment().getPropertySources().addFirst(new MockPropertySource().withProperty("my.name", "myBean")); ctx.refresh(); assertThat(ctx.getBean(TestBean.class).getName(), equalTo("myBean")); }
@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); }
@Test public void classPathBeanDefinitionScanner_inheritsEnvironmentFromEnvironmentCapableBDR_scanProfileAnnotatedComponents() { GenericApplicationContext ctx = new GenericApplicationContext(); ctx.setEnvironment(prodEnv); ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(ctx); scanner.scan("org.springframework.core.env.scan2"); ctx.refresh(); assertThat(scanner.getEnvironment(), is((Environment)ctx.getEnvironment())); assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); }
@Test public void environmentAccess() throws Exception { Map<String, Object> map = new HashMap<>(); map.put("key.foo", "value.foo"); GenericApplicationContext wac = (GenericApplicationContext) context.getRequest().getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE); wac.getEnvironment().getPropertySources().addFirst(new MapPropertySource("mapSource", map)); wac.getDefaultListableBeanFactory().registerSingleton("bean2", context.getRequest().getAttribute("bean")); tag.setExpression("@environment['key.foo']"); int action = tag.doStartTag(); assertEquals(Tag.EVAL_BODY_INCLUDE, action); action = tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, action); assertEquals("value.foo", ((MockHttpServletResponse) context.getResponse()).getContentAsString()); }
public class MyApplicationContextInitializer implements ApplicationContextInitializer<GenericApplicationContext> { public void initialize(GenericApplicationContext context) { context.getEnvironment().getSystemProperties().put(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "some_profile"); }
public class PropertySourceContextLoader extends GenericXmlContextLoader { @Override protected void loadBeanDefinitions(GenericApplicationContext context,MergedContextConfiguration mergedConfig) { PropertySource<String> ps = new MyPropertySource(); context.getEnvironment().getPropertySources().addLast(ps); super.loadBeanDefinitions(context, mergedConfig); } } @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(loader = PropertySourceContextLoader.class, locations = { "classpath:/spring-application-context.xml" }) public class SpringBasedTest { .......... }
private void registerPropertySource(Properties properties) { int propertiesIndex = this.propertiesCounter.getAndIncrement(); String propertySourceName = "properties-" + propertiesIndex; PropertySource<?> propertySource = new PropertiesPropertySource(propertySourceName, properties); this.context.getEnvironment().getPropertySources().addLast(propertySource); }
@Override public void initialize(GenericApplicationContext applicationContext) { if (enabled && applicationContext.getEnvironment() .getProperty("spring.functional.enabled", Boolean.class, false)) { ContextFunctionCatalogBeanRegistrar registrar = new ContextFunctionCatalogBeanRegistrar( applicationContext); applicationContext.addBeanFactoryPostProcessor(registrar); } }
@Override public void initialize(GenericApplicationContext applicationContext) { if (enabled && applicationContext.getEnvironment() .getProperty("spring.functional.enabled", Boolean.class, false)) { ContextFunctionCatalogBeanRegistrar registrar = new ContextFunctionCatalogBeanRegistrar( applicationContext); applicationContext.addBeanFactoryPostProcessor(registrar); } }
@Override protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) { String[] activeProfiles = context.getEnvironment().getActiveProfiles(); log.info("Active profiles: " + StringUtils.arrayToCommaDelimitedString(activeProfiles)); // let parent do its magic super.loadBeanDefinitions(context, mergedConfig); if (!ObjectUtils.containsElement(activeProfiles, YarnTestSystemConstants.PROFILE_ID_NOMINICLUSTER)) { YarnClusterInjectUtils.handleClusterInject(context, mergedConfig); } }
@Override protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) { String[] activeProfiles = context.getEnvironment().getActiveProfiles(); log.info("Active profiles: " + StringUtils.arrayToCommaDelimitedString(activeProfiles)); // let parent do its magic super.loadBeanDefinitions(context, mergedConfig); if (!ObjectUtils.containsElement(activeProfiles, YarnTestSystemConstants.PROFILE_ID_NOMINICLUSTER)) { YarnClusterInjectUtils.handleClusterInject(context, mergedConfig); } }
@Override protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) { String[] activeProfiles = context.getEnvironment().getActiveProfiles(); log.info("Active profiles: " + StringUtils.arrayToCommaDelimitedString(activeProfiles)); // let parent do its magic super.loadBeanDefinitions(context, mergedConfig); if (!ObjectUtils.containsElement(activeProfiles, YarnTestSystemConstants.PROFILE_ID_NOMINICLUSTER)) { YarnClusterInjectUtils.handleClusterInject(context, mergedConfig); } }
@Override protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) { String[] activeProfiles = context.getEnvironment().getActiveProfiles(); log.info("Active profiles: " + StringUtils.arrayToCommaDelimitedString(activeProfiles)); // let parent do its magic super.loadBeanDefinitions(context, mergedConfig); if (!ObjectUtils.containsElement(activeProfiles, YarnTestSystemConstants.PROFILE_ID_NOMINICLUSTER)) { YarnClusterInjectUtils.handleClusterInject(context, mergedConfig); } }
@Override public void initialize(GenericApplicationContext context) { MustacheAutoConfiguration configuration = new MustacheAutoConfiguration(this.properties, context.getEnvironment(), context); context.registerBean(MustacheResourceTemplateLoader.class, () -> configuration.mustacheTemplateLoader()); context.registerBean(Mustache.Compiler.class, () -> configuration.mustacheCompiler(context.getBean(Mustache.TemplateLoader.class))); } }
public AutoConfigurations(GenericApplicationContext applicationContext) { this.context = applicationContext; setBeanFactory(applicationContext.getDefaultListableBeanFactory()); setBeanClassLoader(applicationContext.getClassLoader()); setEnvironment(applicationContext.getEnvironment()); setResourceLoader(applicationContext); }
@Before public void init() throws Exception { when(configurableEnvironment.getActiveProfilesContainer()).thenCallRealMethod(); when(configurableEnvironment.getActiveProfiles()).thenCallRealMethod(); doCallRealMethod().when(configurableEnvironment).setActiveProfilesContainer(any()); doCallRealMethod().when(configurableEnvironment).addActiveProfile(any(String.class)); configurableEnvironment.setActiveProfilesContainer(new HashSet<>()); when(applicationContext.getEnvironment()).thenReturn(configurableEnvironment); when(cloud.getServiceInfos()).thenReturn( Collections.singletonList( new MysqlServiceInfo("mysql", "localhost"))); doCallRealMethod().when(contextInitializer).initialize(any(GenericApplicationContext.class)); }
@Override public void initialize(GenericApplicationContext context) { context.registerBean(MongoClientSettingsBuilderCustomizer.class, () -> new MongoReactiveAutoConfiguration.NettyDriverConfiguration().nettyDriverCustomizer(context.getDefaultListableBeanFactory().getBeanProvider(MongoClientSettings.class))); context.registerBean(MongoClient.class, () -> new MongoReactiveAutoConfiguration(context.getBeanProvider(MongoClientSettings.class)).reactiveStreamsMongoClient(this.properties, context.getEnvironment(), context.getBeanProvider(MongoClientSettingsBuilderCustomizer.class)), (definition) -> { if (embeddedServer) { definition.setDependsOn("embeddedMongoServer"); } }); } }