【譯】註解@Resource @PostConstruct 和@PreDestory例子

註解@Resource @PostConstruct 和@PreDestory例子

本文講解JSR250中的註解。這些註解在Spring 2.5中引入Spring,包括@Resource ,@PostConstruct 和 @PreDestroy三個註解。

@Resource和@Autowired

@Resource和@Autowired這兩個註解在功能上類似。@Autowired是spring的註解,@Resource是JSR-250的註解。可是@Autowired是能夠放在構造器上的。
默認狀況下spring按照對象類型來進行自動裝配。若是你一個類在容器中實例了兩個bean,這樣就會致使spring沒法決定使用哪個了。這個時候,就須要@Qualifier註解來幫忙,它來解決這種衝突。使用@Resource時,你能夠將bean的名字做爲屬性放在註解裏。

@PostConstruct 和 @PreDestroy

這兩個註解修飾的方法會在bean初始化和銷燬的時候調用。能夠參考[spring callback method][1]

例子

package com.javabeat.jsrannotations;

 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import javax.annotation.Resource;

 public class Product {
     private Integer price;
     private String name;

     @Resource(name = "typeB")
     private Type type;

     public Integer getPrice() {
         return price;
     }

     public void setPrice(Integer price) {
         this.price = price;
     }

     public Type getType() {
         return type;
     }

     public String getName() {
         return name;
     }

     public void setName(String name) {
         this.name = name;
     }

     @PostConstruct
     public void init() {
         System.out.println("In init block of Product");
     }

     @PreDestroy
     public void destroy() {
         System.out.println("In destroy block of Product");
     }
}
這個類中,咱們使用了@Resource註解。@PostConstruct 和 @PreDestroy使用在bean各自的生命週期事件回調方法上。
package com.javabeat.jsrannotations;

 public class Type {

      private String productType;

     public String getProductType() {
         return productType;
     }

     public void setProductType(String productType) {
         this.productType = productType;
     }

 }
package com.javabeat.jsrannotations;

 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(
                 "BeansJSRAnnotation.xml");

         Product product = (Product) context.getBean("product");

         System.out.println("Product Name : " + product.getName());
         System.out.println("Price : " + product.getPrice());

         Type productType = product.getType();

         System.out.println(product.getName() + " is of type:"
                 + productType.getProductType());
         context.registerShutdownHook();
     }
}
在MainApp中,使用registerShutdownHook()來註冊一個關閉的鉤子方法。這會保證優雅關閉和調用相關的銷燬方法。
<?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.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <context:annotation-config />

    <!-- Definition for Product bean -->
    <bean id="product" class="com.javabeat.jsrannotations.Product" init-method="init" destroy-method="destroy">
        <property name="name" value="ProductA" />
        <property name="price" value="400" />
    </bean>
    <bean id="typeA" class="com.javabeat.jsrannotations.Type">
         <property name="productType" value="Export" />
    </bean>
    <bean id="typeB" class="com.javabeat.jsrannotations.Type">
         <property name="productType" value="Import" />
    </bean>
</beans>
如你所見,對同一個類型有兩個bean。在Product.java中使用了@Resource(「typeB」)意味着須要執行自動注入Product類型的,而且bean名字是typeB的實例。
 執行結果以下:
    In init block of Product
    Product Name : ProductA
    Price : 400
    ProductA is of type:Import
    In destroy block of Product
相關文章
相關標籤/搜索