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

x33g5p2x  于2022-01-18 转载在 其他  
字(11.5k)|赞(0)|评价(0)|浏览(161)

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

ConfigurableApplicationContext.getBeanFactory介绍

[英]Return the internal bean factory of this application context. Can be used to access specific functionality of the underlying factory.

Note: Do not use this to post-process the bean factory; singletons will already have been instantiated before. Use a BeanFactoryPostProcessor to intercept the BeanFactory setup process before beans get touched.

Generally, this internal factory will only be accessible while the context is active, that is, in-between #refresh() and #close(). The #isActive() flag can be used to check whether the context is in an appropriate state.
[中]返回此应用程序上下文的内部bean工厂。可用于访问基础工厂的特定功能。
注意:不要用这个来后处理豆子工厂;单例之前已经实例化过。在接触bean之前,使用BeanFactory后处理器拦截BeanFactory设置过程。
通常,只有在上下文处于活动状态(即介于#refresh()和#close()之间)时,才能访问此内部工厂。#isActive()标志可用于检查上下文是否处于适当的状态。

代码示例

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

@Nullable
private ConfigurableListableBeanFactory deduceBeanFactory(@Nullable BeanDefinitionRegistry source) {
  if (source instanceof ConfigurableListableBeanFactory) {
    return (ConfigurableListableBeanFactory) source;
  }
  if (source instanceof ConfigurableApplicationContext) {
    return (((ConfigurableApplicationContext) source).getBeanFactory());
  }
  return null;
}

代码示例来源:origin: org.springframework/spring-context

@Nullable
private ConfigurableListableBeanFactory deduceBeanFactory(@Nullable BeanDefinitionRegistry source) {
  if (source instanceof ConfigurableListableBeanFactory) {
    return (ConfigurableListableBeanFactory) source;
  }
  if (source instanceof ConfigurableApplicationContext) {
    return (((ConfigurableApplicationContext) source).getBeanFactory());
  }
  return null;
}

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

/**
 * Create a new ApplicationContextAwareProcessor for the given context.
 */
public ApplicationContextAwareProcessor(ConfigurableApplicationContext applicationContext) {
  this.applicationContext = applicationContext;
  this.embeddedValueResolver = new EmbeddedValueResolver(applicationContext.getBeanFactory());
}

代码示例来源:origin: org.springframework/spring-context

/**
 * Create a new ApplicationContextAwareProcessor for the given context.
 */
public ApplicationContextAwareProcessor(ConfigurableApplicationContext applicationContext) {
  this.applicationContext = applicationContext;
  this.embeddedValueResolver = new EmbeddedValueResolver(applicationContext.getBeanFactory());
}

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

@Override
  protected List<ContextCustomizerFactory> getContextCustomizerFactories() {
    return singletonList(
      (ContextCustomizerFactory) (testClass, configAttributes) -> (ContextCustomizer) (context,
          mergedConfig) -> context.getBeanFactory().registerSingleton("foo", "foo"));
  }
}

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

@Override
  protected List<ContextCustomizerFactory> getContextCustomizerFactories() {
    return singletonList(
      (ContextCustomizerFactory) (testClass, configAttributes) ->
        (ContextCustomizer) (context, mergedConfig) -> context.getBeanFactory().registerSingleton("foo", "foo")
    );
  }
}

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

/**
 * Called to initialize the encoder/decoder.
 * @see javax.websocket.Encoder#init(EndpointConfig)
 * @see javax.websocket.Decoder#init(EndpointConfig)
 */
public void init(EndpointConfig config) {
  ApplicationContext applicationContext = getApplicationContext();
  if (applicationContext != null && applicationContext instanceof ConfigurableApplicationContext) {
    ConfigurableListableBeanFactory beanFactory =
        ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
    beanFactory.autowireBean(this);
  }
}

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

/**
 * Return the internal bean factory of the parent context if it implements
 * ConfigurableApplicationContext; else, return the parent context itself.
 * @see org.springframework.context.ConfigurableApplicationContext#getBeanFactory
 */
@Nullable
protected BeanFactory getInternalParentBeanFactory() {
  return (getParent() instanceof ConfigurableApplicationContext ?
      ((ConfigurableApplicationContext) getParent()).getBeanFactory() : getParent());
}

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

@Override
@Nullable
public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
  AccessControlContext acc = null;
  if (System.getSecurityManager() != null &&
      (bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
          bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
          bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) {
    acc = this.applicationContext.getBeanFactory().getAccessControlContext();
  }
  if (acc != null) {
    AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
      invokeAwareInterfaces(bean);
      return null;
    }, acc);
  }
  else {
    invokeAwareInterfaces(bean);
  }
  return bean;
}

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

protected List<HandlerMethodArgumentResolver> initArgumentResolvers() {
  ApplicationContext context = getApplicationContext();
  ConfigurableBeanFactory beanFactory = (context instanceof ConfigurableApplicationContext ?
      ((ConfigurableApplicationContext) context).getBeanFactory() : null);
  List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
  // Annotation-based argument resolution
  resolvers.add(new HeaderMethodArgumentResolver(this.conversionService, beanFactory));
  resolvers.add(new HeadersMethodArgumentResolver());
  resolvers.add(new DestinationVariableMethodArgumentResolver(this.conversionService));
  // Type-based argument resolution
  resolvers.add(new PrincipalMethodArgumentResolver());
  resolvers.add(new MessageMethodArgumentResolver(this.messageConverter));
  resolvers.addAll(getCustomArgumentResolvers());
  resolvers.add(new PayloadArgumentResolver(this.messageConverter, this.validator));
  return resolvers;
}

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

