Spring JSR-250 注釋

2022-05-16 15:28 更新

Spring JSR-250 注釋

Spring還使用基于 JSR-250 注釋,它包括 @PostConstruct, @PreDestroy 和 @Resource 注釋。因?yàn)槟阋呀?jīng)有了其他的選擇,盡管這些注釋并不是真正所需要的,但是關(guān)于它們?nèi)匀蛔屛医o出一個(gè)簡(jiǎn)短的介紹。

@PostConstruct 和 @PreDestroy 注釋:

為了定義一個(gè) bean 的安裝和卸載,我們使用 init-method 和/或 destroy-method 參數(shù)簡(jiǎn)單的聲明一下 。init-method 屬性指定了一個(gè)方法,該方法在 bean 的實(shí)例化階段會(huì)立即被調(diào)用。同樣地,destroy-method 指定了一個(gè)方法,該方法只在一個(gè) bean 從容器中刪除之前被調(diào)用。

你可以使用 @PostConstruct 注釋作為初始化回調(diào)函數(shù)的一個(gè)替代,@PreDestroy 注釋作為銷毀回調(diào)函數(shù)的一個(gè)替代,其解釋如下示例所示。

示例

讓我們使 Eclipse IDE 處于工作狀態(tài),請(qǐng)按照下列步驟創(chuàng)建一個(gè) Spring 應(yīng)用程序:

步驟 描述
1 創(chuàng)建一個(gè)名為 SpringExample 的項(xiàng)目,并且在所創(chuàng)建項(xiàng)目的 src 文件夾下創(chuàng)建一個(gè)名為 com.tutorialspoint 的包。
2 使用 Add External JARs 選項(xiàng)添加所需的 Spring 庫(kù)文件,就如在 Spring Hello World Example 章節(jié)中解釋的那樣。
3 com.tutorialspoint 包下創(chuàng)建 Java 類 HelloWorldMainApp。
4 src 文件夾下創(chuàng)建 Beans 配置文件 Beans.xml。
5 最后一步是創(chuàng)建所有 Java 文件和 Bean 配置文件的內(nèi)容,并且按如下解釋的那樣運(yùn)行應(yīng)用程序。

這里是 HelloWorld.java 文件的內(nèi)容:

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 文件的內(nèi)容。這里你需要注冊(cè)一個(gè)關(guān)閉鉤 registerShutdownHook() 方法,該方法在 AbstractApplicationContext 類中被聲明。這將確保一個(gè)完美的關(guān)閉并調(diào)用相關(guān)的銷毀方法。

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

下面是配置文件 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 配置文件中完成了上面兩處改變,讓我們運(yùn)行一下應(yīng)用程序。如果你的應(yīng)用程序一切都正常的話,這將會(huì)輸出以下消息:

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

@Resource 注釋:

你可以在字段中或者 setter 方法中使用 @Resource 注釋,它和在 Java EE 5 中的運(yùn)作是一樣的。@Resource 注釋使用一個(gè) ‘name’ 屬性,該屬性以一個(gè) bean 名稱的形式被注入。你可以說(shuō),它遵循 by-name 自動(dòng)連接語(yǔ)義,如下面的示例所示:

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

如果沒(méi)有明確地指定一個(gè) ‘name’,默認(rèn)名稱源于字段名或者 setter 方法。在字段的情況下,它使用的是字段名;在一個(gè) setter 方法情況下,它使用的是 bean 屬性名稱。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)