Spring — Autowiring using @Inject annotation | Code Factory

Code Factory
2 min readJan 21, 2021

Donate : Link

WordPress Blog : Link

Applications… : Link

Spring Tutorial Index Page: Link

  • To inject values into object we use @Autowired and @Qualifier annotation of Spring. Same type of annotation we have from JDK also. By using these annotation we can also do autowiring. So to do autowiring using JDK we have @Resource and @Inject. Both are equal to @Autowired. Under javax package we have these annotations.
  • If we use @Resource instead of @Annotation then it will do byName autowiring. @Inject is equal to @Autowired, it will check byType only and we can also use @Qualifier with @Inject.
  • @Inject first check byName and it is find then there is no ambiguity because we have unique ids in bean but if it is not found then it check byType and if we have multiple beans with the same type then it will throw ambiguity.

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Others… -> Java Project
  • Create Autowiring-Inject project
  • Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs
    - commons-logging-X.X.jar
    - spring-aop-X.X.X.jar
    - spring-beans-X.X.X.jar
    - spring-context-X.X.X.jar
    - spring-core-X.X.X.jar
    - spring-expression-X.X.X.jar
    - javax.inject-1.jar
  • * You can find namespace information from spring-beans-X.X.X.jar -> META-INF -> spring.schemas

spring.xml

<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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:annotation-config />
<bean id="e1" class="com.codeFactory.beans.Engine">
<property name="modelYear" value="2020" />
</bean>
<bean id="e2" class="com.codeFactory.beans.Engine">
<property name="modelYear" value="2019" />
</bean>

<bean id="c" class="com.codeFactory.beans.Car" />
</beans>

Car.java

package com.codeFactory.beans;import javax.inject.Inject;import org.springframework.beans.factory.annotation.Qualifier;
/**
* @author code.factory
*
*/
public class Car {
/* autowiring by using byName and if not matched then byType*/
@Inject
@Qualifier("e2")
private Engine engine;
public void print() {
System.out.println("Engine - Model Year " + engine.getModelYear());
}
}

Engine.java

package com.codeFactory.beans;/**
* @author code.factory
*
*/
public class Engine {
private int modelYear;
public int getModelYear() {
return modelYear;
}
public void setModelYear(int modelYear) {
this.modelYear = modelYear;
}
}

Client.java

package com.codeFactory.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.codeFactory.beans.Car;/**
* @author code.factory
*
*/
public class Client {
public static void main(String... args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");
Car t = (Car) context.getBean("c");
t.print();
}
}

Output:

INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Engine - Model Year 2019

--

--