Example usage for org.springframework.beans.factory.xml XmlBeanDefinitionReader setEnvironment

List of usage examples for org.springframework.beans.factory.xml XmlBeanDefinitionReader setEnvironment

Introduction

In this page you can find the example usage for org.springframework.beans.factory.xml XmlBeanDefinitionReader setEnvironment.

Prototype

public void setEnvironment(Environment environment) 

Source Link

Document

Set the Environment to use when reading bean definitions.

Usage

From source file:org.cloudfoundry.identity.uaa.BootstrapTests.java

private ConfigurableApplicationContext getServletContext(String... resources) {
    String environmentConfigLocations = "required_configuration.yml,${LOGIN_CONFIG_URL},file:${LOGIN_CONFIG_PATH}/login.yml,file:${CLOUD_FOUNDRY_CONFIG_PATH}/login.yml,${UAA_CONFIG_URL},file:${UAA_CONFIG_FILE},file:${UAA_CONFIG_PATH}/uaa.yml,file:${CLOUD_FOUNDRY_CONFIG_PATH}/uaa.yml";
    String profiles = null;//  www  . j  a  v a2 s  .c  o  m
    String[] resourcesToLoad = resources;
    if (!resources[0].endsWith(".xml")) {
        profiles = resources[0];
        resourcesToLoad = new String[resources.length - 1];
        System.arraycopy(resources, 1, resourcesToLoad, 0, resourcesToLoad.length);
    }

    final String[] configLocations = resourcesToLoad;

    AbstractRefreshableWebApplicationContext context = new AbstractRefreshableWebApplicationContext() {

        @Override
        protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory)
                throws BeansException, IOException {
            XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

            // Configure the bean definition reader with this context's
            // resource loading environment.
            beanDefinitionReader.setEnvironment(this.getEnvironment());
            beanDefinitionReader.setResourceLoader(this);
            beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

            if (configLocations != null) {
                for (String configLocation : configLocations) {
                    beanDefinitionReader.loadBeanDefinitions(configLocation);
                }
            }
        }

    };
    MockServletContext servletContext = new MockServletContext() {
        @Override
        public RequestDispatcher getNamedDispatcher(String path) {
            return new MockRequestDispatcher("/");
        }

        public String getVirtualServerName() {
            return null;
        }
    };
    context.setServletContext(servletContext);
    MockServletConfig servletConfig = new MockServletConfig(servletContext);
    servletConfig.addInitParameter("environmentConfigLocations", environmentConfigLocations);
    context.setServletConfig(servletConfig);

    YamlServletProfileInitializer initializer = new YamlServletProfileInitializer();
    initializer.initialize(context);

    if (profiles != null) {
        context.getEnvironment().setActiveProfiles(StringUtils.commaDelimitedListToStringArray(profiles));
    }

    context.refresh();

    return context;
}

From source file:org.cloudfoundry.identity.uaa.login.BootstrapTests.java

private ConfigurableApplicationContext getServletContext(String profiles, String loginYmlPath,
        String uaaYamlPath, String... resources) {
    String[] resourcesToLoad = resources;
    if (!resources[0].endsWith(".xml")) {
        resourcesToLoad = new String[resources.length - 1];
        System.arraycopy(resources, 1, resourcesToLoad, 0, resourcesToLoad.length);
    }/*from w  w  w .jav a2s  . c  o m*/

    final String[] configLocations = resourcesToLoad;

    AbstractRefreshableWebApplicationContext context = new AbstractRefreshableWebApplicationContext() {

        @Override
        protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory)
                throws BeansException, IOException {
            XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

            // Configure the bean definition reader with this context's
            // resource loading environment.
            beanDefinitionReader.setEnvironment(this.getEnvironment());
            beanDefinitionReader.setResourceLoader(this);
            beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

            if (configLocations != null) {
                for (String configLocation : configLocations) {
                    beanDefinitionReader.loadBeanDefinitions(configLocation);
                }
            }
        }

    };

    if (profiles != null) {
        context.getEnvironment().setActiveProfiles(StringUtils.commaDelimitedListToStringArray(profiles));
    }

    MockServletContext servletContext = new MockServletContext() {
        @Override
        public RequestDispatcher getNamedDispatcher(String path) {
            return new MockRequestDispatcher("/");
        }
    };
    context.setServletContext(servletContext);
    MockServletConfig servletConfig = new MockServletConfig(servletContext);
    servletConfig.addInitParameter("environmentConfigLocations", loginYmlPath + "," + uaaYamlPath);
    context.setServletConfig(servletConfig);

    YamlServletProfileInitializer initializer = new YamlServletProfileInitializer();
    initializer.initialize(context);

    if (profiles != null) {
        context.getEnvironment().setActiveProfiles(StringUtils.commaDelimitedListToStringArray(profiles));
    }

    context.refresh();

    return context;
}