gpt4 book ai didi

org.springframework.web.context.support.WebApplicationObjectSupport类的使用及代码示例

转载 作者:知者 更新时间:2024-03-26 07:11:05 26 4
gpt4 key购买 nike

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

WebApplicationObjectSupport介绍

[英]Convenient superclass for application objects running in a WebApplicationContext. Provides getWebApplicationContext(), getServletContext(), and getTempDir() accessors.

Note: It is generally recommended to use individual callback interfaces for the actual callbacks needed. This broad base class is primarily intended for use within the framework, in case of ServletContext access etc typically being needed.
[中]为在WebApplicationContext中运行的应用程序对象提供方便的超类。提供getWebApplicationContext()、getServletContext()和getTempDir()访问器。
注意:对于实际需要的回调,通常建议使用单独的回调接口。这个广泛的基类主要用于框架内,以防通常需要ServletContext访问等。

代码示例

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

/**
 * Return the temporary directory for the current web application,
 * as provided by the servlet container.
 * @return the File representing the temporary directory
 * @throws IllegalStateException if not running within a ServletContext
 * @see org.springframework.web.util.WebUtils#getTempDir(javax.servlet.ServletContext)
 */
protected final File getTempDir() throws IllegalStateException {
  ServletContext servletContext = getServletContext();
  Assert.state(servletContext != null, "ServletContext is required");
  return WebUtils.getTempDir(servletContext);
}

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

/**
 * Return the current ServletContext.
 * @throws IllegalStateException if not running within a required ServletContext
 * @see #isContextRequired()
 */
@Nullable
protected final ServletContext getServletContext() throws IllegalStateException {
  if (this.servletContext != null) {
    return this.servletContext;
  }
  ServletContext servletContext = null;
  WebApplicationContext wac = getWebApplicationContext();
  if (wac != null) {
    servletContext = wac.getServletContext();
  }
  if (servletContext == null && isContextRequired()) {
    throw new IllegalStateException("WebApplicationObjectSupport instance [" + this +
        "] does not run within a ServletContext. Make sure the object is fully configured!");
  }
  return servletContext;
}

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

/**
 * Return the current application context as WebApplicationContext.
 * <p><b>NOTE:</b> Only use this if you actually need to access
 * WebApplicationContext-specific functionality. Preferably use
 * {@code getApplicationContext()} or {@code getServletContext()}
 * else, to be able to run in non-WebApplicationContext environments as well.
 * @throws IllegalStateException if not running in a WebApplicationContext
 * @see #getApplicationContext()
 */
@Nullable
protected final WebApplicationContext getWebApplicationContext() throws IllegalStateException {
  ApplicationContext ctx = getApplicationContext();
  if (ctx instanceof WebApplicationContext) {
    return (WebApplicationContext) getApplicationContext();
  }
  else if (isContextRequired()) {
    throw new IllegalStateException("WebApplicationObjectSupport instance [" + this +
        "] does not run in a WebApplicationContext but in: " + ctx);
  }
  else {
    return null;
  }
}

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

@Override
public final void setServletContext(ServletContext servletContext) {
  if (servletContext != this.servletContext) {
    this.servletContext = servletContext;
    initServletContext(servletContext);
  }
}

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

@Test
@SuppressWarnings("resource")
public void testWebApplicationObjectSupport() {
  StaticWebApplicationContext wac = new StaticWebApplicationContext();
  wac.setServletContext(new MockServletContext());
  File tempDir = new File("");
  wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, tempDir);
  wac.registerBeanDefinition("test", new RootBeanDefinition(TestWebApplicationObject.class));
  wac.refresh();
  WebApplicationObjectSupport wao = (WebApplicationObjectSupport) wac.getBean("test");
  assertEquals(wao.getServletContext(), wac.getServletContext());
  assertEquals(wao.getTempDir(), tempDir);
}

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