@Autowired
public void initializeApp(ConfigurableApplicationContext applicationContext) {
  applicationContext.getBeanFactory().registerSingleton(
      "qualifiedTransactionManager", new CallCountingTransactionManager());
}

代码示例来源:origin: org.springframework/spring-context

/**
 * Return the internal bean factory of the parent context if it implements
 * ConfigurableApplicationContext; else, return the parent context itself.
 * @see org.springframework.context.ConfigurableApplicationContext#getBeanFactory
 */
@Nullable
protected BeanFactory getInternalParentBeanFactory() {
  return (getParent() instanceof ConfigurableApplicationContext ?
      ((ConfigurableApplicationContext) getParent()).getBeanFactory() : getParent());
}

代码示例来源:origin: ltsopensource/light-task-scheduler

private void registerRunnerBeanDefinition() {
  DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory)
      ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
  if (!beanFactory.containsBean(JOB_RUNNER_BEAN_NAME)) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(JobDispatcher.class);
    builder.setScope(BeanDefinition.SCOPE_SINGLETON);
    builder.setLazyInit(false);
    builder.getBeanDefinition().getPropertyValues().addPropertyValue("shardField", properties.getDispatchRunner().getShardValue());
    beanFactory.registerBeanDefinition(JOB_RUNNER_BEAN_NAME, builder.getBeanDefinition());
  }
}

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

@Test
  public void test() {
    ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfig.class);

    assertThat("someDependency was not post processed",
        ctx.getBeanFactory().getBeanDefinition("someDependency").getDescription(),
        equalTo("post processed by MyPostProcessor"));

    MyConfig config = ctx.getBean(MyConfig.class);
    assertTrue("Config class was not enhanced", ClassUtils.isCglibProxy(config));
  }
}

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

@SuppressWarnings("resource")
ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) wac;
ConfigurableListableBeanFactory bf = configurableApplicationContext.getBeanFactory();
bf.registerResolvableDependency(MockHttpServletResponse.class, response);
bf.registerResolvableDependency(ServletWebRequest.class, servletWebRequest);

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

@Test
public void getHandlerWithBeanFactory() {
  @SuppressWarnings("resource")
  ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
  BeanCreatingHandlerProvider<EchoHandler> provider =
      new BeanCreatingHandlerProvider<>(EchoHandler.class);
  provider.setBeanFactory(context.getBeanFactory());
  assertNotNull(provider.getHandler());
}

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

@Test
public void endpointPerConnection() throws Exception {
  @SuppressWarnings("resource")
  ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
  ServerEndpointRegistration registration = new ServerEndpointRegistration("/path", EchoEndpoint.class);
  registration.setBeanFactory(context.getBeanFactory());
  EchoEndpoint endpoint = registration.getConfigurator().getEndpointInstance(EchoEndpoint.class);
  assertNotNull(endpoint);
}

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

@Test
public void testLazyInit() throws Exception {
  // start the MBeanExporter
  ConfigurableApplicationContext ctx = loadContext("org/springframework/jmx/export/notificationPublisherLazyTests.xml");
  assertFalse("Should not have instantiated the bean yet", ctx.getBeanFactory().containsSingleton("publisher"));
  // need to touch the MBean proxy
  server.getAttribute(ObjectNameManager.getInstance("spring:type=Publisher"), "Name");
  this.server.addNotificationListener(ObjectNameManager.getInstance("spring:type=Publisher"), listener, null,
      null);
  MyNotificationPublisher publisher = (MyNotificationPublisher) ctx.getBean("publisher");
  assertNotNull("NotificationPublisher should not be null", publisher.getNotificationPublisher());
  publisher.sendNotification();
  assertEquals("Notification not sent", 1, listener.count);
}

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

@Test
public void getAuthenticationManagerBeanWhenMultipleDefinedAndOnePrimaryThenNoException() throws Exception {
  this.spring.register(MultipleAuthenticationManagerBeanConfig.class).autowire();
  this.spring.getContext().getBeanFactory().getBean(AuthenticationConfiguration.class).getAuthenticationManager();
}

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

@Test
public void eventListenerWorksWithCustomScope() throws Exception {
  load(CustomScopeTestBean.class);
  CustomScope customScope = new CustomScope();
  this.context.getBeanFactory().registerScope("custom", customScope);
  CustomScopeTestBean proxy = this.context.getBean(CustomScopeTestBean.class);
  assertTrue("bean should be a cglib proxy", AopUtils.isCglibProxy(proxy));
  this.eventCollector.assertNoEventReceived(proxy.getId());
  this.context.publishEvent(new ContextRefreshedEvent(this.context));
  this.eventCollector.assertNoEventReceived(proxy.getId());
  customScope.active = false;
  this.context.publishEvent(new ContextRefreshedEvent(this.context));
  customScope.active = true;
  this.eventCollector.assertNoEventReceived(proxy.getId());
  TestEvent event = new TestEvent();
  this.context.publishEvent(event);
  this.eventCollector.assertEvent(proxy.getId(), event);
  this.eventCollector.assertTotalEventsCount(1);
  try {
    customScope.active = false;
    this.context.publishEvent(new TestEvent());
    fail("Should have thrown IllegalStateException");
  }
  catch (BeanCreationException ex) {
    // expected
    assertTrue(ex.getCause() instanceof IllegalStateException);
  }
}

相关文章

微信公众号

最新文章

更多

ConfigurableApplicationContext类方法