Java Code Examples for org.springframework.context.ApplicationContext#getEnvironment()

The following examples show how to use org.springframework.context.ApplicationContext#getEnvironment() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private void buildParameterBuilders(final ApplicationContext applicationContext) {
    final Environment environment = applicationContext.getEnvironment();
    final String builders = StringUtils.defaultString(environment.getProperty("shibcas.parameterBuilders", ""));
    for (final String parameterBuilder : StringUtils.split(builders, ";")) {
        try {
            logger.debug("Loading parameter builder class {}", parameterBuilder);
            final Class clazz = Class.forName(parameterBuilder);
            final IParameterBuilder builder = IParameterBuilder.class.cast(clazz.newInstance());
            if (builder instanceof ApplicationContextAware) {
                ((ApplicationContextAware) builder).setApplicationContext(applicationContext);
            }
            this.parameterBuilders.add(builder);
            logger.debug("Added parameter builder {}", parameterBuilder);
        } catch (final Throwable e) {
            logger.error("Error building parameter builder with name: " + parameterBuilder, e);
        }
    }
}
 
Example 2
private void buildParameterBuilders(final ApplicationContext applicationContext) {
    final Environment environment = applicationContext.getEnvironment();
    final String builders = StringUtils.defaultString(environment.getProperty("shibcas.parameterBuilders", ""));
    for (final String parameterBuilder : StringUtils.split(builders, ";")) {
        try {
            logger.debug("Loading parameter builder class {}", parameterBuilder);
            final Class clazz = Class.forName(parameterBuilder);
            final IParameterBuilder builder = IParameterBuilder.class.cast(clazz.newInstance());
            if (builder instanceof ApplicationContextAware) {
                ((ApplicationContextAware) builder).setApplicationContext(applicationContext);
            }
            this.parameterBuilders.add(builder);
            logger.debug("Added parameter builder {}", parameterBuilder);
        } catch (final Throwable e) {
            logger.error("Error building parameter builder with name: " + parameterBuilder, e);
        }
    }
}
 
Example 3
/**
 * Set the parent of this application context.
 * <p>The parent {@linkplain ApplicationContext#getEnvironment() environment} is
 * {@linkplain ConfigurableEnvironment#merge(ConfigurableEnvironment) merged} with
 * this (child) application context environment if the parent is non-{@code null} and
 * its environment is an instance of {@link ConfigurableEnvironment}.
 * @see ConfigurableEnvironment#merge(ConfigurableEnvironment)
 */
@Override
public void setParent(@Nullable ApplicationContext parent) {
	this.parent = parent;
	if (parent != null) {
		Environment parentEnvironment = parent.getEnvironment();
		if (parentEnvironment instanceof ConfigurableEnvironment) {
			getEnvironment().merge((ConfigurableEnvironment) parentEnvironment);
		}
	}
}
 
Example 4
private void assertHasEnvironment(ApplicationContext ctx, Environment expectedEnv) {
	// ensure the custom environment took
	Environment actualEnv = ctx.getEnvironment();
	assertThat(actualEnv, notNullValue());
	assertThat(actualEnv, is(expectedEnv));
	// ensure environment is registered as a bean
	assertThat(ctx.containsBean(ENVIRONMENT_BEAN_NAME), is(true));
}
 
Example 5
Source Project: code   File: IOCTest_PropertyValue.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test01() {
    //1.创建IOC容器
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
    System.out.println("容器初始化...");

    Person person = (Person) applicationContext.getBean("person");
    System.out.println(person);

    Environment environment = applicationContext.getEnvironment();
    String property = environment.getProperty("person.nickname");
    System.out.println(property);

    //printBeans(applicationContext);
}
 
Example 6
Source Project: java-cfenv   File: AbstractCfEnvTests.java    License: Apache License 2.0 5 votes vote down vote up
public Environment getEnvironment(Map<String, Object> properties) {
	SpringApplicationBuilder builder = new SpringApplicationBuilder(TestApp.class)
		.web(WebApplicationType.NONE);
	if (!CollectionUtils.isEmpty(properties)) {
		builder.properties(properties);
	}
	builder.bannerMode(Banner.Mode.OFF);
	ApplicationContext applicationContext = builder.run();
	Environment environment = applicationContext.getEnvironment();
	((ConfigurableApplicationContext) applicationContext).close();
	return environment;
}
 
Example 7
public Environment getEnvironment() {
	SpringApplicationBuilder builder = new SpringApplicationBuilder(TestApp.class)
			.web(WebApplicationType.NONE);
	builder.bannerMode(Banner.Mode.OFF);
	ApplicationContext applicationContext = builder.run();
	return applicationContext.getEnvironment();
}
 
