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

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

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

ApplicationContextAware介绍

[英]Interface to be implemented by any object that wishes to be notified of the ApplicationContext that it runs in.

Implementing this interface makes sense for example when an object requires access to a set of collaborating beans. Note that configuration via bean references is preferable to implementing this interface just for bean lookup purposes.

This interface can also be implemented if an object needs access to file resources, i.e. wants to call getResource, wants to publish an application event, or requires access to the MessageSource. However, it is preferable to implement the more specific ResourceLoaderAware, ApplicationEventPublisherAware or MessageSourceAware interface in such a specific scenario.

Note that file resource dependencies can also be exposed as bean properties of type org.springframework.core.io.Resource, populated via Strings with automatic type conversion by the bean factory. This removes the need for implementing any callback interface just for the purpose of accessing a specific file resource.

org.springframework.context.support.ApplicationObjectSupport is a convenience base class for application objects, implementing this interface.

For a list of all bean lifecycle methods, see the org.springframework.beans.factory.BeanFactory.
[中]接口,该接口将由任何希望收到其运行的ApplicationContext通知的对象实现。
例如,当一个对象需要访问一组协作bean时,实现这个接口是有意义的。请注意,通过bean引用进行配置比仅为bean查找目的实现此接口更可取。
如果一个对象需要访问文件资源,也就是说,想要调用getResource,想要发布应用程序事件,或者需要访问MessageSource,那么这个接口也可以实现。但是,最好在这种特定场景中实现更具体的ResourceLoaderware、ApplicationEventPublisherAware或MessageSourceAware接口。
注意,文件资源依赖关系也可以作为org类型的bean属性公开。springframework。果心木卫一。资源,由bean工厂通过自动类型转换的字符串填充。这样就不需要为了访问特定文件资源而实现任何回调接口。
组织。springframework。上下文支持ApplicationObjectSupport是一个方便的应用程序对象基类,实现了这个接口。
有关所有bean生命周期方法的列表,请参阅org。springframework。豆。工厂豆工厂。

代码示例

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

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  for (ViewResolver viewResolver : this.viewResolvers) {
    if (viewResolver instanceof ApplicationContextAware) {
      ((ApplicationContextAware)viewResolver).setApplicationContext(applicationContext);
    }
  }
}

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

@Override
public Object initializeBean(Object existingBean, String beanName) throws BeansException {
  if (existingBean instanceof ApplicationContextAware) {
    ((ApplicationContextAware) existingBean).setApplicationContext(StubWebApplicationContext.this);
  }
  return existingBean;
}

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

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  for (ViewResolver viewResolver : this.viewResolvers) {
    if (viewResolver instanceof ApplicationContextAware) {
      ((ApplicationContextAware)viewResolver).setApplicationContext(applicationContext);
    }
  }
}

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

@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
  if (elasticsearchConverter instanceof ApplicationContextAware) {
    ((ApplicationContextAware) elasticsearchConverter).setApplicationContext(context);
  }
}

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

@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
  if (factory instanceof ApplicationContextAware) {
    ((ApplicationContextAware) factory).setApplicationContext(context);
  }
}

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

/**
 * Add some factories to the set that will be used to load contexts and jobs.
 *
 * @param applicationContextFactory the {@link ApplicationContextFactory} values to use
 */
public void addApplicationContextFactory(ApplicationContextFactory applicationContextFactory) {
  if (applicationContextFactory instanceof ApplicationContextAware) {
    ((ApplicationContextAware) applicationContextFactory).setApplicationContext(applicationContext);
  }
  this.applicationContextFactories.add(applicationContextFactory);
}

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

@Override
  protected void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
    if (handlerExceptionResolvers == null) {
      return;
    }
    for (HandlerExceptionResolver resolver : handlerExceptionResolvers) {
      if (resolver instanceof ApplicationContextAware) {
        ApplicationContext applicationContext  = getApplicationContext();
        if (applicationContext != null) {
          ((ApplicationContextAware) resolver).setApplicationContext(applicationContext);
        }
      }
      if (resolver instanceof InitializingBean) {
        try {
          ((InitializingBean) resolver).afterPropertiesSet();
        }
        catch (Exception ex) {
          throw new IllegalStateException("Failure from afterPropertiesSet", ex);
        }
      }
      exceptionResolvers.add(resolver);
    }
  }
}

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

((ApplicationContextAware) this.jobFactory).setApplicationContext(this.applicationContext);

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

