private List<Resource> getResource(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext, String locations) { List<Resource> resources = new LinkedList<>(); String[] configFileLocations = locations == null ? DEFAULT_PROFILE_CONFIG_FILE_LOCATIONS : StringUtils .commaDelimitedListToStringArray(locations); for (String location : configFileLocations) { location = applicationContext.getEnvironment().resolvePlaceholders(location); servletContext.log("Testing for YAML resources at: " + location); Resource resource = applicationContext.getResource(location); if (resource != null && resource.exists()) { resources.add(resource); } } return resources; }
@Override public void initialize(ConfigurableWebApplicationContext applicationContext) { // test that ApplicationContextInitializers can access ServletContext properties // via the environment (SPR-8991) String value = applicationContext.getEnvironment().getRequiredProperty("someProperty"); assertThat(value, is("someValue")); } }
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information String idParam = sc.getInitParameter(CONTEXT_ID_PARAM); if (idParam != null) { wac.setId(idParam); } else { // Generate default id... wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(sc.getContextPath())); } } wac.setServletContext(sc); String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM); if (configLocationParam != null) { wac.setConfigLocation(configLocationParam); } // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(sc, null); } customizeContext(sc, wac); wac.refresh(); }
@Test @SuppressWarnings("deprecation") public void environmentMerge() { assertThat(this.root.getEnvironment().acceptsProfiles("rootProfile1"), is(true)); assertThat(this.root.getEnvironment().acceptsProfiles("wacProfile1"), is(false)); assertThat(this.applicationContext.getEnvironment().acceptsProfiles("rootProfile1"), is(true)); assertThat(this.applicationContext.getEnvironment().acceptsProfiles("wacProfile1"), is(true)); }
servletContext.addListener(publisher); WebApplicationContextUtils.initServletPropertySources(applicationContext.getEnvironment().getPropertySources(), servletContext, applicationContext.getServletConfig()); map.put(rawYamlKey, yamlStr); NestedMapPropertySource properties = new NestedMapPropertySource("servletConfigYaml", map); applicationContext.getEnvironment().getPropertySources().addLast(properties); applySpringProfiles(applicationContext.getEnvironment(), servletContext); applyLog4jConfiguration(applicationContext.getEnvironment(), servletContext);
@Before public void setup() { Mockito.when(servletConfig.getInitParameterNames()).thenReturn(new EmptyEnumerationOfString()); Mockito.when(servletContext.getInitParameterNames()).thenReturn(new EmptyEnumerationOfString()); Mockito.when(context.getServletConfig()).thenReturn(servletConfig); Mockito.when(context.getServletContext()).thenReturn(servletContext); Mockito.when(context.getEnvironment()).thenReturn(environment); Mockito.doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { System.err.println(invocation.getArguments()[0]); return null; } }).when(servletContext).log(ArgumentMatchers.anyString()); Mockito.when(servletContext.getContextPath()).thenReturn("/context"); }
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information String idParam = sc.getInitParameter(CONTEXT_ID_PARAM); if (idParam != null) { wac.setId(idParam); } else { // Generate default id... wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(sc.getContextPath())); } } wac.setServletContext(sc); String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM); if (configLocationParam != null) { wac.setConfigLocation(configLocationParam); } // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(sc, null); } customizeContext(sc, wac); wac.refresh(); }
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information if (this.contextId != null) { wac.setId(this.contextId); } else { // Generate default id... wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(getServletContext().getContextPath()) + '/' + getServletName()); } } wac.setServletContext(getServletContext()); wac.setServletConfig(getServletConfig()); wac.setNamespace(getNamespace()); wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener())); // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig()); } postProcessWebApplicationContext(wac); applyInitializers(wac); wac.refresh(); }
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information if (this.contextId != null) { wac.setId(this.contextId); } else { // Generate default id... wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(getServletContext().getContextPath()) + '/' + getServletName()); } } wac.setServletContext(getServletContext()); wac.setServletConfig(getServletConfig()); wac.setNamespace(getNamespace()); wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener())); // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig()); } postProcessWebApplicationContext(wac); applyInitializers(wac); wac.refresh(); }
public class AppContextInitializer implements ApplicationContextInitializer<ConfigurableWebApplicationContext> { @Override public void initialize(ConfigurableWebApplicationContext applicationContext) { Properties props; try { props = PropertiesLoaderUtils.loadAllProperties("/some/path"); PropertiesPropertySource ps = new PropertiesPropertySource("profile", props); applicationContext.getEnvironment().getPropertySources().addFirst(ps); } catch (IOException e) { // handle error } } }
InitAndIB.constructed = false; root = new XmlWebApplicationContext(); root.getEnvironment().addActiveProfile("rootProfile1"); MockServletContext sc = new MockServletContext(""); root.setServletContext(sc);
Timer.start("SPRING_INITIALIZER_ONSTARTUP"); if (springProfiles != null) { applicationContext.getEnvironment().setActiveProfiles(springProfiles.toArray(new String[0]));
public class CustomInitializer implements ApplicationContextInitializer<ConfigurableWebApplicationContext> { public void initialize(ConfigurableWebApplicationContext ctx) { ctx.getEnvironment().getPropertySources().addLast(new FooPropertySource()); } }
@Override public ConfigurableEnvironment getEnvironment() { return this.appContext.getEnvironment(); }
public class MyInitializer implements ApplicationContextInitializer<ConfigurableWebApplicationContext> { public void initialize(ConfigurableWebApplicationContext ctx) { PropertySource ps = new ResourcePropertySource(new ClassPathResource("sample.properties")); // handle exception ctx.getEnvironment().getPropertySources().addFirst(ps); } }
public class RuntimeEnvironmentInitializer implements ApplicationContextInitializer<ConfigurableWebApplicationContext> { private ConfigurableWebApplicationContext configurableApplicationContext; @Override public void initialize(ConfigurableWebApplicationContext arg0) { this.configurableApplicationContext = arg0; String valueString = arg0.getServletContext().getInitParameter("InitParam"); configurableApplicationContext.getEnvironment().setActiveProfiles("prod"); } }
/** * Activate the given profiles before the application context is "refreshed". */ public ContextMockMvcBuilder activateProfiles(String...profiles) { this.webAppContext.getEnvironment().setActiveProfiles(profiles); return this; }
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information String idParam = sc.getInitParameter(CONTEXT_ID_PARAM); if (idParam != null) { wac.setId(idParam); } else { // Generate default id... wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(sc.getContextPath())); } } wac.setServletContext(sc); String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM); if (configLocationParam != null) { wac.setConfigLocation(configLocationParam); } // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(sc, null); } customizeContext(sc, wac); wac.refresh(); }
@Bean(name=HTTP_TRANSPORT_CHILD_CONTEXT_BEAN_NAME) public SpringContextWrapper transportWebMvcContext(ConfigurableWebApplicationContext parentContext, ServletContextHandler servletContextHandler){ AnnotationConfigWebApplicationContext transportWebMvcContext = new AnnotationConfigWebApplicationContext(); transportWebMvcContext.setDisplayName("httpTransport-webMvcContext"); transportWebMvcContext.setServletContext(servletContextHandler.getServletContext()); transportWebMvcContext.setParent(parentContext); transportWebMvcContext.setEnvironment(parentContext.getEnvironment()); transportWebMvcContext.register(SpringMvcConfiguration.class); transportWebMvcContext.register(PropertySourcesPlaceholderConfigurer.class); transportWebMvcContext.refresh(); return new SpringContextWrapper(transportWebMvcContext); }
@Override protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext wac) { Configuration.setApplicationDirectory(dataDirectory); // Make dataDir available as Servlet Context attribute servletContext.setAttribute(APPLICATION_DIRECTORY, dataDirectory); // Set up @Autowired support ApplicationContextUtils.addAutowiredSupport(wac); final Configuration configuration = new Configuration(properties); if(configuration.getBoolean("caching.enabled", true)){ wac.getEnvironment().setActiveProfiles("useCaching"); } Aksess.setContextPath(servletContext.getContextPath()); // Set and load configuration on these classes since they are not DI-based (hackish..) Aksess.setConfiguration(configuration); Aksess.loadConfiguration(); // Add ${appDir} property for the Spring Context ApplicationContextUtils.addAppDirPropertySupport(wac); dbConnectionFactory.setServletContext(servletContext); dbConnectionFactory.setConfiguration(configuration); dbConnectionFactory.loadConfiguration(); // Add the Configuration and the ConfigurationLoader as Spring beans addConfigurationAndLoaderAsSingletonsInContext(wac, configuration, configurationLoader); // Replace ${} properties in Spring with config properties addConfigurationPropertyReplacer(wac, properties); RootContext.setInstance(wac); }