Spring JSR-250 注解
Spring 还支持基于 JSR-250 的注解,其中包括 @PostConstruct、@PreDestroy 和 @Resource 注解。 尽管这些注解并不是真正需要的,因为您已经有其他替代品,但让我们简要了解一下它们。
@PostConstruct 和 @PreDestroy 注解
要定义 bean 的设置和拆卸,我们只需使用 init-method 和/或 destroy-method 参数声明 a1。 init-method 属性指定了在实例化时立即在 bean 上调用的方法。 类似地,destroy-method 指定了一个在从容器中删除 bean 之前调用的方法。
您可以使用 @PostConstruct 注解作为初始化回调的替代品,并使用 @PreDestroy 注解作为销毁回调的替代品,如下面的示例所述。
示例
让我们有一个可以工作的 Eclipse IDE,并按照以下步骤创建一个 Spring 应用程序 −
| 步骤 | 描述 |
|---|---|
| 1 | 创建一个名为 SpringExample 的项目,并在创建的项目中的 src 文件夹下创建一个包 com.tutorialspoint。 |
| 2 | 使用 Add External JARs 选项添加所需的 Spring 库,如 Spring Hello World 示例 一章中所述。 |
| 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 类上声明的关闭 hook 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 注解
您可以在字段或 setter 方法上使用 @Resource 注解,它的工作方式与 Java EE 5 中的相同。@Resource 注解采用"名称"属性,该属性将被解释为要注入的 bean 名称 . 您可以说,它遵循 by-name 自动装配语义,如以下示例所示 −
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 属性名称。

