Java @Named and @Inject Annotations in Spring

By Arvind Rai, March 14, 2023
On this page, we will provide Java @Named and @Inject JSR 330 annotation example. JSR 330 provides dependency injection with @Inject and @Named annotations. JSR 330 @Named and @Inject annotations are supported in Spring equivalent to @Component and @Autowired respectively. A bean annotated with @Named at class level behaves as a Spring component in container. The @Inject autowires the bean in the same way as Spring @Autowired annotation.

1. 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.

2. 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.

3. Gradle Dependences

Find the gradle file to resolve Spring and JSR 330 @Named and @Inject annotations API.
build.gradle
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> 

4. @Named and @Inject using @ComponentScan Annotation with JavaConfig

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

4.1 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
 

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

4.3 With Class 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.4. With @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;
	}
} 

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

4.6 With Qualified Name

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
 

5. @Named and @Inject using XML Configuration

Here we will provide XML configuration that will support @Named and @Inject annotations. 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> 

6. Reference

7. Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us