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

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

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

ConfigurableApplicationContext.refresh介绍

[英]Load or refresh the persistent representation of the configuration, which might an XML file, properties file, or relational database schema.

As this is a startup method, it should destroy already created singletons if it fails, to avoid dangling resources. In other words, after invocation of that method, either all or no singletons at all should be instantiated.
[中]加载或刷新配置的持久表示形式,可能是XML文件、属性文件或关系数据库架构。
由于这是一个启动方法,如果失败,它应该销毁已经创建的单例,以避免悬空资源。换句话说,在调用该方法之后,要么全部实例化,要么根本不实例化。

代码示例

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

@Override
public void onReload(Container container) {
  if (ctx instanceof ConfigurableApplicationContext) {
    ((ConfigurableApplicationContext) ctx).refresh();
  }
}

代码示例来源:origin: Dreampie/Resty

public static void refreshContext() {
 if (SpringHolder.alive) {
  SpringBuilder.context.refresh();
 }
}

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

/**
 * Refresh the given application context, if necessary.
 */
protected void refreshApplicationContext(ApplicationContext context) {
  if (context instanceof ConfigurableApplicationContext) {
    ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context;
    if (!cac.isActive()) {
      cac.refresh();
    }
  }
}

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

/**
 * Refresh this servlet's application context, as well as the
 * dependent state of the servlet.
 * @see #getWebApplicationContext()
 * @see org.springframework.context.ConfigurableApplicationContext#refresh()
 */
public void refresh() {
  WebApplicationContext wac = getWebApplicationContext();
  if (!(wac instanceof ConfigurableApplicationContext)) {
    throw new IllegalStateException("WebApplicationContext does not support refresh: " + wac);
  }
  ((ConfigurableApplicationContext) wac).refresh();
}

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

/**
 * Refresh the given application context, if necessary.
 */
protected void refreshApplicationContext(ApplicationContext context) {
  if (context instanceof ConfigurableApplicationContext) {
    ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context;
    if (!cac.isActive()) {
      cac.refresh();
    }
  }
}

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

/**
 * Refresh this servlet's application context, as well as the
 * dependent state of the servlet.
 * @see #getWebApplicationContext()
 * @see org.springframework.context.ConfigurableApplicationContext#refresh()
 */
public void refresh() {
  WebApplicationContext wac = getWebApplicationContext();
  if (!(wac instanceof ConfigurableApplicationContext)) {
    throw new IllegalStateException("WebApplicationContext does not support refresh: " + wac);
  }
  ((ConfigurableApplicationContext) wac).refresh();
}

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

private synchronized MessageChannel createNewCustomerChannel(String customer) {
  MessageChannel channel = this.channels.get(customer);
  if (channel == null) {
    ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(
        new String[] { "/META-INF/spring/integration/dynamic-ftp-outbound-adapter-context.xml" },
        false);
    this.setEnvironmentForCustomer(ctx, customer);
    ctx.refresh();
    channel = ctx.getBean("toFtpChannel", MessageChannel.class);
    this.channels.put(customer, channel);
    //Will works as the same reference is presented always
    this.contexts.put(channel, ctx);
  }
  return channel;
}

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

if (!cac.isActive()) {
  cac.refresh();

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

private void assertFactoryCountThroughoutLifecycle(ConfigurableApplicationContext ctx) throws Exception {
  assertThat(serializableFactoryCount(), equalTo(0));
  try {
    ctx.refresh();
    assertThat(serializableFactoryCount(), equalTo(1));
    ctx.close();
  }
  catch (BeanCreationException ex) {
    // ignore - this is expected on refresh() for failure case tests
  }
  finally {
    assertThat(serializableFactoryCount(), equalTo(0));
  }
}

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

@Test
public void abstractApplicationContextValidatesRequiredPropertiesOnRefresh() {
  {
    ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.refresh();
  }
  {
    ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.getEnvironment().setRequiredProperties("foo", "bar");
    try {
      ctx.refresh();
      fail("expected missing property exception");
    }
    catch (MissingRequiredPropertiesException ex) {
    }
  }
  {
    ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.getEnvironment().setRequiredProperties("foo");
    ctx.setEnvironment(new MockEnvironment().withProperty("foo", "fooValue"));
    ctx.refresh(); // should succeed
  }
}

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

if (!cac.isActive()) {
  cac.refresh();

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

@Test
public void classPathXmlApplicationContext() {
  ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(XML_PATH);
  ctx.setEnvironment(prodEnv);
  ctx.refresh();
  assertEnvironmentBeanRegistered(ctx);
  assertHasEnvironment(ctx, prodEnv);
  assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment());
  assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false));
  assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true));
}

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

