@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) {
 		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;

  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;

  public class ProductService {
  	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"

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


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


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.


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

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!