
- Spring核心基礎
- Spring - 首頁
- Spring - 概述
- Spring - 架構
- Spring - 環境搭建
- Spring - Hello World 示例
- Spring - IoC容器
- Spring - Bean定義
- Spring - Bean作用域
- Spring - Bean生命週期
- Spring - Bean後處理器
- Spring - Bean定義繼承
- Spring - 依賴注入
- Spring - 注入內部Bean
- Spring - 注入集合
- Spring - Bean自動裝配
- 基於註解的配置
- Spring - 基於Java的配置
- Spring - Spring中的事件處理
- Spring - Spring中的自定義事件
- Spring - Spring框架中的AOP
- Spring - JDBC框架
- Spring - 事務管理
- Spring - Web MVC框架
- Spring - 使用Log4J進行日誌記錄
- Spring問答
- Spring - 問答
- Spring有用資源
- Spring - 快速指南
- Spring - 有用資源
- Spring - 討論
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類HelloWorld和MainApp。 |
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屬性名稱。