【譯】註解@Inject 和@Named的例子

原文連接java

@Inject和@Named註解示例

本文介紹的兩個註解,是JSR-330的一部分。在Spring 3中,開始支持JSR-330的註解。這些註解在使用上和Spring的註解同樣,所不一樣的只是須要額外的相關jar包。你能夠使用下面的註解在spring 3應用中。
- @Inject替代@Autowired來執行注入
- @Named替代@Component來聲明一個Bean

讓咱們使用Eclipse來建立Spring應用:
- 1.建立一個項目:建立一個名爲SpringAnnotationExamples的項目,建立一個包com.javabeat.injectandnamedannotations 
- 2.添加庫:添加相關spring jar包
- 3.建立java文件:在包com.javabeat.injectandnamedannotations中建立Product ,ProductDao, ProductDaoImpl,ProductService和MainApp
- 4.建立配置文件:Beans.xml

代碼文件

package com.javabeat.injectandnamedannotations;

 public class Product {
     String name;

     public Product(String name) {
         super();
         this.name = name;
     }

     public String getName() {
         return "Product name is:" + name;
     }

     public void setName(String name) {
         this.name = name;
     }
 }
package com.javabeat.injectandnamedannotations;

 public interface ProductDao {
     public Product getProduct(String id);
 }
package com.javabeat.injectandnamedannotations;

  import java.util.HashMap;
  import java.util.Map;

  import javax.inject.Named;

  import org.springframework.context.annotation.Scope;
  import org.springframework.stereotype.Repository;

  @Named
  public class ProductDaoImpl implements ProductDao {
      private Map<String, Product> products;

      public ProductDaoImpl() {
          products = new HashMap<String, Product>();
          products.put("P1", new Product("Product1"));
          products.put("P2", new Product("Product2"));
          products.put("P3", new Product("Product3"));

      }

      public Product getProduct(String id) {
          return products.get(id);
      }

  }
package com.javabeat.injectandnamedannotations;

  import javax.inject.Inject;
  import javax.inject.Named;

  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;

  @Named
  public class ProductService {
      @Inject
      private ProductDao productDao;

      public Product getProductDetail(String productId) {
          Product product = productDao.getProduct(productId);
          return product;
      }
  }
<?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:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
              http://www.springframework.org/schema/aop
              http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context-2.5.xsd">

         <context:component-scan base-package="com.javabeat.injectandnamedannotations"/>

    </beans>
package com.javabeat.injectandnamedannotations;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] {"Beans.xml" });

        ProductService productService = (ProductService) context
                .getBean("productService");
        System.out.println(productService.getProductDetail("P1").getName());
        System.out.println(productService.getProductDetail("P2").getName());
        System.out.println(productService.getProductDetail("P3").getName());

    }
}

代碼執行

執行MainApp在控制檯獲得以下輸出:
Product name is:Product1
Product name is:Product2
Product name is:Product3spring

  • 注意@Inject是沒有required屬性的

總結

相關文章
相關標籤/搜索