Java Code Examples for org.springframework.web.context.ConfigurableWebApplicationContext

Following code examples demonstrate how to use org.springframework.web.context.ConfigurableWebApplicationContextfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.web.context.ConfigurableWebApplicationContextand various code implementation of this class.

        if (configurableApplicationContext instanceof ConfigurableWebApplicationContext) {
            wac = (ConfigurableWebApplicationContext) configurableApplicationContext;
            servletName = wac.getServletConfig().getServletName();
            logger.debug("servletName=" + servletName);
            logger.debug(servletName + ":current activeProfiles=" + Arrays.toString(env.getActiveProfiles()));
            logger.debug(servletName + ":current spring.profiles.active=" + env.getProperty("spring.profiles.active"));
            String activeProfilesParm = wac.getServletConfig().getInitParameter("spring.profiles.active");
            logger.debug(servletName + ":init-parm spring.profiles.active=" + activeProfilesParm);

            String[] activeProfilesFromInit = StringUtils.tokenizeToStringArray(activeProfilesParm, ",");
            env.setActiveProfiles(activeProfilesFromInit);
            logger.info(servletName + ":setting new activeProfiles=" + Arrays.toString(env.getActiveProfiles()));

            Map<String, Object> additionalServletProperties = new HashMap<String, Object>();
            additionalServletProperties.put(SERVLET_NAME_KEY, servletName);
            additionalServletProperties.put(APPLICATION_CONTEXT_PATH_KEY, wac.getServletConfig().getServletContext().getContextPath());

            MapPropertySource additionalServletPropertySource = new MapPropertySource(ADDITIONAL_SERVLET_PROPERTY_SOURCE_NAME, additionalServletProperties);
            MutablePropertySources mutablePropertySources = env.getPropertySources();
            if (mutablePropertySources.contains(ADDITIONAL_SERVLET_PROPERTY_SOURCE_NAME)) {
                mutablePropertySources.replace(ADDITIONAL_SERVLET_PROPERTY_SOURCE_NAME, additionalServletPropertySource);
            } else {
                mutablePropertySources.addLast(additionalServletPropertySource);
            }

        } else {
            logger.warn("configurableApplicationContext (" + configurableApplicationContext + ") not instance of org.springframework.web.context.ConfigurableWebApplicationContext:cannot be initialized");
        } 


    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {

        String servletName = "unknown";
        ConfigurableEnvironment env = configurableApplicationContext.getEnvironment();
        ConfigurableWebApplicationContext wac = null;
        if (configurableApplicationContext instanceof ConfigurableWebApplicationContext) {
            wac = (ConfigurableWebApplicationContext) configurableApplicationContext;
            servletName = wac.getServletConfig().getServletName();
            logger.debug("servletName=" + servletName);
            logger.debug(servletName + ":current activeProfiles=" + Arrays.toString(env.getActiveProfiles()));
            logger.debug(servletName + ":current spring.profiles.active=" + env.getProperty("spring.profiles.active"));
            String activeProfilesParm = wac.getServletConfig().getInitParameter("spring.profiles.active");
            logger.debug(servletName + ":init-parm spring.profiles.active=" + activeProfilesParm);

            String[] activeProfilesFromInit = StringUtils.tokenizeToStringArray(activeProfilesParm, ",");
            env.setActiveProfiles(activeProfilesFromInit);
            logger.info(servletName + ":setting new activeProfiles=" + Arrays.toString(env.getActiveProfiles()));

            Map<String, Object> additionalServletProperties = new HashMap<String, Object>();
            additionalServletProperties.put(SERVLET_NAME_KEY, servletName);
            additionalServletProperties.put(APPLICATION_CONTEXT_PATH_KEY, wac.getServletConfig().getServletContext().getContextPath());

            MapPropertySource additionalServletPropertySource = new MapPropertySource(ADDITIONAL_SERVLET_PROPERTY_SOURCE_NAME, additionalServletProperties);
            MutablePropertySources mutablePropertySources = env.getPropertySources();
            if (mutablePropertySources.contains(ADDITIONAL_SERVLET_PROPERTY_SOURCE_NAME)) {
                mutablePropertySources.replace(ADDITIONAL_SERVLET_PROPERTY_SOURCE_NAME, additionalServletPropertySource);
            } else {
                mutablePropertySources.addLast(additionalServletPropertySource);
            }

        } else {
            logger.warn("configurableApplicationContext (" + configurableApplicationContext + ") not instance of org.springframework.web.context.ConfigurableWebApplicationContext:cannot be initialized");
        }
    } 

	protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext) {	
		super.customizeContext(servletContext, applicationContext);	
		XmlWebApplicationContext xmlWebApplicationContext = (XmlWebApplicationContext)applicationContext;
		boolean allowBeanDefinitionOverriding  = false;
		boolean allowCircularReferences = false;
		if("true".equals(servletContext.getInitParameter("allowBeanDefinitionOverriding")))
		{
			allowBeanDefinitionOverriding = true;			
		}
		if("true".equals(servletContext.getInitParameter("allowCircularReferences")))
		{
			allowCircularReferences = true;		
		}	
	} 

Advertisement
Javadoc
Interface to be implemented by configurable web application contexts. Supported by ContextLoader and

org.springframework.web.servlet.FrameworkServlet.

Note: The setters of this interface need to be called before an invocation of the #refresh method inherited from org.springframework.context.ConfigurableApplicationContext. They do not cause an initialization of the context on their own. @author Juergen Hoeller @since 05.12.2003 @see #refresh @see ContextLoader#createWebApplicationContext @see org.springframework.web.servlet.FrameworkServlet#createWebApplicationContext

Read More
Advertisement