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

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

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

AnnotationConfigApplicationContext.getBeanFactory介绍

暂无

代码示例

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

@Override
protected ConfigurableApplicationContext getApplicationContext() {
  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  context.getBeanFactory().registerSingleton("cachingProvider", getCachingProvider());
  context.register(EnableCachingConfig.class);
  context.refresh();
  jCacheManager = context.getBean("jCacheManager", CacheManager.class);
  return context;
}

代码示例来源: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 testCustomWithAttributeOverride() {
  AnnotationConfigApplicationContext ctx =
      new AnnotationConfigApplicationContext(CustomConfigWithAttributeOverride.class, CustomPojo.class);
  assertFalse(ctx.getBeanFactory().containsSingleton("testBeanX"));
  CustomPojo pojo = ctx.getBean(CustomPojo.class);
  assertThat(pojo.testBean.getName(), equalTo("interesting"));
}

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

@Test
public void testCustomWithAsm() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.registerBeanDefinition("customConfig", new RootBeanDefinition(CustomConfig.class.getName()));
  RootBeanDefinition customPojo = new RootBeanDefinition(CustomPojo.class.getName());
  customPojo.setLazyInit(true);
  ctx.registerBeanDefinition("customPojo", customPojo);
  ctx.refresh();
  assertFalse(ctx.getBeanFactory().containsSingleton("testBean1"));
  assertFalse(ctx.getBeanFactory().containsSingleton("testBean2"));
  CustomPojo pojo = ctx.getBean(CustomPojo.class);
  assertThat(pojo.testBean.getName(), equalTo("interesting"));
}

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

@Test
public void testStandard() {
  AnnotationConfigApplicationContext ctx =
      new AnnotationConfigApplicationContext(StandardConfig.class, StandardPojo.class);
  assertFalse(ctx.getBeanFactory().containsSingleton("testBean1"));
  StandardPojo pojo = ctx.getBean(StandardPojo.class);
  assertThat(pojo.testBean.getName(), equalTo("interesting"));
  assertThat(pojo.testBean2.getName(), equalTo("boring"));
}

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

@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  context.refresh();
  ReactiveAdapterRegistry registry = ReactiveAdapterRegistry.getSharedInstance();
  this.resolver = new RequestAttributeMethodArgumentResolver(context.getBeanFactory(), registry);
}

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

@Test
public void testScoped() {
  AnnotationConfigApplicationContext ctx =
      new AnnotationConfigApplicationContext(ScopedConfig.class, StandardPojo.class);
  assertFalse(ctx.getBeanFactory().containsSingleton("testBean1"));
  StandardPojo pojo = ctx.getBean(StandardPojo.class);
  assertThat(pojo.testBean.getName(), equalTo("interesting"));
  assertThat(pojo.testBean2.getName(), equalTo("boring"));
}

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

@Test
public void testScopedProxy() {
  AnnotationConfigApplicationContext ctx =
      new AnnotationConfigApplicationContext(ScopedProxyConfig.class, StandardPojo.class);
  assertTrue(ctx.getBeanFactory().containsSingleton("testBean1"));  // a shared scoped proxy
  StandardPojo pojo = ctx.getBean(StandardPojo.class);
  assertThat(pojo.testBean.getName(), equalTo("interesting"));
  assertThat(pojo.testBean2.getName(), equalTo("boring"));
}

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

/**
 * Test that values supplied to @Configuration(value="...") are propagated as the
 * bean name for the configuration class even in the case of inclusion via @Import
 * or in the case of automatic registration via nesting
 */
@Test
public void reproSpr9023() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(B.class);
  ctx.refresh();
  System.out.println(ctx.getBeanFactory());
  assertThat(ctx.getBeanNamesForType(B.class)[0], is("config-b"));
  assertThat(ctx.getBeanNamesForType(A.class)[0], is("config-a"));
}

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

@Test
public void individualNamedBeanWithSupplier() {
  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  context.registerBean("a", BeanA.class,
      () -> new BeanA(context.getBean(BeanB.class), context.getBean(BeanC.class)));
  context.registerBean("b", BeanB.class, BeanB::new);
  context.registerBean("c", BeanC.class, BeanC::new);
  context.refresh();
  assertTrue(context.getBeanFactory().containsSingleton("a"));
  assertSame(context.getBean("b", BeanB.class), context.getBean(BeanA.class).b);
  assertSame(context.getBean("c"), context.getBean("a", BeanA.class).c);
  assertSame(context, context.getBean("b", BeanB.class).applicationContext);
}

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

