org.springframework.context.annotation.AnnotationConfigApplicationContext类的使用及代码示例

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

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

AnnotationConfigApplicationContext介绍

[英]Standalone application context, accepting annotated classes as input - in particular Configuration-annotated classes, but also plain org.springframework.stereotype.Component types and JSR-330 compliant classes using javax.inject annotations. Allows for registering classes one by one using #register(Class...) as well as for classpath scanning using #scan(String...).

In case of multiple @Configuration classes, @ Bean methods defined in later classes will override those defined in earlier classes. This can be leveraged to deliberately override certain bean definitions via an extra @Configurationclass.

See @ Configuration's javadoc for usage examples.
[中]独立的应用程序上下文,接受带注释的类作为输入-特别是配置带注释的类,但也接受纯org。springframework。刻板印象使用javax的组件类型和符合JSR-330的类。注入注释。允许使用#寄存器(类…)逐个注册类以及使用#scan(String…)进行类路径扫描。
在多个@Configuration类的情况下,在后面的类中定义的@Bean方法将覆盖在前面的类中定义的方法。这可以通过一个额外的@Configurationclass故意覆盖某些bean定义。
有关用法示例,请参阅@Configuration的javadoc。

代码示例

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

@Before
public void setup() {
  ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
  this.cacheManager = context.getBean("cacheManager", CacheManager.class);
  this.anotherCacheManager = context.getBean("anotherCacheManager", CacheManager.class);
  this.simpleService = context.getBean(SimpleService.class);
}

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

@Test
public void beanMethodThroughAopProxy() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(Config.class);
  ctx.register(AnnotationAwareAspectJAutoProxyCreator.class);
  ctx.register(TestAdvisor.class);
  ctx.refresh();
  ctx.getBean("testBean", TestBean.class);
}

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

@Test
public void txManagerIsResolvedOnInvocationOfTransactionalMethod() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
      EnableTxConfig.class, TxManagerConfig.class);
  TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
  // invoke a transactional method, causing the PlatformTransactionManager bean to be resolved.
  bean.findAllFoos();
  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 withInnerClassAndLambdaExpression() {
  ApplicationContext ctx = new AnnotationConfigApplicationContext(Application.class, CountingAspect.class);
  ctx.getBeansOfType(Runnable.class).forEach((k, v) -> v.run());
  // TODO: returns just 1 as of AspectJ 1.9 beta 3, not detecting the applicable lambda expression anymore
  // assertEquals(2, ctx.getBean(CountingAspect.class).count);
}

代码示例来源: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();
}

代码示例来源: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 transactionProxyIsCreated() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
      EnableTxConfig.class, TxManagerConfig.class);
  TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
  assertTrue("testBean is not a proxy", AopUtils.isAopProxy(bean));
  Map<?,?> services = ctx.getBeansWithAnnotation(Service.class);
  assertTrue("Stereotype annotation not visible", services.containsKey("testBean"));
  ctx.close();
}

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

@Test
public void importXmlWithOtherConfigurationClass() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlWithConfigurationClass.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 transactionProxyIsCreatedWithEnableOnSuperclass() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
      InheritedEnableTxConfig.class, TxManagerConfig.class);
  TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
  assertTrue("testBean is not a proxy", AopUtils.isAopProxy(bean));
  Map<?,?> services = ctx.getBeansWithAnnotation(Service.class);
  assertTrue("Stereotype annotation not visible", services.containsKey("testBean"));
  ctx.close();
}

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

@Test
public void importXmlIsInheritedFromSuperclassDeclarations() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(FirstLevelSubConfig.class);
  assertTrue(ctx.containsBean("xmlDeclaredBean"));
  ctx.close();
}

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

@Test
public void testNullArgumentThroughBeanMethodCall() {
  ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanArgumentConfigWithNull.class);
  ctx.getBean("aFoo");
}

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

@Test
public void withMultipleAnnotationIncludeFilters1() throws IOException, ClassNotFoundException {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(ComponentScanWithMultipleAnnotationIncludeFilters1.class);
  ctx.refresh();
  ctx.getBean(DefaultNamedComponent.class); // @CustomStereotype-annotated
  ctx.getBean(MessageBean.class);           // @CustomComponent-annotated
}

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

@Test
public void txManagerIsResolvedCorrectlyWhenMultipleManagersArePresent() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
      EnableTxConfig.class, MultiTxManagerConfig.class);
  TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
  // invoke a transactional method, causing the PlatformTransactionManager bean to be resolved.
  bean.findAllFoos();
  ctx.close();
}

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

@Test
public void importXmlIsMergedFromSuperclassDeclarations() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SecondLevelSubConfig.class);
  assertTrue("failed to pick up second-level-declared XML bean", ctx.containsBean("secondLevelXmlDeclaredBean"));
  assertTrue("failed to pick up parent-declared XML bean", ctx.containsBean("xmlDeclaredBean"));
  ctx.close();
}

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

@Test(expected = BeanDefinitionStoreException.class)
public void testNameClashBetweenConfigurationClassAndBean() {
  ApplicationContext ctx = new AnnotationConfigApplicationContext(MyTestBean.class);
  ctx.getBean("myTestBean", TestBean.class);
}

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

@Test
public void withMultipleAnnotationIncludeFilters2() throws IOException, ClassNotFoundException {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(ComponentScanWithMultipleAnnotationIncludeFilters2.class);
  ctx.refresh();
  ctx.getBean(DefaultNamedComponent.class); // @CustomStereotype-annotated
  ctx.getBean(MessageBean.class);           // @CustomComponent-annotated
}

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

@Test
public void importXmlWithAutowiredConfig() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlAutowiredConfig.class);
  String name = ctx.getBean("xmlBeanName", String.class);
  assertThat(name, equalTo("xml.declared"));
  ctx.close();
}

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

@Test
public void testInjectionPointMatchForNarrowTargetReturnType() {
  ApplicationContext ctx = new AnnotationConfigApplicationContext(FooBarConfiguration.class);
  assertSame(ctx.getBean(BarImpl.class), ctx.getBean(FooImpl.class).bar);
}

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

@Test
public void spr12233() throws Exception {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(PropertySourcesPlaceholderConfigurer.class);
  ctx.register(ImportConfiguration.class);
  ctx.refresh();
  ctx.close();
}

相关文章

微信公众号

最新文章

更多

AnnotationConfigApplicationContext类方法