Example 8
/**
 * Set the parent of this application context.
 * <p>The parent {@linkplain ApplicationContext#getEnvironment() environment} is
 * {@linkplain ConfigurableEnvironment#merge(ConfigurableEnvironment) merged} with
 * this (child) application context environment if the parent is non-{@code null} and
 * its environment is an instance of {@link ConfigurableEnvironment}.
 * @see ConfigurableEnvironment#merge(ConfigurableEnvironment)
 */
@Override
public void setParent(@Nullable ApplicationContext parent) {
	this.parent = parent;
	if (parent != null) {
		Environment parentEnvironment = parent.getEnvironment();
		if (parentEnvironment instanceof ConfigurableEnvironment) {
			getEnvironment().merge((ConfigurableEnvironment) parentEnvironment);
		}
	}
}
 
Example 9
Source Project: kkbinlog   File: BeanConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
    return args -> {
        Environment env = ctx.getEnvironment();
        log.info("server.port=>{}",env.getProperty("server.port"));
        log.info("spring.redisson.address=>{}",env.getProperty("spring.redisson.address"));
        log.info("spring.redisson.database=>{}", env.getProperty("spring.redisson.database"));
        String[] beanDefinitionNames =  ctx.getBeanDefinitionNames();
        Arrays.stream(beanDefinitionNames).sorted().forEach(val ->{
        });
    };
}
 
Example 10
private void removeDecryptedProperties(ApplicationContext applicationContext) {
	ApplicationContext parent = applicationContext;
	while (parent != null) {
		if (parent.getEnvironment() instanceof ConfigurableEnvironment) {
			((ConfigurableEnvironment) parent.getEnvironment()).getPropertySources()
					.remove(DECRYPTED_PROPERTY_SOURCE_NAME);
		}
		parent = parent.getParent();
	}
}
 
Example 11
public static void main(String[] args) throws Exception {
	SpringApplication app = new SpringApplication(AlbedoApiApplication.class);
	final ApplicationContext applicationContext = app.run(args);
	Environment env = applicationContext.getEnvironment();
	log.info("\n----------------------------------------------------------\n\t" +
			"Application '{}' is running! Access URLs:\n\t" +
			"Local: \t\thttp://localhost:{}\n\t" +
			"External: \thttp://{}:{}\n\t" +
			"Swagger: \thttp://localhost:{}\n----------------------------------------------------------",
		env.getProperty(SPRING_APPLICATION_NAME),
		env.getProperty(SERVER_PORT),
		InetAddress.getLocalHost().getHostAddress(),
		env.getProperty(SERVER_PORT),
		env.getProperty(SERVER_PORT));
}
 
Example 12
public static void main(String[] args) throws Exception {
	SpringApplication app = new SpringApplication(AlbedoAdminApplication.class);
	final ApplicationContext applicationContext = app.run(args);
	Environment env = applicationContext.getEnvironment();
	log.info("\n----------------------------------------------------------\n\t" +
			"Application '{}' is running! Access URLs:\n\t" +
			"Local: \t\thttp://localhost:{}\n\t" +
			"External: \thttp://{}:{}\n----------------------------------------------------------",
		env.getProperty(SPRING_APPLICATION_NAME),
		env.getProperty(SERVER_PORT),
		InetAddress.getLocalHost().getHostAddress(),
		env.getProperty(SERVER_PORT));
}
 
Example 13
/**
 * Set the parent of this application context.
 * <p>The parent {@linkplain ApplicationContext#getEnvironment() environment} is
 * {@linkplain ConfigurableEnvironment#merge(ConfigurableEnvironment) merged} with
 * this (child) application context environment if the parent is non-{@code null} and
 * its environment is an instance of {@link ConfigurableEnvironment}.
 * @see ConfigurableEnvironment#merge(ConfigurableEnvironment)
 */
@Override
public void setParent(ApplicationContext parent) {
	this.parent = parent;
	if (parent != null) {
		Environment parentEnvironment = parent.getEnvironment();
		if (parentEnvironment instanceof ConfigurableEnvironment) {
			getEnvironment().merge((ConfigurableEnvironment) parentEnvironment);
		}
	}
}
 
Example 14
/**
 * {@inheritDoc}
 * <p>The parent {@linkplain ApplicationContext#getEnvironment() environment} is
 * {@linkplain ConfigurableEnvironment#merge(ConfigurableEnvironment) merged} with
 * this (child) application context environment if the parent is non-{@code null} and
 * its environment is an instance of {@link ConfigurableEnvironment}.
 * @see ConfigurableEnvironment#merge(ConfigurableEnvironment)
 */