@Test
public void withScopedProxyThroughAspectJPattern() throws IOException, ClassNotFoundException {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(ComponentScanWithScopedProxyThroughAspectJPattern.class);
  ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
  ctx.refresh();
  // should cast to the interface
  FooService bean = (FooService) ctx.getBean("scopedProxyTestBean");
  // should be dynamic proxy
  assertThat(AopUtils.isJdkDynamicProxy(bean), is(true));
}

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

@Before
@SuppressWarnings("resource")
public void setup() {
  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  context.refresh();
  ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
  this.resolver = new SessionAttributeMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);
  this.session = mock(WebSession.class);
  this.exchange = MockServerWebExchange.builder(MockServerHttpRequest.get("/")).session(this.session).build();
  this.handleMethod = ReflectionUtils.findMethod(getClass(), "handleWithSessionAttribute", (Class<?>[]) null);
}

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

@Test
public void testCustomWithEarlyResolution() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(CustomConfig.class, CustomPojo.class);
  ctx.refresh();
  assertFalse(ctx.getBeanFactory().containsSingleton("testBean1"));
  assertFalse(ctx.getBeanFactory().containsSingleton("testBean2"));
  ctx.getBean("testBean2");
  assertTrue(BeanFactoryAnnotationUtils.isQualifierMatch(value -> value.equals("boring"),
      "testBean2", ctx.getDefaultListableBeanFactory()));
  CustomPojo pojo = ctx.getBean(CustomPojo.class);
  assertThat(pojo.testBean.getName(), equalTo("interesting"));
}

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

@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  context.refresh();
  ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
  this.resolver = new ExpressionValueMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);
  Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
  this.paramSystemProperty = new MethodParameter(method, 0);
  this.paramNotSupported = new MethodParameter(method, 1);
  this.paramAlsoNotSupported = new MethodParameter(method, 2);
}

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

@Test
public void withScopedProxyThroughRegex() throws IOException, ClassNotFoundException {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(ComponentScanWithScopedProxyThroughRegex.class);
  ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
  ctx.refresh();
  // should cast to the interface
  FooService bean = (FooService) ctx.getBean("scopedProxyTestBean");
  // should be dynamic proxy
  assertThat(AopUtils.isJdkDynamicProxy(bean), is(true));
}

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

@Test
public void individualBeanWithSupplierAndCustomizer() {
  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  context.registerBean(BeanA.class,
      () -> new BeanA(context.getBean(BeanB.class), context.getBean(BeanC.class)),
      bd -> bd.setLazyInit(true));
  context.registerBean(BeanB.class, BeanB::new);
  context.registerBean(BeanC.class, BeanC::new);
  context.refresh();
  assertFalse(context.getBeanFactory().containsSingleton("annotationConfigApplicationContextTests.BeanA"));
  assertSame(context.getBean(BeanB.class), context.getBean(BeanA.class).b);
  assertSame(context.getBean(BeanC.class), context.getBean(BeanA.class).c);
  assertSame(context, context.getBean(BeanB.class).applicationContext);
}

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

@Test
public void testCustomWithLazyResolution() {
  AnnotationConfigApplicationContext ctx =
      new AnnotationConfigApplicationContext(CustomConfig.class, CustomPojo.class);
  assertFalse(ctx.getBeanFactory().containsSingleton("testBean1"));
  assertFalse(ctx.getBeanFactory().containsSingleton("testBean2"));
  assertTrue(BeanFactoryAnnotationUtils.isQualifierMatch(value -> value.equals("boring"),
      "testBean2", ctx.getDefaultListableBeanFactory()));
  CustomPojo pojo = ctx.getBean(CustomPojo.class);
  assertThat(pojo.testBean.getName(), equalTo("interesting"));
  TestBean testBean2 = BeanFactoryAnnotationUtils.qualifiedBeanOfType(
      ctx.getDefaultListableBeanFactory(), TestBean.class, "boring");
  assertThat(testBean2.getName(), equalTo("boring"));
}

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

@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  context.refresh();
  ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
  this.resolver = new CookieValueMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);
  this.bindingContext = new BindingContext();
  Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
  this.cookieParameter = new SynthesizingMethodParameter(method, 0);
  this.cookieStringParameter = new SynthesizingMethodParameter(method, 1);
  this.stringParameter = new SynthesizingMethodParameter(method, 2);
  this.cookieMonoParameter = new SynthesizingMethodParameter(method, 3);
}

相关文章

微信公众号

最新文章

更多

AnnotationConfigApplicationContext类方法