Spring-jsr250-annotations

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

Spring JSR-250アノテーション

Springは、@ PostConstruct、@ PreDestroy、@ Resourceアノテーションを含むJSR-250ベースのアノテーションもサポートしています。 既に他の代替手段があるため、これらの注釈は必ずしも必要ではありませんが、それらについて簡単に説明します。

@PostConstructおよび@PreDestroyアノテーション

Beanのセットアップとティアダウンを定義するには、 init-method または destroy-method パラメーターで<bean>を宣言するだけです。 init-method属性は、インスタンス化の直後にBeanで呼び出されるメソッドを指定します。 同様に、destroy-methodは、Beanがコンテナから削除される直前に呼び出されるメソッドを指定します。

以下の例で説明するように、初期化コールバックの代替として @ PostConstruct アノテーションを使用し、破棄コールバックの代替として @ PreDestroy アノテーションを使用できます。

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

Step 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;
import javax.annotation.*;

public class HelloWorld {
   private String message;

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

   @PostConstruct
   public void init(){
      System.out.println("Bean is going through init.");
   }

   @PreDestroy
   public void destroy(){
      System.out.println("Bean will destroy now.");
   }
}

以下は MainApp.java ファイルの内容です。 ここでは、AbstractApplicationContextクラスで宣言されているシャットダウンフック* registerShutdownHook()*メソッドを登録する必要があります。 これにより、正常なシャットダウンが保証され、関連する破棄メソッドが呼び出されます。

package com.finddevguides;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

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

以下は、initおよびdestroyメソッドに必要な構成ファイル 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"
   xmlns:context = "http://www.springframework.org/schema/context"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config/>
   <bean id = "helloWorld" class = "com.finddevguides.HelloWorld"
      init-method = "init" destroy-method = "destroy">
      <property name = "message" value = "Hello World!"/>
   </bean>

</beans>

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

Bean is going through init.
Your Message : Hello World!
Bean will destroy now.

@リソース注釈

フィールドまたはセッターメソッドで @ Resource アノテーションを使用でき、Java EE 5と同じように機能します。 @Resourceアノテーションは、注入されるBean名として解釈される 'name’属性を取ります。 あなたが言うことができる、それは次の例に示すように by-name 自動配線セマンティクスに従います-

package com.finddevguides;

import javax.annotation.Resource;

public class TextEditor {
   private SpellChecker spellChecker;

   @Resource(name = "spellChecker")
   public void setSpellChecker( SpellChecker spellChecker ){
      this.spellChecker = spellChecker;
   }
   public SpellChecker getSpellChecker(){
      return spellChecker;
   }
   public void spellCheck(){
      spellChecker.checkSpelling();
   }
}

「名前」が明示的に指定されていない場合、デフォルト名はフィールド名またはセッターメソッドから派生します。 フィールドの場合、フィールド名を取ります。セッターメソッドの場合、Beanプロパティ名を使用します。