((ApplicationContextAware) this.jobFactory).setApplicationContext(this.applicationContext);

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

@Override
public void contextLoaded(ConfigurableApplicationContext context) {
  for (ApplicationListener<?> listener : this.application.getListeners()) {
    if (listener instanceof ApplicationContextAware) {
      ((ApplicationContextAware) listener).setApplicationContext(context);
    }
    context.addApplicationListener(listener);
  }
  this.initialMulticaster.multicastEvent(
      new ApplicationPreparedEvent(this.application, this.args, context));
}

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

private void invokeAwareInterfaces(Object bean) {
  if (bean instanceof Aware) {
    if (bean instanceof EnvironmentAware) {
      ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
    }
    if (bean instanceof EmbeddedValueResolverAware) {
      ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);
    }
    if (bean instanceof ResourceLoaderAware) {
      ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
    }
    if (bean instanceof ApplicationEventPublisherAware) {
      ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
    }
    if (bean instanceof MessageSourceAware) {
      ((MessageSourceAware) bean).setMessageSource(this.applicationContext);
    }
    if (bean instanceof ApplicationContextAware) {
      ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
    }
  }
}

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

private void invokeAwareInterfaces(Object bean) {
  if (bean instanceof Aware) {
    if (bean instanceof EnvironmentAware) {
      ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
    }
    if (bean instanceof EmbeddedValueResolverAware) {
      ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);
    }
    if (bean instanceof ResourceLoaderAware) {
      ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
    }
    if (bean instanceof ApplicationEventPublisherAware) {
      ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
    }
    if (bean instanceof MessageSourceAware) {
      ((MessageSourceAware) bean).setMessageSource(this.applicationContext);
    }
    if (bean instanceof ApplicationContextAware) {
      ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
    }
  }
}

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

@Test
public void postProcessWhenApplicationContextAwareThenAwareInvoked() {
  this.spring.register(Config.class).autowire();
  ApplicationContextAware toPostProcess = mock(ApplicationContextAware.class);
  this.objectObjectPostProcessor.postProcess(toPostProcess);
  verify(toPostProcess).setApplicationContext(isNotNull());
}

代码示例来源:origin: org.apache.camel/camel-spring

protected void initBeanPostProcessor(SpringCamelContext context) {
  if (beanPostProcessor != null) {
    if (beanPostProcessor instanceof ApplicationContextAware) {
      ((ApplicationContextAware) beanPostProcessor).setApplicationContext(applicationContext);
    }
    if (beanPostProcessor instanceof CamelBeanPostProcessor) {
      ((CamelBeanPostProcessor) beanPostProcessor).setCamelContext(getContext());
    }
  }
}

代码示例来源:origin: gravitee-io/gravitee-gateway

((ApplicationContextAware) resourceInstance).setApplicationContext(applicationContext);

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.spring-test

@Override
public Object initializeBean(Object existingBean, String beanName) throws BeansException {
  if (existingBean instanceof ApplicationContextAware) {
    ((ApplicationContextAware) existingBean).setApplicationContext(StubWebApplicationContext.this);
  }
  return existingBean;
}

代码示例来源:origin: org.apache.struts.xwork/xwork-core

private void injectApplicationContext(Object bean) {
  if (bean instanceof ApplicationContextAware) {
    ((ApplicationContextAware) bean).setApplicationContext(appContext);
  }
}

代码示例来源:origin: org.grails/grails-web-gsp

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  if(innerViewResolver instanceof ApplicationContextAware) {
    ((ApplicationContextAware)innerViewResolver).setApplicationContext(applicationContext);
  }
}

代码示例来源:origin: org.qi4j.library/org.qi4j.library.spring

@Override
  public void setApplicationContext( final ApplicationContext applicationContext ) throws BeansException
  {
    if ( this.applicationBootstrap instanceof ApplicationContextAware )
    {
      // propagate application context to the application bootstrap
      ApplicationContextAware aware = (ApplicationContextAware) this.applicationBootstrap;
      aware.setApplicationContext( applicationContext );
    }
  }
}

代码示例来源:origin: apache/servicemix-bundles

@Override
public Object initializeBean(Object existingBean, String beanName) throws BeansException {
  if (existingBean instanceof ApplicationContextAware) {
    ((ApplicationContextAware) existingBean).setApplicationContext(StubWebApplicationContext.this);
  }
  return existingBean;
}

相关文章

微信公众号

最新文章

更多

ApplicationContextAware类方法