Spring JSR-250注解

Spring也基于JSR-250注解,包括@PostConstruct,@PreDestroy和@Resource 注解。虽然这些注释都没有真正必需的,因为你已经有其他的候补,但还是让我给他们有关一个简单的想法。

 

@PostConstruct 和@PreDestroy 注解:

要定义安装和拆卸一个bean,我们只是声明了初始化方法和/或销毁,<bean>方法的参数。在init-method属性指定一个方法,是被称为bean上后立即实例化。同样,销毁规定了被称为bean被从容器中取出之前的方法。

您可以使用@PostConstruct 注释的初始化回调的替代和@PreDestroyannotation 作为销毁回调的另一种如在下面的例子来说明。

例子

让我们使用Eclipse IDE,然后按照下面的步骤来创建一个Spring应用程序:

步骤 描述
1 Create a project with a name SpringExample and create a package com.manongjc under the src folder in the created project.
2 Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3 Create Java classes HelloWorld and MainApp under the com.manongjc package.
4 Create Beans configuration file Beans.xml under the src folder.
5 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

这里是HelloWorld.java 文件的内容:

package com.manongjc;
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文件的内容。在这里,你需要注册一个关闭挂钩registerShutdownHook() 是在AbstractApplicationContext类中声明方法。这将确保正常关闭,并调用相关的destroy()方法。

package com.manongjc;

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.manongjc.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.manongjc;

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属性名。