@Test
public void fileSystemXmlApplicationContext() throws IOException {
  ClassPathResource xml = new ClassPathResource(XML_PATH);
  File tmpFile = File.createTempFile("test", "xml");
  FileCopyUtils.copy(xml.getFile(), tmpFile);
  // strange - FSXAC strips leading '/' unless prefixed with 'file:'
  ConfigurableApplicationContext ctx =
      new FileSystemXmlApplicationContext(new String[] {"file:" + tmpFile.getPath()}, false);
  ctx.setEnvironment(prodEnv);
  ctx.refresh();
  assertEnvironmentBeanRegistered(ctx);
  assertHasEnvironment(ctx, prodEnv);
  assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment());
  assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false));
  assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true));
}

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

@Test
public void genericApplicationContext_standardEnv() {
  ConfigurableApplicationContext ctx = new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean());
  ctx.refresh();
  assertHasStandardEnvironment(ctx);
  assertEnvironmentBeanRegistered(ctx);
  assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment());
}

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

@Test
public void repro() {
  ConfigurableApplicationContext parent = new GenericApplicationContext();
  parent.refresh();
  AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
  child.setParent(parent);
  child.refresh();
  ConfigurableEnvironment env = child.getBean(ConfigurableEnvironment.class);
  assertThat("unknown env", env, anyOf(
      sameInstance(parent.getEnvironment()),
      sameInstance(child.getEnvironment())));
  assertThat("expected child ctx env", env, sameInstance(child.getEnvironment()));
  child.close();
  parent.close();
}

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

@Test
public void componentScanRespectsProfileAnnotation() {
  String xmlLocation = "org/springframework/context/annotation/componentScanRespectsProfileAnnotationTests.xml";
  { // should exclude the profile-annotated bean if active profiles remains unset
    GenericXmlApplicationContext context = new GenericXmlApplicationContext();
    context.load(xmlLocation);
    context.refresh();
    assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(false));
    context.close();
  }
  { // should include the profile-annotated bean with active profiles set
    GenericXmlApplicationContext context = new GenericXmlApplicationContext();
    context.getEnvironment().setActiveProfiles(ProfileAnnotatedComponent.PROFILE_NAME);
    context.load(xmlLocation);
    context.refresh();
    assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(true));
    context.close();
  }
  { // ensure the same works for AbstractRefreshableApplicationContext impls too
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] { xmlLocation },
      false);
    context.getEnvironment().setActiveProfiles(ProfileAnnotatedComponent.PROFILE_NAME);
    context.refresh();
    assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(true));
    context.close();
  }
}

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

@Test
public void dispatcherServletContextRefresh() throws ServletException {
  MockServletContext servletContext = new MockServletContext("org/springframework/web/context");
  DispatcherServlet servlet = new DispatcherServlet();
  servlet.init(new MockServletConfig(servletContext, "empty"));
  ServletContextAwareBean contextBean =
      (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean");
  ServletConfigAwareBean configBean =
      (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean");
  assertSame(servletContext, contextBean.getServletContext());
  assertSame(servlet.getServletConfig(), configBean.getServletConfig());
  MultipartResolver multipartResolver = servlet.getMultipartResolver();
  assertNotNull(multipartResolver);
  ((ConfigurableApplicationContext) servlet.getWebApplicationContext()).refresh();
  ServletContextAwareBean contextBean2 =
      (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean");
  ServletConfigAwareBean configBean2 =
      (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean");
  assertSame(servletContext, contextBean2.getServletContext());
  assertSame(servlet.getServletConfig(), configBean2.getServletConfig());
  assertTrue(contextBean != contextBean2);
  assertTrue(configBean != configBean2);
  MultipartResolver multipartResolver2 = servlet.getMultipartResolver();
  assertTrue(multipartResolver != multipartResolver2);
  servlet.destroy();
}

代码示例来源:origin: mulesoft/mule

@Override
protected void doInitialise() throws InitialisationException {
 ((AbstractApplicationContext) applicationContext)
   .addBeanFactoryPostProcessor(createBeforeInitialisationRegisteredObjectsPostProcessor());
 // This is used to track the Spring context lifecycle since there is no way to confirm the lifecycle phase from the
 // application context
 springContextInitialised.set(true);
 if (!readOnly) {
  ((ConfigurableApplicationContext) applicationContext).refresh();
 }
}

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

@BeforeClass
public static void start() {
  applicationContext.refresh();
}

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

assertThat(eventMessage.getPayload(), instanceOf(StompConnectionFailedEvent.class));
this.serverContext.refresh();

相关文章

微信公众号

最新文章

更多

ConfigurableApplicationContext类方法