Spring – Autowiring using @Resource annotation | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

This image has an empty alt attribute; its file name is image-1.png

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-Resource 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
  • * 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="engine" 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.annotation.Resource;

/**
 * @author code.factory
 *
 */
public class Car {

	/* autowiring by using byName*/
	@Resource
	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:

Engine - Model Year 2020

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s