Spring-applicationcontext-container

提供:Dev Guides
移動先:案内検索

Spring ApplicationContextコンテナ

アプリケーションコンテキストは、Springの高度なコンテナです。 BeanFactoryと同様に、Bean定義をロードし、Beanをワイヤリングし、要求に応じてBeanを分配できます。 さらに、プロパティファイルからテキストメッセージを解決する機能や、関心のあるイベントリスナーにアプリケーションイベントを発行する機能など、エンタープライズ固有の機能を追加します。 このコンテナは、_org.springframework.context.ApplicationContext_インターフェイスによって定義されます。

_ApplicationContext_には_BeanFactory_のすべての機能が含まれます。通常、BeanFactoryよりも推奨されます。 BeanFactoryは、モバイルデバイスやアプレットベースのアプリケーションなどの軽量アプリケーションに引き続き使用できます。

最も一般的に使用されるApplicationContext実装は-

  • FileSystemXmlApplicationContext -このコンテナは、XMLファイルからBeanの定義をロードします。 ここでは、コンストラクターにXML Bean構成ファイルの完全なパスを提供する必要があります。
  • ClassPathXmlApplicationContext -このコンテナは、XMLファイルからBeanの定義をロードします。 ここでは、XMLファイルのフルパスを指定する必要はありませんが、このコンテナはCLASSPATHのBean構成XMLファイルのように見えるため、CLASSPATHを適切に設定する必要があります。
  • WebXmlApplicationContext -このコンテナは、Webアプリケーション内からすべてのBeanの定義を含むXMLファイルをロードします。

_Spring Hello World Example_のClassPathXmlApplicationContextコンテナの例を既に見ており、WebベースのSpringアプリケーションについて説明するときに、別の章でXmlWebApplicationContextについて詳しく説明します。 それでは、FileSystemXmlApplicationContextの例を見てみましょう。

動作するEclipse IDEを用意し、次の手順を実行してSpringアプリケーションを作成します。

Steps Description
1 Create a project with a name SpringExample and create a package com.finddevguides under the *src *folder in the created project.
2 Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3 Create Java classes HelloWorld and MainApp under the com.finddevguides package.
4 Create Beans configuration file Beans.xml under the* src* folder.
5 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.
*HelloWorld.java* ファイルの内容は次のとおりです-
package com.finddevguides;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }
   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
}

以下は、2番目のファイル MainApp.java の内容です-

package com.finddevguides;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new FileSystemXmlApplicationContext
         ("C:/Users/ZARA/workspace/HelloSpring/src/Beans.xml");

      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
      obj.getMessage();
   }
}

次の2つの重要な点は、メインプログラムについて注意する必要があります-

 *最初のステップは、フレームワークAPI*  *FileSystemXmlApplicationContext* *を使用してファクトリオブジェクトを作成し、指定されたパスからBean構成ファイルをロードした後にファクトリBeanを作成することです。 ** FileSystemXmlApplicationContext()** APIは、すべてのオブジェクトの作成と初期化を処理します。 XML Bean構成ファイルに記載されているBean。
* 2番目のステップは、作成されたコンテキストの* getBean()*メソッドを使用して必要なBeanを取得するために使用されます。 このメソッドはBean IDを使用して、最終的に実際のオブジェクトにキャストできる汎用オブジェクトを返します。 オブジェクトを取得したら、このオブジェクトを使用して任意のクラスメソッドを呼び出すことができます。

Bean構成ファイル Beans.xml の内容は次のとおりです。

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
   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-3.0.xsd">

   <bean id = "helloWorld" class = "com.finddevguides.HelloWorld">
      <property name = "message" value = "Hello World!"/>
   </bean>

</beans>

ソースおよびBean構成ファイルの作成が完了したら、アプリケーションを実行します。 すべてがあなたのアプリケーションでうまくいけば、それは次のメッセージを印刷します-

Your Message : Hello World!