Skip to main content

Spring autowire Example

What is Autowire in Spring ?

Autowiring is a mechanism of resolving dependencies in Spring IoC container, as per Spring Reference. So instead of directly specifying the dependency (in XML or in Java configuration), you can depend on container itself to provide you with candidate(s). Spring itself should abort if you find more than one matching dependency (unless you are looking for a collection of beans).

Autowiring in Spring Autowiring feature of spring framework enables you to inject the object dependency implicitly. It internally uses setter or constructor injection. Autowiring can’t be used to inject primitive and string values. It works with reference only.

Advantage of Autowiring

It requires the less code because we don’t need to write the code to inject the dependency explicitly.

Spring support autowire
1. byName
2. byType

Project structure

Point.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package org.training.javaforlearn;
 
public class Point 
{
 
int x;
int y;
 
public int getX() 
{
	return x;
}
public void setX(int x) 
{
	this.x = x;
}
 
public int getY() 
 
{
	return y;
}
 
public void setY(int y) 
{
	this.y = y;
}
 
}

Triangle.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package org.training.javaforlearn;
 
public class Triangle
 
{
 
	Point pointa;
	Point pointb;
	Point pointc;
 
 
	public Point getPointa() 
	{
		return pointa;
	}
 
	public void setPointa(Point pointa) 
	{
		this.pointa = pointa;
	}
 
	public Point getPointb() 
	{
		return pointb;
	}
 
	public void setPointb(Point pointb)
	{
		this.pointb = pointb;
	}
 
	public Point getPointc() 
	{
		return pointc;
	}
 
	public void setPointc(Point pointc) 
	{
		this.pointc = pointc;
	}
 
 
	public void draw()
	{
 
		System.out.println("****** triangle drawn******** " );
 
		System.out.println("point1 "+"("+getPointa().getX()+","+getPointa().getY()+")");
		System.out.println("point2 "+"("+getPointb().getX()+","+getPointb().getY()+")");
		System.out.println("point2 "+"("+getPointc().getX()+","+getPointc().getY()+")");
 
	}
}

Spring8.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="UTF-8"?>
 
   <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
 
   <bean id="triangle1" class="org.training.javaforlearn.Triangle" autowire="byName">   
    </bean> 
 
   <bean id="pointa" class="org.training.javaforlearn.Point">
   <qualifier value="circle-o"/>
   <property name="x" value="0"></property>
   <property name="y" value="0"></property>
   </bean>    
 
   <bean id="pointb" class="org.training.javaforlearn.Point">  
    <property name="x" value="-12"></property>
   <property name="y" value="0"></property>
   </bean>    
 
   <bean id="pointc" class="org.training.javaforlearn.Point">
   <property name="x" value="12"></property>
   <property name="y" value="0"></property>
   </bean>    
 
  </beans>

DrawTriangle.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package org.training.javaforlearn;
 
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 
public class DrawTriangle 
{
	public static void main(String[] args) 
	{
		ApplicationContext context=new ClassPathXmlApplicationContext("spring8.xml");
		Triangle triangle=(Triangle)context.getBean("triangle1");
		triangle.draw();
	}
}

Output