下面列出了org.springframework.context.ApplicationContext#getEnvironment ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
}
}
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);
}
}
}
/**
* 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);
}
}
}
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));
}
@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);
}
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;
}
public Environment getEnvironment() {
SpringApplicationBuilder builder = new SpringApplicationBuilder(TestApp.class)
.web(WebApplicationType.NONE);
builder.bannerMode(Banner.Mode.OFF);
ApplicationContext applicationContext = builder.run();
return applicationContext.getEnvironment();
}
/**
* 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);
}
}
}
@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 ->{
});
};
}
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();
}
}
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));
}
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));
}
/**
* 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);
}
}
}
/**
* {@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);
}
}
}
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));
}
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();
}
}
private void assertHasStandardEnvironment(ApplicationContext ctx) {
Environment defaultEnv = ctx.getEnvironment();
assertThat(defaultEnv, notNullValue());
assertThat(defaultEnv, instanceOf(StandardEnvironment.class));
}
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;
}
private void assertHasStandardEnvironment(ApplicationContext ctx) {
Environment defaultEnv = ctx.getEnvironment();
assertThat(defaultEnv, notNullValue());
assertThat(defaultEnv, instanceOf(StandardEnvironment.class));
}
private void assertHasStandardEnvironment(ApplicationContext ctx) {
Environment defaultEnv = ctx.getEnvironment();
assertThat(defaultEnv, notNullValue());
assertThat(defaultEnv, instanceOf(StandardEnvironment.class));
}