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

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

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

ApplicationContext.getAutowireCapableBeanFactory介绍

[英]Expose AutowireCapableBeanFactory functionality for this context.

This is not typically used by application code, except for the purpose of initializing bean instances that live outside of the application context, applying the Spring bean lifecycle (fully or partly) to them.

Alternatively, the internal BeanFactory exposed by the ConfigurableApplicationContext interface offers access to the AutowireCapableBeanFactory interface too. The present method mainly serves as a convenient, specific facility on the ApplicationContext interface.

NOTE: As of 4.2, this method will consistently throw IllegalStateException after the application context has been closed. In current Spring Framework versions, only refreshable application contexts behave that way; as of 4.2, all application context implementations will be required to comply.
[中]为此上下文公开AutowireCapableBeanFactory功能。
这通常不被应用程序代码使用,除非是为了初始化位于应用程序上下文之外的bean实例,对它们应用springbean生命周期(全部或部分)。
或者,ConfigurableApplicationContext接口公开的内部BeanFactory也提供对AutowireCapableBeanFactory接口的访问。本方法主要用作ApplicationContext接口上的一种方便、特定的工具。
注意:从4.2开始,此方法将在应用程序上下文关闭后始终抛出IllegalStateException。在当前的Spring框架版本中,只有可刷新的应用程序上下文才会这样做;从4.2开始,所有应用程序上下文实现都需要遵守。

代码示例

代码示例来源:origin: knightliao/disconf

@Override
public ScanStaticModel scan(List<String> packNameList) {
  factory = (DefaultListableBeanFactory) context.getAutowireCapableBeanFactory();
  return null;
}

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

/**
 * Create a default strategy.
 * <p>The default implementation uses
 * {@link org.springframework.beans.factory.config.AutowireCapableBeanFactory#createBean}.
 * @param context the current WebApplicationContext
 * @param clazz the strategy implementation class to instantiate
 * @return the fully configured strategy instance
 * @see org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory()
 * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#createBean
 */
protected Object createDefaultStrategy(ApplicationContext context, Class<?> clazz) {
  return context.getAutowireCapableBeanFactory().createBean(clazz);
}

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

private static List<HandlerMapping> initFallback(ApplicationContext applicationContext) {
  Properties props;
  String path = "DispatcherServlet.properties";
  try {
    Resource resource = new ClassPathResource(path, DispatcherServlet.class);
    props = PropertiesLoaderUtils.loadProperties(resource);
  }
  catch (IOException ex) {
    throw new IllegalStateException("Could not load '" + path + "': " + ex.getMessage());
  }
  String value = props.getProperty(HandlerMapping.class.getName());
  String[] names = StringUtils.commaDelimitedListToStringArray(value);
  List<HandlerMapping> result = new ArrayList<>(names.length);
  for (String name : names) {
    try {
      Class<?> clazz = ClassUtils.forName(name, DispatcherServlet.class.getClassLoader());
      Object mapping = applicationContext.getAutowireCapableBeanFactory().createBean(clazz);
      result.add((HandlerMapping) mapping);
    }
    catch (ClassNotFoundException ex) {
      throw new IllegalStateException("Could not find default HandlerMapping [" + name + "]");
    }
  }
  return result;
}

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

BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();

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

/**
 * Apply the containing {@link ApplicationContext}'s lifecycle methods
 * to the given {@link View} instance, if such a context is available.
 * @param viewName the name of the view
 * @param view the freshly created View instance, pre-configured with
 * {@link AbstractUrlBasedView}'s properties
 * @return the {@link View} instance to use (either the original one
 * or a decorated variant)
 * @since 5.0
 * @see #getApplicationContext()
 * @see ApplicationContext#getAutowireCapableBeanFactory()
 * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#initializeBean
 */
protected View applyLifecycleMethods(String viewName, AbstractUrlBasedView view) {
  ApplicationContext context = getApplicationContext();
  if (context != null) {
    Object initialized = context.getAutowireCapableBeanFactory().initializeBean(view, viewName);
    if (initialized instanceof View) {
      return (View) initialized;
    }
  }
  return view;
}

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

