Home  >  Spring Core

Spring @Named and @Inject JSR 330 Annotation Example

By Arvind Rai, June 10, 2016
On this page we will provide spring @Named and @Inject JSR 330 annotation example. JSR 330 provides dependency injection with @Inject and @Named. JSR 330 @Named and @Inject annotations are supported in spring equivalent to spring @Component and @Autowired respectively. A bean annotated with @Named at class level behaves as a spring component in spring container. @Inject autowires the bean in the same way as spring @Autowired annotation. Find the complete example.

JSR 330 Standard Annotations: @Named and @Inject

JSR 330 @Named annotation is equivalent to spring @Component and @Inject is equivalent to spring @Autowired in spring container with some limitations. A bean annotated with @Named annotation is considered as a component in spring container. We can also provide a name to bean using @Named("anyName"). @Inject autowires the bean dependency and is supported in spring in the same way as @Autowired annotation.

Limitations of JSR 330 Standard Approach

Find the limitations of JSR 330 standard approach.
1. @Inject has no required attribute.
2. @Named does not provide a composable model. It only identifies named components.
3. JSR-330 bean default scope is prototype but in spring container JSR-330 bean default scope is singleton.
4. JSR-330 does not provide annotations equivalent to spring @Value, @Required and @Lazy
5. JSR 330 provides javax.inject.Provider that is alternative of spring ObjectFactory.

Gradle to Resolve Spring and JSR 330 @Named and @Inject API

Find the gradle file to resolve spring and JSR 330 @Named and @Inject annotations API.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'SpringDemo'
version = '1' 
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.springframework.boot:spring-boot-starter:1.3.3.RELEASE'
    compile 'javax.inject:javax.inject:1'
} 
Find the maven for JSR 330 @Named and @Inject annotations.
 <dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency> 

@Named and @Inject using @ComponentScan Annotation with JavaConfig

Here we will discuss @Named and @Inject using @ComponentScan annotation with JavaConfig.

1. @Named and @Inject with Setter Method

Employee.java
package com.concretepage.bean;
import javax.inject.Named;
@Named
public class Employee {
	public String getEmpMsg() {
		return "Software makes world beautiful";
	}
} 
EmployeeService.java
package com.concretepage;
import javax.inject.Inject;
import javax.inject.Named;
@Named
public class EmployeeService {
	private Employee employee;
	public Employee getEmployee() {
		return employee;
	}
	@Inject
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
} 
AppConfig.java
package com.concretepage;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages="com.concretepage")
public class AppConfig {
} 
SpringDemo.java
package com.concretepage;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringDemo {
   public static void main(String[] args) {
       AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
       ctx.register(AppConfig.class);
       ctx.refresh();
       EmployeeService service = ctx.getBean(EmployeeService.class);
       System.out.println(service.getEmployee().getEmpMsg());
       ctx.close();
   }
} 
Output
Software makes world beautiful
 

2. @Named and @Inject with Constructor

EmployeeService.java
package com.concretepage;
import javax.inject.Inject;
import javax.inject.Named;
@Named
public class EmployeeService {
	private Employee employee;
	@Inject
	private EmployeeService(Employee employee) {
		this.employee = employee;
	}
	public Employee getEmployee() {
		return employee;
	}
} 

3. @Named and @Inject with Field

EmployeeService.java
package com.concretepage;
import javax.inject.Inject;
import javax.inject.Named;
@Named
public class EmployeeService {
	@Inject
	private Employee employee;
	public Employee getEmployee() {
		return employee;
	}
} 

4. @Inject with Spring @Component

EmployeeService.java
package com.concretepage;
import javax.inject.Inject;
import org.springframework.stereotype.Component;
@Component
public class EmployeeService {
	@Inject
	private Employee employee;
	public Employee getEmployee() {
		return employee;
	}
} 

5. @Inject with javax.inject.Provider

EmployeeService.java
package com.concretepage;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
@Named
public class EmployeeService {
	@Inject
	private Provider employeeProvider;
	public Employee getEmployee() {
		return employeeProvider.get();
	}
} 

6. @Named and @Inject Qualified Name for Dependency

SalesEmployee.java
package com.concretepage.bean;
import javax.inject.Named;
@Named("sales")
public class SalesEmployee implements IEmployee {
	@Override
	public String getEmpMsg() {
		return "Sales Employee";
	}
} 
SoftwareEmployee.java
package com.concretepage.bean;
import javax.inject.Named;
@Named("soft")
public class SoftwareEmployee implements IEmployee {
	@Override
	public String getEmpMsg() {
		return "Software Employee";
	}

} 
IEmployee.java
package com.concretepage.bean;
public interface IEmployee {
	String getEmpMsg();
} 
EmployeeService.java
package com.concretepage;
import javax.inject.Inject;
import javax.inject.Named;
import com.concretepage.bean.IEmployee;
@Named
public class EmployeeService {
	private IEmployee employee;
	@Inject
	private EmployeeService(@Named("soft")IEmployee employee) {
		this.employee = employee;
	}
	public IEmployee getEmployee() {
		return employee;
	}
} 

Output

Software Employee
 

@Named and @Inject using XML Configuration

Here we will provide XML configuration that will support @Named and @Inject. We need to configure component-scan to support these annotations.
app-conf.xml
<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.concretepage"/>
</beans> 

References

Dependency Injection with @Inject and @Named
Spring @Autowired Annotation Example with Setter Method, Field and Constructor using XML and Java Configuration

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us