BeanFactory ApplicationContext WebApplicationContext ServletContext Servlet

BeanFactory
The root interface for accessing a Spring bean container.

ApplicationContext
Central interface to provide configuration for an application.
This is read-only while the application is running, but may be
reloaded if the implementation supports this.

An ApplicationContext provides:

  • Bean factory methods for accessing application conponents.
    Inherited from ListableBeanFactory.
  • The ability to load file resources in a generic fashion.
    Inherited from the ResourceLoader interface.
  • The ability to publish events to registered listeners.
    Inherited from the ApplicationEventPublisher interface.
  • ……

In addition to standard BeanFactory lifecycle capabilities, ApplicationContext implementations detect and invoke ApplicationContextAware beans as well as ResourceLoaderAware, ApplicationEventPublisherAware and MessageSourceAware beans.

WebApplicationContext
Interface to provide configuration for a web application.
This is read-only while the application is running, but may be reloaded if the implementation supports this.

This interface adds a getServletContext() method to the generic ApplicationContext interface, and defines a well-known application attribute name that the root context must be bound to in the bootstrap process.

Like generic application contexts, web applicaiton contexts are hierarchical.
There is a single root context per application, while each servlet in the application(including a dispatcher servlet in the MVC framework) has its own child context.

In addition to standard application context lifecycle capabilities,
WebApplicationContext implementations need to detect ServletContextAware beans and invoke the setServletContext method accordingly.

ServletContext
Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.

There is one context per “web application” per Java Virtual Machine. (A “web application” is a colleciton of servlets and content installed under a specific subset of the server’s URL namespace such as /catalog and possibly installed via a .war file)

In the case of a web application marked “distributed” in its deployment descriptor, there will be one context instance for each virtual machine. In this situation, the context cannot be used as a location to share global information(because the infromation won’t be truly global). Use an external resource like a database instead.

The ServletContext object is contained within the ServletConfig object, which the web server provides the servlet when the servlet is initialized.

Servlet interface
Defines methods that all servlets must implement.

A servlet is a small Java program that runs within a web server.
Servlets receive and respond to requests from web clients, usually across HTTP.

To implement this interface, you can write a generic servlet that extends GenericServlet or an HTTP servlet that extends HttpServlet.

/**Called by the servlet container to indicate to a servlet that the 
servlet is being placed into service.

The servlet container calls the init method exactly once after instantiating the servlet. The init method must complete successfully before the servlet can receive any requests.
*/
public void init(ServletConfig config) throws ServletException;

/**
Called by the servlet container to allow the servlet to respond to a request.
This method is only called after the servlet's init method has completed successfully.
*/
/**
Servlets typically run inside multithreaded servlet containers that can handle multiple requests concurrently. 
Developers must be aware to synchronize access to any shared resources such as files, network connections, and as well as the servlet's class and instance variables.
*/
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException;

ServletRequest interface
Defines an object to provide client request information to a servlet.
The servlet container creates a ServletRequest object and passes it as an argument to the servlet’s service method.

A ServletRequest object provides data including parameter name and values, attributes, and an input stream.
Interfaces that extend ServletRequest can provide additional protocol-specific data ( for example, HTTP data is provided by HttpServletRequest).

ServletResponse interface
Defines an object to assist a servlet in sending a response to the client.
The servlet container creates a ServletResponse object and passes it as an argument to the servlet’s service method.

To send binary data in a MIME body response, use the ServletOutputStream returned by getOutputStream.
To send character data, use the PrintWriter object returned by getWriter.
To mix binary and text data, for example, to create a multipart response, use a
ServletOutputStream and manage the character sections manually.

The charset for the MIME body response can be specified explicitly using the setCharacterEncoding and setContentType methods, or implicitly using the setLocale method.
Explicit specifications take precedence over implicit specifications. If no charset is specified, ISO-8859-1 will be used. The setCharacterEncoding, setContentType,setLocale method must be called before getWriter and before committing the response for the character encoding to be used.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325485375&siteId=291194637