/**
 * Apply the containing {@link ApplicationContext}'s lifecycle methods
 * to the given {@link View} instance, if such a context is available.
 * @param viewName the name of the view
 * @param view the freshly created View instance, pre-configured with
 * {@link AbstractUrlBasedView}'s properties
 * @return the {@link View} instance to use (either the original one
 * or a decorated variant)
 * @see #getApplicationContext()
 * @see ApplicationContext#getAutowireCapableBeanFactory()
 * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#initializeBean
 */
protected View applyLifecycleMethods(String viewName, AbstractUrlBasedView view) {
  ApplicationContext context = getApplicationContext();
  if (context != null) {
    Object initialized = context.getAutowireCapableBeanFactory().initializeBean(view, viewName);
    if (initialized instanceof View) {
      return (View) initialized;
    }
  }
  return view;
}

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

/**
 * Create a HandlerMethod instance from an Object handler that is either a handler
 * instance or a String-based bean name.
 */
protected HandlerMethod createHandlerMethod(Object handler, Method method) {
  HandlerMethod handlerMethod;
  if (handler instanceof String) {
    ApplicationContext context = getApplicationContext();
    Assert.state(context != null, "ApplicationContext is required for resolving handler bean names");
    String beanName = (String) handler;
    handlerMethod = new HandlerMethod(beanName, context.getAutowireCapableBeanFactory(), method);
  }
  else {
    handlerMethod = new HandlerMethod(handler, method);
  }
  return handlerMethod;
}

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

@Override
protected void initServletContext(ServletContext servletContext) {
  Collection<ViewResolver> matchingBeans =
      BeanFactoryUtils.beansOfTypeIncludingAncestors(obtainApplicationContext(), ViewResolver.class).values();
  if (this.viewResolvers == null) {
    this.viewResolvers = new ArrayList<>(matchingBeans.size());
    for (ViewResolver viewResolver : matchingBeans) {
      if (this != viewResolver) {
        this.viewResolvers.add(viewResolver);
      }
    }
  }
  else {
    for (int i = 0; i < this.viewResolvers.size(); i++) {
      ViewResolver vr = this.viewResolvers.get(i);
      if (matchingBeans.contains(vr)) {
        continue;
      }
      String name = vr.getClass().getName() + i;
      obtainApplicationContext().getAutowireCapableBeanFactory().initializeBean(vr, name);
    }
  }
  AnnotationAwareOrderComparator.sort(this.viewResolvers);
  this.cnmFactoryBean.setServletContext(servletContext);
}

代码示例来源:origin: gocd/gocd

VersionInfoService getVersionInfoService() {
  return this.getApplicationContext().getAutowireCapableBeanFactory().getBean(VersionInfoService.class);
}

代码示例来源:origin: gocd/gocd

MaintenanceModeService getMaintenanceModeService() {
  return this.getApplicationContext().getAutowireCapableBeanFactory().getBean(MaintenanceModeService.class);
}

代码示例来源:origin: gocd/gocd

WebpackAssetsService webpackAssetsService() {
  return this.getApplicationContext().getAutowireCapableBeanFactory().getBean(WebpackAssetsService.class);
}

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

/**
 * Create the HandlerMethod instance.
 * @param handler either a bean name or an actual handler instance
 * @param method the target method
 * @return the created HandlerMethod
 */
protected HandlerMethod createHandlerMethod(Object handler, Method method) {
  HandlerMethod handlerMethod;
  if (handler instanceof String) {
    String beanName = (String) handler;
    handlerMethod = new HandlerMethod(beanName,
        obtainApplicationContext().getAutowireCapableBeanFactory(), method);
  }
  else {
    handlerMethod = new HandlerMethod(handler, method);
  }
  return handlerMethod;
}

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

/**
 * Create the HandlerMethod instance.
 * @param handler either a bean name or an actual handler instance
 * @param method the target method
 * @return the created HandlerMethod
 */
protected HandlerMethod createHandlerMethod(Object handler, Method method) {
  HandlerMethod handlerMethod;
  if (handler instanceof String) {
    String beanName = (String) handler;
    handlerMethod = new HandlerMethod(beanName,
        obtainApplicationContext().getAutowireCapableBeanFactory(), method);
  }
  else {
    handlerMethod = new HandlerMethod(handler, method);
  }
  return handlerMethod;
}

