Example usage for org.springframework.context.support GenericApplicationContext getEnvironment

List of usage examples for org.springframework.context.support GenericApplicationContext getEnvironment

Introduction

In this page you can find the example usage for org.springframework.context.support GenericApplicationContext getEnvironment.

Prototype

@Override
public ConfigurableEnvironment getEnvironment() 

Source Link

Document

Return the Environment for this application context in configurable form, allowing for further customization.

Usage

From source file:org.craftercms.engine.service.context.SiteContextFactory.java

protected ConfigurableApplicationContext getApplicationContext(SiteContext siteContext,
        URLClassLoader classLoader, HierarchicalConfiguration config, String[] applicationContextPaths,
        ResourceLoader resourceLoader) {
    try {/*from  www  .j  a  va 2 s  .  c o m*/
        List<Resource> resources = new ArrayList<>();

        for (String path : applicationContextPaths) {
            Resource resource = resourceLoader.getResource(path);
            if (resource.exists()) {
                resources.add(resource);
            }
        }

        if (CollectionUtils.isNotEmpty(resources)) {
            String siteName = siteContext.getSiteName();

            logger.info("--------------------------------------------------");
            logger.info("<Loading application context for site: " + siteName + ">");
            logger.info("--------------------------------------------------");

            GenericApplicationContext appContext = new GenericApplicationContext(mainApplicationContext);
            appContext.setClassLoader(classLoader);

            if (config != null) {
                MutablePropertySources propertySources = appContext.getEnvironment().getPropertySources();
                propertySources.addFirst(new ApacheCommonsConfiguration2PropertySource("siteConfig", config));
            }

            XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(appContext);
            reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);

            for (Resource resource : resources) {
                reader.loadBeanDefinitions(resource);
            }

            appContext.refresh();

            logger.info("--------------------------------------------------");
            logger.info("</Loading application context for site: " + siteName + ">");
            logger.info("--------------------------------------------------");

            return appContext;
        } else {
            return null;
        }
    } catch (Exception e) {
        throw new SiteContextCreationException(
                "Unable to load application context for site '" + siteContext.getSiteName() + "'", e);
    }
}

From source file:org.springframework.cloud.function.web.function.FunctionEndpointInitializer.java

@Override
public void initialize(GenericApplicationContext context) {
    if (ContextFunctionCatalogInitializer.enabled
            && context.getEnvironment().getProperty(FunctionalSpringApplication.SPRING_WEB_APPLICATION_TYPE,
                    WebApplicationType.class, WebApplicationType.REACTIVE) == WebApplicationType.REACTIVE
            && context.getEnvironment().getProperty("spring.functional.enabled", Boolean.class, false)
            && ClassUtils.isPresent("org.springframework.http.server.reactive.HttpHandler", null)) {
        registerEndpoint(context);/*from   w  w  w . ja va  2s  .  co m*/
        registerWebFluxAutoConfiguration(context);
    }
}

From source file:org.springframework.cloud.function.web.function.FunctionEndpointInitializer.java

private void registerEndpoint(GenericApplicationContext context) {
    context.registerBean(StringConverter.class,
            () -> new BasicStringConverter(context.getBean(FunctionInspector.class), context.getBeanFactory()));
    context.registerBean(RequestProcessor.class,
            () -> new RequestProcessor(context.getBean(FunctionInspector.class),
                    context.getBeanProvider(JsonMapper.class), context.getBean(StringConverter.class),
                    context.getBeanProvider(ServerCodecConfigurer.class)));
    context.registerBean(FunctionEndpointFactory.class,
            () -> new FunctionEndpointFactory(context.getBean(FunctionCatalog.class),
                    context.getBean(FunctionInspector.class), context.getBean(RequestProcessor.class),
                    context.getEnvironment()));
    context.registerBean(RouterFunction.class,
            () -> context.getBean(FunctionEndpointFactory.class).functionEndpoints());
}

From source file:org.springframework.integration.samples.tcpclientserver.support.CustomTestContextLoader.java

@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);
    }//from w  w  w.j a va  2 s  .c o  m

    final MapPropertySource propertySource = new MapPropertySource("sockets", sockets);

    context.getEnvironment().getPropertySources().addLast(propertySource);
    super.loadBeanDefinitions(context, mergedConfig);
}

From source file:org.springframework.yarn.test.context.YarnClusterInjectingAnnotationConfigContextLoader.java

@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);
    }//from   w ww . j  a v  a2 s. co m
}