@Test
@SuppressWarnings("resource")
public void testWebApplicationObjectSupportWithWrongContext() {
  StaticApplicationContext ac = new StaticApplicationContext();
  ac.registerBeanDefinition("test", new RootBeanDefinition(TestWebApplicationObject.class));
  WebApplicationObjectSupport wao = (WebApplicationObjectSupport) ac.getBean("test");
  try {
    wao.getWebApplicationContext();
    fail("Should have thrown IllegalStateException");
  }
  catch (IllegalStateException ex) {
    // expected
  }
}

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

private List<ViewResolver> initViewResolvers(WebApplicationContext wac) {
  this.viewResolvers = (this.viewResolvers != null ? this.viewResolvers :
      Collections.singletonList(new InternalResourceViewResolver()));
  for (Object viewResolver : this.viewResolvers) {
    if (viewResolver instanceof WebApplicationObjectSupport) {
      ((WebApplicationObjectSupport) viewResolver).setApplicationContext(wac);
    }
  }
  return this.viewResolvers;
}

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

/**
 * Return the current application context as WebApplicationContext.
 * @throws IllegalStateException if not running in a WebApplicationContext
 */
protected final WebApplicationContext getWebApplicationContext() throws IllegalStateException {
  ApplicationContext ctx = getApplicationContext();
  if (!(ctx instanceof WebApplicationContext)) {
    throw new IllegalStateException(
        "WebApplicationObjectSupport instance [" + this +
        "] does not run in a WebApplicationContext but in: " + ctx);
  }
  return (WebApplicationContext) getApplicationContext();
}

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

@Override
public final void setServletContext(ServletContext servletContext) {
  if (servletContext != this.servletContext) {
    this.servletContext = servletContext;
    initServletContext(servletContext);
  }
}

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

/**
 * Return the current ServletContext.
 * @throws IllegalStateException if not running in a WebApplicationContext
 */
protected final ServletContext getServletContext() {
  return getWebApplicationContext().getServletContext();
}

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

private List<ViewResolver> initViewResolvers(WebApplicationContext wac) {
  this.viewResolvers = (this.viewResolvers != null ? this.viewResolvers :
      Collections.singletonList(new InternalResourceViewResolver()));
  for (Object viewResolver : this.viewResolvers) {
    if (viewResolver instanceof WebApplicationObjectSupport) {
      ((WebApplicationObjectSupport) viewResolver).setApplicationContext(wac);
    }
  }
  return this.viewResolvers;
}

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

/**
 * Return the current ServletContext.
 * @throws IllegalStateException if not running within a required ServletContext
 * @see #isContextRequired()
 */
@Nullable
protected final ServletContext getServletContext() throws IllegalStateException {
  if (this.servletContext != null) {
    return this.servletContext;
  }
  ServletContext servletContext = null;
  WebApplicationContext wac = getWebApplicationContext();
  if (wac != null) {
    servletContext = wac.getServletContext();
  }
  if (servletContext == null && isContextRequired()) {
    throw new IllegalStateException("WebApplicationObjectSupport instance [" + this +
        "] does not run within a ServletContext. Make sure the object is fully configured!");
  }
  return servletContext;
}

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

/**
 * Return the current application context as WebApplicationContext.
 * <p><b>NOTE:</b> Only use this if you actually need to access
 * WebApplicationContext-specific functionality. Preferably use
 * {@code getApplicationContext()} or {@code getServletContext()}
 * else, to be able to run in non-WebApplicationContext environments as well.
 * @throws IllegalStateException if not running in a WebApplicationContext
 * @see #getApplicationContext()
 */
@Nullable
protected final WebApplicationContext getWebApplicationContext() throws IllegalStateException {
  ApplicationContext ctx = getApplicationContext();
  if (ctx instanceof WebApplicationContext) {
    return (WebApplicationContext) getApplicationContext();
  }
  else if (isContextRequired()) {
    throw new IllegalStateException("WebApplicationObjectSupport instance [" + this +
        "] does not run in a WebApplicationContext but in: " + ctx);
  }
  else {
    return null;
  }
}

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

