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

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

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

AnnotationConfigApplicationContext.containsBean介绍

暂无

代码示例

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

@Test
public void localAndMetaImportsAreProcessed() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(MultiMetaImportConfigWithLocalImport.class);
  ctx.refresh();
  assertThat(ctx.containsBean("testBean1"), is(true));
  assertThat(ctx.containsBean("testBean2"), is(true));
  assertThat(ctx.containsBean("testBean3"), is(true));
}

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

@Test
public void viaContextRegistration_WithValueAttribute() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(ComponentScanAnnotatedConfig_WithValueAttribute.class);
  ctx.refresh();
  ctx.getBean(ComponentScanAnnotatedConfig_WithValueAttribute.class);
  ctx.getBean(TestBean.class);
  assertThat("config class bean not found", ctx.containsBeanDefinition("componentScanAnnotatedConfig_WithValueAttribute"), is(true));
  assertThat("@ComponentScan annotated @Configuration class registered directly against " +
      "AnnotationConfigApplicationContext did not trigger component scanning as expected",
      ctx.containsBean("fooServiceImpl"), is(true));
}

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

/**
 * Prior to the fix for SPR-8761, this test threw because the nested MyComponent
 * annotation was being falsely considered as a 'lite' Configuration class candidate.
 */
@Test
public void repro() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.scan(getClass().getPackage().getName());
  ctx.refresh();
  assertThat(ctx.containsBean("withNestedAnnotation"), is(true));
}

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

@Test
public void viaContextRegistration_FromPackageOfConfigClass() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(ComponentScanAnnotatedConfigWithImplicitBasePackage.class);
  ctx.refresh();
  ctx.getBean(ComponentScanAnnotatedConfigWithImplicitBasePackage.class);
  assertThat("config class bean not found", ctx.containsBeanDefinition("componentScanAnnotatedConfigWithImplicitBasePackage"), is(true));
  assertThat("@ComponentScan annotated @Configuration class registered directly against " +
      "AnnotationConfigApplicationContext did not trigger component scanning as expected",
      ctx.containsBean("scannedComponent"), is(true));
  assertThat("@Bean method overrides scanned class", ctx.getBean(ConfigurableComponent.class).isFlag(), is(true));
}

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

@Test
public void conditionalOnMissingBeanNoMatch() throws Exception {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(BeanTwoConfiguration.class);
  ctx.refresh();
  assertFalse(ctx.containsBean("bean1"));
  assertTrue(ctx.containsBean("bean2"));
  assertTrue(ctx.containsBean("configurationClassWithConditionTests.BeanTwoConfiguration"));
}

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

@Test
public void withCustomBeanNameGenerator() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(ComponentScanWithBeanNameGenerator.class);
  ctx.refresh();
  assertThat(ctx.containsBean("custom_fooServiceImpl"), is(true));
  assertThat(ctx.containsBean("fooServiceImpl"), is(false));
}

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

@Test
public void controlScan() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.scan(example.scannable._package.class.getPackage().getName());
  ctx.refresh();
  assertThat("control scan for example.scannable package failed to register FooServiceImpl bean",
      ctx.containsBean("fooServiceImpl"), is(true));
}

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

@Test
public void methodConditionalWithAsm() throws Exception {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.registerBeanDefinition("config", new RootBeanDefinition(ConditionOnMethodConfiguration.class.getName()));
  ctx.refresh();
  assertFalse(ctx.containsBean("bean1"));
}

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

@Test
public void mostSpecificDerivedClassDrivesEnvironment_withDerivedDevEnvAndDerivedDevConfigClass() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  StandardEnvironment derivedDevEnv = new StandardEnvironment();
  derivedDevEnv.setActiveProfiles(DERIVED_DEV_ENV_NAME);
  ctx.setEnvironment(derivedDevEnv);
  ctx.register(DerivedDevConfig.class);
  ctx.refresh();
  assertThat("should have dev bean", ctx.containsBean(DEV_BEAN_NAME), is(true));
  assertThat("should have derived dev bean", ctx.containsBean(DERIVED_DEV_BEAN_NAME), is(true));
  assertThat("should have transitive bean", ctx.containsBean(TRANSITIVE_BEAN_NAME), is(true));
}

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

@Test
public void conditionalOnBeanNoMatch() throws Exception {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(BeanThreeConfiguration.class);
  ctx.refresh();
  assertFalse(ctx.containsBean("bean1"));
  assertFalse(ctx.containsBean("bean3"));
}

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

@Test
public void importXml() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlConfig.class);
  assertTrue("did not contain java-declared bean", ctx.containsBean("javaDeclaredBean"));
  assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean"));
  TestBean tb = ctx.getBean("javaDeclaredBean", TestBean.class);
  assertEquals("myName", tb.getName());
  ctx.close();
}

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

@Test
public void importNonXmlResource() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportNonXmlResourceConfig.class);
  assertTrue(ctx.containsBean("propertiesDeclaredBean"));
  ctx.close();
}

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

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

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

@Test
public void metaConditional() throws Exception {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(ConfigurationWithMetaCondition.class);
  ctx.refresh();
  assertTrue(ctx.containsBean("bean"));
}

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

@Test
public void transactionalEventListenerRegisteredProperly() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(EnableTxConfig.class);
  assertTrue(ctx.containsBean(TransactionManagementConfigUtils.TRANSACTIONAL_EVENT_LISTENER_FACTORY_BEAN_NAME));
  assertEquals(1, ctx.getBeansOfType(TransactionalEventListenerFactory.class).size());
  ctx.close();
}

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

@Test
public void nonConfigurationClass() throws Exception {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(NonConfigurationClass.class);
  ctx.refresh();
  assertFalse(ctx.containsBean("bean1"));
}

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

@Test
public void methodConditional() throws Exception {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(ConditionOnMethodConfiguration.class);
  ctx.refresh();
  assertFalse(ctx.containsBean("bean1"));
}

代码示例来源: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类方法