SpringのapplicationContext.xmlとspring-servlet.xmlの違い
1. 序章
Springアプリケーションを開発するときは、Beanを探す場所をフレームワークに指示する必要があります。 アプリケーションが起動すると、フレームワークはそれらすべてを見つけて登録し、さらに実行します。 同様に、Webアプリケーションへのすべての着信要求が処理されるマッピングを定義する必要があります。
すべてのJavaWebフレームワークは、サーブレットAPIの上に構築されています。 Webアプリケーションでは、3つのファイルが重要な役割を果たします。 通常、これらを次の順序でチェーンします: web.xml-> applicationContext.xml-> spring-servlet.xml
この記事では、applicationContextとspring-servletの違いを見ていきます。
2. applicationContext.xml
制御の反転(IoC)はSpringフレームワークのコアです。 IoC対応のフレームワークでは、通常、コンテナーはオブジェクトのインスタンス化、作成、および削除を担当します。 Springでは、applicationContextがIoCコンテナの役割を果たします。
標準のJ2EEアプリケーションを開発する場合、web.xmlファイルでContextLoaderListenerを宣言します。 さらに、 contextConfigLocation も定義され、XML構成ファイルを示します。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
アプリケーションが起動すると、Springはこの構成ファイルをロードし、それを使用してWebApplicationContextオブジェクトを作成します。 contextConfigLocation、デフォルトがない場合、システムは/WEB-INF/applicationContext.xmlを検索してをロードします。
つまり、applicationContextはSpringの中央インターフェースです。 アプリケーションの構成情報を提供します。
このファイルでは、アプリケーションに関連する構成を提供します。 通常、これらは、他の拡張機能の中でも、基本的なデータソース、プロパティプレースホルダーファイル、およびプロジェクトローカリゼーション用のメッセージソースです。
サンプルファイルを見てみましょう。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:property-placeholder location="classpath:/database.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>
</beans>
ApplicationContextはBeanFactoryインターフェースの完全なスーパーセットであるため、BeanFactoryのすべての機能を提供します。統合されたライフサイクル管理、BeanPostProcessorおよび
3. spring-servlet.xml
Springでは、単一のフロントサーブレットが着信リクエストを受け取り、それらを適切なコントローラメソッドに委任します。 フロントコントローラーデザインパターンに基づくフロントサーブレットは、特定のWebアプリケーションのすべてのHTTPリクエストを処理します。 このフロントサーブレットは、着信要求をすべて制御します。
同様に、 spring-servlet はフロントコントローラーサーブレットとして機能し、単一のエントリポイントを提供します。 着信URIを受け取ります。 舞台裏では、 HandlerMapping 実装を使用して、リクエストとハンドラーオブジェクト間のマッピングを定義します。
サンプルコードを見てみましょう。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.baeldung.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
4. applicationContext.xml対。 spring-servlet.xml
要約ビューを見てみましょう。
特徴 | applicationContext.xml | spring-servlet.xml |
フレームワーク | これはSpringフレームワークの一部です。 | これは、SpringMVCフレームワークの一部です。 |
目的 | 春の豆を定義するコンテナ。 | 着信要求を処理するフロントコントローラー。 |
範囲 | これは、すべてのサーブレット間で共有されるBeanを定義します。 | サーブレット固有のBeanのみを定義します。 |
管理します | データソース、などのグローバルなものを管理し、接続ファクトリが定義されています。 | 逆に、コントローラーやviewresolverなどのWeb関連のもののみが定義されます。 |
参考文献 | spring-servletのBeanにはアクセスできません。 | applicationContextで定義されたBeanにアクセスできます。 |
共有 | アプリケーション全体に共通するプロパティはここにあります。 | 1つのサーブレットにのみ固有のプロパティがここに表示されます。 |
走査 | パッケージを含める/除外するフィルターを定義します。 | コントローラのコンポーネントスキャンを宣言します。 |
発生 | アプリケーションで複数のコンテキストファイルを定義するのが一般的です。 | 同様に、Webアプリケーションで複数のファイルを定義できます。 |
読み込み中 | ファイルapplicationContext.xmlは、ContextLoaderListenerによってロードされます。 | ファイルspring-servlet.xmlは、DispatcherServletによってロードされます。 |
必須 | オプション | 必須 |
5. 結論
このチュートリアルでは、applicationContextおよびspring-servletファイルについて学習しました。 次に、Springアプリケーションでのそれらの役割と責任について説明しました。 最後に、それらの違いを調べました。