目录

JSR-250注释(JSR-250 Annotations)

Spring还支持基于JSR-250的注释,包括@ PostConstruct,@ PreDestroy和@Resource注释。 虽然这些注释并不是真的需要,因为你已经有了其他的替代品,但让我们对它们进行简要的了解。

@PostConstruct和@PreDestroy注释

要定义bean的设置和拆卸,我们只需使用init-method和/或destroy-method参数声明<bean>。 init-method属性指定在实例化时立即在bean上调用的方法。 类似地,destroy-method指定在从容器中删除bean之前调用的方法。

您可以使用@PostConstruct注释作为初始化回调和@PreDestroy注释的替代,作为销毁回调的替代,如下面的示例所述。

例子 (Example)

让我们有一个可用的Eclipse IDE,并按照以下步骤创建一个Spring应用程序 -

描述
1 创建一个名为SpringExample的项目,并在创建的项目中的src文件夹下创建一个包com.iowiki
2 使用Add External JARs选项添加所需的Spring库,如Spring Hello World Example章节中所述。
3com.iowiki包下创建Java类HelloWorldMainApp
4src文件夹下创建Beans配置文件Beans.xml
5 最后一步是创建所有Java文件和Bean配置文件的内容并运行应用程序,如下所述。

以下是HelloWorld.java文件的内容 -

package com.iowiki;
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.iowiki;
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.iowiki.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注释采用'name'属性,该属性将被解释为要注入的bean名称。 你可以说,它遵循by-name自动装配语义,如下例所示 -

package com.iowiki;
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属性名称。

↑回到顶部↑
WIKI教程 @2018