Saturday, 4 May 2019

Explain @Inject and @Named Annotations with Example


Support for JSR-330 annotations was introduced in Spring 3. These annotations are scanned the same way as the Spring annotations, only requirement would be to have the relevant jars in your classpath. You can use the following annotations in Spring 3 applications:

@Inject instead of Spring’s @Autowired to inject a bean.



@Named instead of Spring’s @Component to declare a component/bean.(@Component can be referred from my previous post Classpath Scaning)

@Inject and @Named Annotations Example

Let’s tweak the same example I used in my previous post on Classpath Scanning and managed Components and use the annotations @Inject and @Named. Let us have working Eclipse IDE in place and follow the following steps to create a Spring application:

Create a project: Create a project with a name SpringAnnotationExamples and create a package com.aid.injectandnamedannotations under the src directory in the created project.

Add Libraries: Add required Spring libraries using Add External JARs option as explained in the article Customizing callback methods.

Create source files: Create Java classes Product, ProductDao, ProductDaoImpl, ProductService and MainApp under the com.aid.injectandnamedannotations package.

Create configuration file: Create XML based configuration file Beans.xml under src directory.

Contents of Product.java are:

 package com.aid.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;
  }
 }

 


Contents of ProductDao.java are:

 package com.aid.injectandnamedannotations;

 public interface ProductDao {

  public Product getProduct(String id);

 } 


Contents of ProductDaoImpl.java are:

  package com.aid.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);
   }
  }




Contents of ProductService.java are:

  package com.aid.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;
   }
  }

 

Contents of Beans.xml are:

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.aid.injectandnamedannotations"/>
    </beans>


The Contents of MainApp are:

package com.aid.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());
 }
}


Execute the code
The final step is run the application. Once all the above code is ready execute and the below output appears on the console:

 Product name is:Product1
 Product name is:Product2
 Product name is:Product3

One of the limitations while using @Inject is, it has no required attribute.

If you have any questions related to spring framework, please write your comments. 

Spring Boot @ConfigurationProperties and Properties File

 In this tutorial, you will learn to use @ConfigurationProperties to map properties files to POJO classes in Spring Boot application. Let’s ...