/**
 * Return the temporary directory for the current web application,
 * as provided by the servlet container.
 * @return the File representing the temporary directory
 * @throws IllegalStateException if not running within a ServletContext
 * @see org.springframework.web.util.WebUtils#getTempDir(javax.servlet.ServletContext)
 */
protected final File getTempDir() throws IllegalStateException {
  ServletContext servletContext = getServletContext();
  Assert.state(servletContext != null, "ServletContext is required");
  return WebUtils.getTempDir(servletContext);
}

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

/**
 * Calls {@link #initServletContext(javax.servlet.ServletContext)} if the
 * given ApplicationContext is a {@link WebApplicationContext}.
 */
@Override
protected void initApplicationContext(ApplicationContext context) {
  super.initApplicationContext(context);
  if (this.servletContext == null && context instanceof WebApplicationContext) {
    this.servletContext = ((WebApplicationContext) context).getServletContext();
    if (this.servletContext != null) {
      initServletContext(this.servletContext);
    }
  }
}

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

private List<ViewResolver> initViewResolvers(WebApplicationContext wac) {
  this.viewResolvers = (this.viewResolvers == null) ?
      Arrays.<ViewResolver>asList(new InternalResourceViewResolver()) : viewResolvers;
  for (Object viewResolver : this.viewResolvers) {
    if (viewResolver instanceof WebApplicationObjectSupport) {
      ((WebApplicationObjectSupport) viewResolver).setApplicationContext(wac);
    }
  }
  return this.viewResolvers;
}

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

/**
 * Return the current ServletContext.
 * @throws IllegalStateException if not running within a required ServletContext
 * @see #isContextRequired()
 */
@Nullable
protected final ServletContext getServletContext() throws IllegalStateException {
  if (this.servletContext != null) {
    return this.servletContext;
  }
  ServletContext servletContext = null;
  WebApplicationContext wac = getWebApplicationContext();
  if (wac != null) {
    servletContext = wac.getServletContext();
  }
  if (servletContext == null && isContextRequired()) {
    throw new IllegalStateException("WebApplicationObjectSupport instance [" + this +
        "] does not run within a ServletContext. Make sure the object is fully configured!");
  }
  return servletContext;
}

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

/**
 * Return the current application context as WebApplicationContext.
 * <p><b>NOTE:</b> Only use this if you actually need to access
 * WebApplicationContext-specific functionality. Preferably use
 * {@code getApplicationContext()} or {@code getServletContext()}
 * else, to be able to run in non-WebApplicationContext environments as well.
 * @throws IllegalStateException if not running in a WebApplicationContext
 * @see #getApplicationContext()
 */
@Nullable
protected final WebApplicationContext getWebApplicationContext() throws IllegalStateException {
  ApplicationContext ctx = getApplicationContext();
  if (ctx instanceof WebApplicationContext) {
    return (WebApplicationContext) getApplicationContext();
  }
  else if (isContextRequired()) {
    throw new IllegalStateException("WebApplicationObjectSupport instance [" + this +
        "] does not run in a WebApplicationContext but in: " + ctx);
  }
  else {
    return null;
  }
}

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

/**
 * Return the temporary directory for the current web application,
 * as provided by the servlet container.
 * @return the File representing the temporary directory
 * @throws IllegalStateException if not running within a ServletContext
 * @see org.springframework.web.util.WebUtils#getTempDir(javax.servlet.ServletContext)
 */
protected final File getTempDir() throws IllegalStateException {
  ServletContext servletContext = getServletContext();
  Assert.state(servletContext != null, "ServletContext is required");
  return WebUtils.getTempDir(servletContext);
}

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

/**
 * Calls {@link #initServletContext(javax.servlet.ServletContext)} if the
 * given ApplicationContext is a {@link WebApplicationContext}.
 */
@Override
protected void initApplicationContext(ApplicationContext context) {
  super.initApplicationContext(context);
  if (this.servletContext == null && context instanceof WebApplicationContext) {
    this.servletContext = ((WebApplicationContext) context).getServletContext();
    if (this.servletContext != null) {
      initServletContext(this.servletContext);
    }
  }
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com