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

The following examples show how to use org.springframework.context.ApplicationContext#getEnvironment() . 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
Source File: ShibcasAuthServlet.java    From shib-cas-authn3 with Apache License 2.0 6 votes vote down vote up
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
Source File: ShibcasAuthServlet.java    From shib-cas-authn3 with Apache License 2.0 6 votes vote down vote up
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
Source File: BeanConfig.java    From kkbinlog with 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 4
Source File: EnvironmentDecryptApplicationInitializer.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
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 5
Source File: EnvironmentSystemIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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 6
Source File: AbstractApplicationContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * {@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 7
Source File: AbstractApplicationContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 8
Source File: AlbedoAdminApplication.java    From albedo with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 9
Source File: AlbedoApiApplication.java    From albedo with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 10
Source File: EnvironmentDecryptApplicationInitializer.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
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
Source File: AbstractApplicationContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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 12
Source File: AbstractApplicationContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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 13
Source File: MongoCfEnvProcessorTests.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
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 14
Source File: AbstractCfEnvTests.java    From java-cfenv with 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 15
Source File: IOCTest_PropertyValue.java    From code with 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 16
Source File: EnvironmentSystemIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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 17
Source File: EnvironmentSystemIntegrationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void assertHasStandardEnvironment(ApplicationContext ctx) {
	Environment defaultEnv = ctx.getEnvironment();
	assertThat(defaultEnv, notNullValue());
	assertThat(defaultEnv, instanceOf(StandardEnvironment.class));
}
 
Example 18
Source File: EnvironmentSystemIntegrationTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private void assertHasStandardEnvironment(ApplicationContext ctx) {
	Environment defaultEnv = ctx.getEnvironment();
	assertThat(defaultEnv, notNullValue());
	assertThat(defaultEnv, instanceOf(StandardEnvironment.class));
}
 
Example 19
Source File: CamelAutoConfiguration.java    From camel-spring-boot with Apache License 2.0 4 votes vote down vote up
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 20
Source File: EnvironmentSystemIntegrationTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private void assertHasStandardEnvironment(ApplicationContext ctx) {
	Environment defaultEnv = ctx.getEnvironment();
	assertThat(defaultEnv, notNullValue());
	assertThat(defaultEnv, instanceOf(StandardEnvironment.class));
}