サーブレット、Listener、FilterがサーブレットContext(アプリケーション)を取得する方法
Listenerのプロジェクトコンテキスト(サーブレットContext既存アプリケーション)はeventから取得され、eventはListenerとコンテナ間のコミュニケーションの仲介者である
public interface ServletContextListener extends EventListener {
/**
** Notification that the web application initialization
** process is starting.
** All ServletContextListeners are notified of context
** initialization before any filter or servlet in the web
** application is initialized.
*/
public void contextInitialized ( ServletContextEvent sce );
--------------------------------------------
ServletContext servletContext;
public void contextInitialized(ServletContextEvent sce)
{
servletContext = sce.getServletContext();
}
Filterのプロジェクトコンテキスト(サーブレットContext既存アプリケーション)はFilterConfigから取得され、FilterConfigはFilterとコンテナ間のコミュニケーションの仲介者である
public interface Filter {
/**
* Called by the web container to indicate to a filter that it is being placed into
* service. The servlet container calls the init method exactly once after instantiating the
* filter. The init method must complete successfully before the filter is asked to do any
* filtering work. <br><br>
* The web container cannot place the filter into service if the init method either<br>
* 1.Throws a ServletException <br>
* 2.Does not return within a time period defined by the web container
*/
public void init(FilterConfig filterConfig) throws ServletException;
------------------------
filterConfig.getServletContext()
一方、サーブレットのプロジェクトコンテキスト(サーブレットContext既存アプリケーション)は、サーブレットコンフィグから取得され、サーブレットコンフィグはサーブレットとコンテナ間のコミュニケーションの仲介者である
public interface Servlet {
/**
* Called by the servlet container to indicate to a servlet that the
* servlet is being placed into service.
*
* <p>The servlet container calls the <code>init</code>
* method exactly once after instantiating the servlet.
* The <code>init</code> method must complete successfully
* before the servlet can receive any requests.
*
* <p>The servlet container cannot place the servlet into service
* if the <code>init</code> method
* <ol>
* <li>Throws a <code>ServletException</code>
* <li>Does not return within a time period defined by the Web server
* </ol>
*
*
* @param config a <code>ServletConfig</code> object
* containing the servlet's
* configuration and initialization parameters
*
* @exception ServletException if an exception has occurred that
* interferes with the servlet's normal
* operation
*
* @see UnavailableException
* @see #getServletConfig
*
*/
public void init(ServletConfig config) throws ServletException;
---------------------------------------
getServletConfig().getServletContext()
私たちのアプリケーションコンポーネントは、一定のルールを受動的に遵守し、コンテナと付き合い、他のコンポーネントと通信し、コンテナの力を借りなければなりません.この中には実は反転を制御する味があり、コンポーネントが容器の中に生きている以上、容器から与えられた食べ物を受動的に受け入れなければならず、自分で創造することはできない.
Springが容器(軽量級と呼ばれる)と呼ぶのは、彼にコントロールされたコンポーネントが、与えられたものを受動的に食べ、自分で作ることができないからだ.