@Inject and @Named (JSR 330) Annotations 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)

also read: follow us on @twitter and @facebook

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

  1. Create a project: Create a project with a name SpringAnnotationExamples and create a package com.javabeat.injectandnamedannotations under the src directory in the created project.
  2. Add Libraries: Add required Spring libraries using Add External JARs option as explained in the article Customizing callback methods.
  3. Create source files: Create Java classes Product, ProductDao, ProductDaoImpl, ProductService and MainApp under the com.javabeat.injectandnamedannotations package.
  4. Create configuration file: Create XML based configuration file Beans.xml under src directory.

Contents of Product.java are:

 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;
 	}
 }
 

Contents of ProductDao.java are:

 package com.javabeat.injectandnamedannotations;

 public interface ProductDao {
 	public Product getProduct(String id);
 }
 

Contents of ProductDaoImpl.java are:

  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);
  	}

  }

 

Contents of ProductService.java are:

  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;
  	}
  }
 

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

    </beans>

The Contents of MainApp are:

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());

	}
}

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.

Summary

In this article I demonstrated with an example the use of JSR-330 annotations- @Inject and @Named. If you are interested in receiving the future articles, please subscribe here. follow us on @twitter and @facebook

Comments

comments

About Manisha Patil

Manisha S Patil, currently residing at Pune India. She is currently working as freelance writer for websites. She had earlier worked at Caritor Bangalore, TCS Bangalore and Sungard Pune. She has 5 years of experience in Java/J2EE technologies.

Comments

  1. Thank for this but you’re still not explaining the use for these annotations. Can you give more details please?

  2. Communicator says:

    How do you Inject SessionFactory/Entity Manager created by spring container into DAO which is annotated with “Named” which pure Java contained annotation?

Speak Your Mind

*