@Override
public void setParent(ApplicationContext parent) {
	this.parent = parent;
	if (parent != null) {
		Environment parentEnvironment = parent.getEnvironment();
		if (parentEnvironment instanceof ConfigurableEnvironment) {
			getEnvironment().merge((ConfigurableEnvironment) parentEnvironment);
		}
	}
}
 
Example 15
private void assertHasEnvironment(ApplicationContext ctx, Environment expectedEnv) {
	// ensure the custom environment took
	Environment actualEnv = ctx.getEnvironment();
	assertThat(actualEnv, notNullValue());
	assertThat(actualEnv, is(expectedEnv));
	// ensure environment is registered as a bean
	assertThat(ctx.containsBean(ENVIRONMENT_BEAN_NAME), is(true));
}
 
Example 16
private void insert(ApplicationContext applicationContext,
		PropertySource<?> propertySource) {
	ApplicationContext parent = applicationContext;
	while (parent != null) {
		if (parent.getEnvironment() instanceof ConfigurableEnvironment) {
			ConfigurableEnvironment mutable = (ConfigurableEnvironment) parent
					.getEnvironment();
			insert(mutable.getPropertySources(), propertySource);
		}
		parent = parent.getParent();
	}
}
 
Example 17
private void assertHasStandardEnvironment(ApplicationContext ctx) {
	Environment defaultEnv = ctx.getEnvironment();
	assertThat(defaultEnv, notNullValue());
	assertThat(defaultEnv, instanceOf(StandardEnvironment.class));
}
 
Example 18
static CamelContext doConfigureCamelContext(ApplicationContext applicationContext,
                                            CamelContext camelContext,
                                            CamelConfigurationProperties config) throws Exception {

    camelContext.build();

    // initialize properties component eager
    PropertiesComponent pc = applicationContext.getBeanProvider(PropertiesComponent.class).getIfAvailable();
    if (pc != null) {
        pc.setCamelContext(camelContext);
        camelContext.setPropertiesComponent(pc);
    }

    final Map<String, BeanRepository> repositories = applicationContext.getBeansOfType(BeanRepository.class);
    if (!repositories.isEmpty()) {
        List<BeanRepository> reps = new ArrayList<>();
        // include default bean repository as well
        reps.add(new ApplicationContextBeanRepository(applicationContext));
        // and then any custom
        reps.addAll(repositories.values());
        // sort by ordered
        OrderComparator.sort(reps);
        // and plugin as new registry
        camelContext.adapt(ExtendedCamelContext.class).setRegistry(new DefaultRegistry(reps));
    }

    if (ObjectHelper.isNotEmpty(config.getFileConfigurations())) {
        Environment env = applicationContext.getEnvironment();
        if (env instanceof ConfigurableEnvironment) {
            MutablePropertySources sources = ((ConfigurableEnvironment) env).getPropertySources();
            if (sources != null) {
                if (!sources.contains("camel-file-configuration")) {
                    sources.addFirst(new FilePropertySource("camel-file-configuration", applicationContext, config.getFileConfigurations()));
                }
            }
        }
    }

    camelContext.adapt(ExtendedCamelContext.class).setPackageScanClassResolver(new FatJarPackageScanClassResolver());

    if (config.getRouteFilterIncludePattern() != null || config.getRouteFilterExcludePattern() != null) {
        LOG.info("Route filtering pattern: include={}, exclude={}", config.getRouteFilterIncludePattern(), config.getRouteFilterExcludePattern());
        camelContext.getExtension(Model.class).setRouteFilterPattern(config.getRouteFilterIncludePattern(), config.getRouteFilterExcludePattern());
    }

    // configure the common/default options
    DefaultConfigurationConfigurer.configure(camelContext, config);
    // lookup and configure SPI beans
    DefaultConfigurationConfigurer.afterConfigure(camelContext);
    // and call after all properties are set
    DefaultConfigurationConfigurer.afterPropertiesSet(camelContext);

    return camelContext;
}
 
Example 19
private void assertHasStandardEnvironment(ApplicationContext ctx) {
	Environment defaultEnv = ctx.getEnvironment();
	assertThat(defaultEnv, notNullValue());
	assertThat(defaultEnv, instanceOf(StandardEnvironment.class));
}
 
Example 20
private void assertHasStandardEnvironment(ApplicationContext ctx) {
	Environment defaultEnv = ctx.getEnvironment();
	assertThat(defaultEnv, notNullValue());
	assertThat(defaultEnv, instanceOf(StandardEnvironment.class));
}