原文链接

@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:Product3

  • 注意@Inject是没有required属性的

总结


沈子平
183 声望17 粉丝

慢慢积累,一切都不会太晚.