Package org.springframework.web.context.support

# Class WebApplicationObjectSupport

java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport

All Implemented Interfaces:
  `org.springframework.beans.factory.Aware`, `org.springframework.context.ApplicationContextAware`, `ServletContextAware`

public abstract class WebApplicationObjectSupport extends org.springframework.context.support.ApplicationObjectSupport implements ServletContextAware
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.

Since:
  28.08.2003
Author:
  Juergen Hoeller
See Also:
  - `SpringBeanAutowiringSupport`

## Field Summary

### Fields inherited from class org.springframework.context.support.ApplicationObjectSupport

`logger`

## Constructor Summary

Constructors
Constructor
Description
`WebApplicationObjectSupport()`

## Method Summary

Modifier and Type
Method
Description
`protected final ServletContext`
`getServletContext()`
Return the current ServletContext.
`protected final File`
`getTempDir()`
Return the temporary directory for the current web application, as provided by the servlet container.
`protected final WebApplicationContext`
`getWebApplicationContext()`
Return the current application context as WebApplicationContext.
`protected void`
`initApplicationContext(org.springframework.context.ApplicationContext context)`
Calls `initServletContext(jakarta.servlet.ServletContext)` if the given ApplicationContext is a `WebApplicationContext`.
`protected void`
`initServletContext(ServletContext servletContext)`
Subclasses may override this for custom initialization based on the ServletContext that this application object runs in.
`protected boolean`
`isContextRequired()`
Overrides the base class behavior to enforce running in an ApplicationContext.
`final void`
`setServletContext(ServletContext servletContext)`
Set the `ServletContext` that this object runs in.

### Methods inherited from class org.springframework.context.support.ApplicationObjectSupport

`getApplicationContext, getMessageSourceAccessor, initApplicationContext, obtainApplicationContext, requiredContextClass, setApplicationContext`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### WebApplicationObjectSupport

public WebApplicationObjectSupport()

## Method Details

### setServletContext

public final void setServletContext(ServletContext servletContext)
Description copied from interface: `ServletContextAware`
Set the `ServletContext` that this object runs in.

Invoked after population of normal bean properties but before an init callback like InitializingBean's `afterPropertiesSet` or a custom init-method. Invoked after ApplicationContextAware's `setApplicationContext`.

Specified by:
  `setServletContext` in interface `ServletContextAware`
Parameters:
  `servletContext` - the ServletContext object to be used by this object
See Also:
  - `InitializingBean.afterPropertiesSet()`
  - `ApplicationContextAware.setApplicationContext(org.springframework.context.ApplicationContext)`

### isContextRequired

protected boolean isContextRequired()
Overrides the base class behavior to enforce running in an ApplicationContext. All accessors will throw IllegalStateException if not running in a context.

Overrides:
  `isContextRequired` in class `org.springframework.context.support.ApplicationObjectSupport`
See Also:
  - `ApplicationObjectSupport.getApplicationContext()`
  - `ApplicationObjectSupport.getMessageSourceAccessor()`
  - `getWebApplicationContext()`
  - `getServletContext()`
  - `getTempDir()`

### initApplicationContext

protected void initApplicationContext(org.springframework.context.ApplicationContext context)
Calls `initServletContext(jakarta.servlet.ServletContext)` if the given ApplicationContext is a `WebApplicationContext`.

Overrides:
  `initApplicationContext` in class `org.springframework.context.support.ApplicationObjectSupport`

### initServletContext

protected void initServletContext(ServletContext servletContext)
Subclasses may override this for custom initialization based on the ServletContext that this application object runs in.

The default implementation is empty. Called by `initApplicationContext(org.springframework.context.ApplicationContext)` as well as `setServletContext(jakarta.servlet.ServletContext)`.

Parameters:
  `servletContext` - the ServletContext that this application object runs in (never `null`)

### getWebApplicationContext

@Nullable protected final WebApplicationContext getWebApplicationContext() throws IllegalStateException
Return the current application context as WebApplicationContext.

NOTE: Only use this if you actually need to access WebApplicationContext-specific functionality. Preferably use `getApplicationContext()` or `getServletContext()` else, to be able to run in non-WebApplicationContext environments as well.

Throws:
  `IllegalStateException` - if not running in a WebApplicationContext
See Also:
  - `ApplicationObjectSupport.getApplicationContext()`

### getServletContext

@Nullable protected final ServletContext getServletContext() throws IllegalStateException
Return the current ServletContext.

Throws:
  `IllegalStateException` - if not running within a required ServletContext
See Also:
  - `isContextRequired()`

### getTempDir

protected final File getTempDir() throws IllegalStateException
Return the temporary directory for the current web application, as provided by the servlet container.

Returns:
  the File representing the temporary directory
Throws:
  `IllegalStateException` - if not running within a ServletContext
See Also:
  - `WebUtils.getTempDir(jakarta.servlet.ServletContext)`