代码示例来源:origin: PipelineAI/pipeline

@Override
protected <T> T createProxy(Class<T> t) {
  AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
  return beanFactory.createBean(t);
}

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

/**
 * Performs dependency injection and bean initialization for the supplied
 * {@link TestContext} as described in
 * {@link #prepareTestInstance(TestContext) prepareTestInstance()}.
 * <p>The {@link #REINJECT_DEPENDENCIES_ATTRIBUTE} will be subsequently removed
 * from the test context, regardless of its value.
 * @param testContext the test context for which dependency injection should
 * be performed (never {@code null})
 * @throws Exception allows any exception to propagate
 * @see #prepareTestInstance(TestContext)
 * @see #beforeTestMethod(TestContext)
 */
protected void injectDependencies(TestContext testContext) throws Exception {
  Object bean = testContext.getTestInstance();
  Class<?> clazz = testContext.getTestClass();
  AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory();
  beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
  beanFactory.initializeBean(bean, clazz.getName() + AutowireCapableBeanFactory.ORIGINAL_INSTANCE_SUFFIX);
  testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
}

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

protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
  Object job = (this.applicationContext != null ?
      this.applicationContext.getAutowireCapableBeanFactory().createBean(
          bundle.getJobDetail().getJobClass(), AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false) :
      super.createJobInstance(bundle));

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

@Test
public void testAspectAppliedForInitializeBeanWithEmptyName() {
  ITestBean bean = (ITestBean) this.context.getAutowireCapableBeanFactory().initializeBean(new TestBean(), "");
  CountingAspectJAdvice advice = (CountingAspectJAdvice) this.context.getBean("countingAdvice");
  assertEquals("Incorrect before count", 0, advice.getBeforeCount());
  assertEquals("Incorrect after count", 0, advice.getAfterCount());
  bean.setName("Sally");
  assertEquals("Incorrect before count", 1, advice.getBeforeCount());
  assertEquals("Incorrect after count", 1, advice.getAfterCount());
  bean.getName();
  assertEquals("Incorrect before count", 1, advice.getBeforeCount());
  assertEquals("Incorrect after count", 1, advice.getAfterCount());
}

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

@Test
public void testAspectAppliedForInitializeBeanWithNullName() {
  ITestBean bean = (ITestBean) this.context.getAutowireCapableBeanFactory().initializeBean(new TestBean(), null);
  CountingAspectJAdvice advice = (CountingAspectJAdvice) this.context.getBean("countingAdvice");
  assertEquals("Incorrect before count", 0, advice.getBeforeCount());
  assertEquals("Incorrect after count", 0, advice.getAfterCount());
  bean.setName("Sally");
  assertEquals("Incorrect before count", 1, advice.getBeforeCount());
  assertEquals("Incorrect after count", 1, advice.getAfterCount());
  bean.getName();
  assertEquals("Incorrect before count", 1, advice.getBeforeCount());
  assertEquals("Incorrect after count", 1, advice.getAfterCount());
}

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

@Test
public void missingDataSourceAndTxMgr() throws Exception {
  ApplicationContext ctx = mock(ApplicationContext.class);
  given(ctx.getResource(anyString())).willReturn(mock(Resource.class));
  given(ctx.getAutowireCapableBeanFactory()).willReturn(mock(AutowireCapableBeanFactory.class));
  Class<?> clazz = MissingDataSourceAndTxMgr.class;
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo"));
  given(testContext.getApplicationContext()).willReturn(ctx);
  assertExceptionContains("supply at least a DataSource or PlatformTransactionManager");
}

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

@Test
public void isolatedTxModeDeclaredWithoutTxMgr() throws Exception {
  ApplicationContext ctx = mock(ApplicationContext.class);
  given(ctx.getResource(anyString())).willReturn(mock(Resource.class));
  given(ctx.getAutowireCapableBeanFactory()).willReturn(mock(AutowireCapableBeanFactory.class));
  Class<?> clazz = IsolatedWithoutTxMgr.class;
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo"));
  given(testContext.getApplicationContext()).willReturn(ctx);
  assertExceptionContains("cannot execute SQL scripts using Transaction Mode [ISOLATED] without a PlatformTransactionManager");
}

相关文章

微信公众号

最新文章

更多