org.springframework.context.annotation.AnnotationConfigApplicationContext.getEnvironment()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(10.1k)|赞(0)|评价(0)|浏览(124)

本文整理了Java中org.springframework.context.annotation.AnnotationConfigApplicationContext.getEnvironment()方法的一些代码示例,展示了AnnotationConfigApplicationContext.getEnvironment()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AnnotationConfigApplicationContext.getEnvironment()方法的具体详情如下:
包路径:org.springframework.context.annotation.AnnotationConfigApplicationContext
类名称:AnnotationConfigApplicationContext
方法名:getEnvironment

AnnotationConfigApplicationContext.getEnvironment介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Test
public void withNameAndMultipleResourceLocations() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNameAndMultipleResourceLocations.class);
  assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true));
  assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true));
  // p2 should 'win' as it was registered last
  assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void orderingWithAndWithoutNameAndMultipleResourceLocations() {
  // SPR-10820: p2 should 'win' as it was registered last
  AnnotationConfigApplicationContext ctxWithName = new AnnotationConfigApplicationContext(ConfigWithNameAndMultipleResourceLocations.class);
  AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(ConfigWithMultipleResourceLocations.class);
  assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
  assertThat(ctxWithName.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void withSameSourceImportedInDifferentOrder() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithSameSourceImportedInDifferentOrder.class);
  assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true));
  assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true));
  assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void withMultipleResourceLocations() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithMultipleResourceLocations.class);
  assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true));
  assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true));
  // p2 should 'win' as it was registered last
  assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void withNamedPropertySources() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNamedPropertySources.class);
  assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true));
  assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true));
  // p2 should 'win' as it was registered last
  assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void withIgnoredPropertySource() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithIgnoredPropertySource.class);
  assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true));
  assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void withPropertySources() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithPropertySources.class);
  assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true));
  assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true));
  // p2 should 'win' as it was registered last
  assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void orderingWithAndWithoutNameAndFourResourceLocations() {
  // SPR-12198: p4 should 'win' as it was registered last
  AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(ConfigWithFourResourceLocations.class);
  assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("p4TestBean"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void orderingDoesntReplaceExisting() throws Exception {
  // SPR-12198: mySource should 'win' as it was registered manually
  AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext();
  MapPropertySource mySource = new MapPropertySource("mine", Collections.singletonMap("testbean.name", "myTestBean"));
  ctxWithoutName.getEnvironment().getPropertySources().addLast(mySource);
  ctxWithoutName.register(ConfigWithFourResourceLocations.class);
  ctxWithoutName.refresh();
  assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("myTestBean"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void shouldInvokeAwareMethodsInImportBeanDefinitionRegistrar() {
  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
  context.getBean(MessageSource.class);
  assertThat(SampleRegistrar.beanFactory, is(context.getBeanFactory()));
  assertThat(SampleRegistrar.classLoader, is(context.getBeanFactory().getBeanClassLoader()));
  assertThat(SampleRegistrar.resourceLoader, is(notNullValue()));
  assertThat(SampleRegistrar.environment, is(context.getEnvironment()));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void invokeAwareMethodsInImportSelector() {
  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class);
  assertThat(SampleImportSelector.beanFactory, is(context.getBeanFactory()));
  assertThat(SampleImportSelector.classLoader, is(context.getBeanFactory().getBeanClassLoader()));
  assertThat(SampleImportSelector.resourceLoader, is(notNullValue()));
  assertThat(SampleImportSelector.environment, is(context.getEnvironment()));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void invokeAwareMethodsInImportGroup() {
  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(GroupedConfig1.class);
  assertThat(TestImportGroup.beanFactory, is(context.getBeanFactory()));
  assertThat(TestImportGroup.classLoader, is(context.getBeanFactory().getBeanClassLoader()));
  assertThat(TestImportGroup.resourceLoader, is(notNullValue()));
  assertThat(TestImportGroup.environment, is(context.getEnvironment()));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void withImplicitName() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(ConfigWithImplicitName.class);
  ctx.refresh();
  assertTrue("property source p1 was not added",
      ctx.getEnvironment().getPropertySources().contains("class path resource [org/springframework/context/annotation/p1.properties]"));
  assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void withAwareTypeFilter() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithAwareTypeFilter.class);
  assertTrue(ctx.getEnvironment().acceptsProfiles(Profiles.of("the-filter-ran")));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testIntegrationWithAnnotationConfigApplicationContext_validMetaAnnotatedProfile() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.getEnvironment().setActiveProfiles(DevComponent.PROFILE_NAME);
  ctx.register(ProfileMetaAnnotatedComponent.class);
  ctx.refresh();
  assertThat(ctx.containsBean(ProfileMetaAnnotatedComponent.BEAN_NAME), is(true));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testIntegrationWithAnnotationConfigApplicationContext_invalidProfile() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.getEnvironment().setActiveProfiles("other");
  ctx.register(ProfileAnnotatedComponent.class);
  ctx.refresh();
  assertThat(ctx.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(false));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testIntegrationWithAnnotationConfigApplicationContext_defaultProfile() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.getEnvironment().setDefaultProfiles(TEST_DEFAULT_PROFILE_NAME);
  // no active profiles are set
  ctx.register(DefaultProfileAnnotatedComponent.class);
  ctx.refresh();
  assertThat(ctx.containsBean(DefaultProfileAnnotatedComponent.BEAN_NAME), is(true));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testIntegrationWithAnnotationConfigApplicationContext_invalidMetaAnnotatedProfile() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.getEnvironment().setActiveProfiles("other");
  ctx.register(ProfileMetaAnnotatedComponent.class);
  ctx.refresh();
  assertThat(ctx.containsBean(ProfileMetaAnnotatedComponent.BEAN_NAME), is(false));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testIntegrationWithAnnotationConfigApplicationContext_validProfile() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.getEnvironment().setActiveProfiles(ProfileAnnotatedComponent.PROFILE_NAME);
  ctx.register(ProfileAnnotatedComponent.class);
  ctx.refresh();
  assertThat(ctx.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(true));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void importWithPlaceholder() throws Exception {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  PropertySource<?> propertySource = new MapPropertySource("test",
      Collections.<String, Object> singletonMap("test", "springframework"));
  ctx.getEnvironment().getPropertySources().addFirst(propertySource);
  ctx.register(ImportXmlConfig.class);
  ctx.refresh();
  assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean"));
  ctx.close();
}

相关文章

微信公众号

最新文章

更多

AnnotationConfigApplicationContext类方法