Spring JSR-250 註解



Spring也支援基於JSR-250的註解,包括@PostConstruct、@PreDestroy和@Resource註解。雖然這些註解並非必需,因為你已經有其他替代方案,但讓我們簡要了解一下它們。

@PostConstruct和@PreDestroy註解

要定義Bean的設定和拆卸,我們只需使用init-method和/或destroy-method引數宣告<bean>。init-method屬性指定一個方法,該方法將在例項化Bean後立即呼叫。類似地,destroy-method屬性指定一個方法,該方法在Bean從容器中移除之前呼叫。

您可以使用@PostConstruct註解作為初始化回撥的替代方案,使用@PreDestroy註解作為銷燬回撥的替代方案,如下例所示。

示例

讓我們準備好一個可用的Eclipse IDE,並按照以下步驟建立一個Spring應用程式:

步驟 描述
1 建立一個名為SpringExample的專案,並在建立的專案中的src資料夾下建立一個com.tutorialspoint包。
2 Spring Hello World 示例章節所述,使用新增外部JAR選項新增所需的Spring庫。
3 com.tutorialspoint包下建立Java類HelloWorldMainApp
4 src資料夾下建立Beans配置檔案Beans.xml
5 最後一步是建立所有Java檔案和Bean配置檔案的內容,並執行應用程式,如下所述。

以下是HelloWorld.java檔案的內容:

package com.tutorialspoint;
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.tutorialspoint;

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.tutorialspoint.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註解

您可以將@Resource註解用於欄位或setter方法,其作用與Java EE 5中相同。@Resource註解採用'name'屬性,該屬性將被解釋為要注入的Bean名稱。可以說,它遵循以下示例中演示的按名稱自動裝配語義:

package com.tutorialspoint;

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();
   }
}

如果沒有顯式指定'name',則預設名稱將從欄位名稱或setter方法派生。對於欄位,它採用欄位名稱;對於setter方法,它採用Bean屬性名稱。

spring_annotation_based_configuration.htm
廣告