AutoWiring by Name (autowire=”byName“)
======================================
MiniTriangle.java
package com.saravanansivaji.spring;
public class MiniTriangle {
private Point pointA;
private Point pointB;
private 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("Mini-Triangle drawn with the following points using AutoWire:");
System.out.println("("+getPointA().getX()+","+getPointA().getY()+")");
System.out.println("("+getPointB().getX()+","+getPointB().getY()+")");
System.out.println("("+getPointC().getX()+","+getPointC().getY()+")");
}
}
spring.xml
<!-- MINI-SQUARE AREA Begins -->
<bean id="miniTriangle" class="com.saravanansivaji.spring.MiniTriangle" autowire="byName"/>
<bean id="pointA" class="com.saravanansivaji.spring.Point">
<property name="X" value="10"></property>
<property name="Y" value="10"></property>
</bean>
<bean id="pointB" class="com.saravanansivaji.spring.Point">
<property name="X" value="10"></property>
<property name="Y" value="20"></property>
</bean>
<bean id="pointC" class="com.saravanansivaji.spring.Point">
<property name="X" value="10"></property>
<property name="Y" value="40"></property>
</bean>
<!-- MINI-SQUARE AREA Ends-->
From application file
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
MiniTriangle miniTriangle = (MiniTriangle) context.getBean("miniTriangle");
miniTriangle.draw();
Upon execution, the output is
Mini-Triangle drawn with the following points using AutoWire:
(10,10)
(10,20)
(10,40)
So Spring does this autowiring “byName”, when the member variable names of the bean class matches with any of the bean id’s name in the spring configuration file. Spring looks for a bean with the same name as the property that needs to be autowired.
Bean id names should be unique in the spring configuration xml document.
AutoWiring by Type (autowire=”byType“)
======================================
Let I execute the above same code with little modification in the spring configuration file document,
Just changed the attribute autowire to “byType”
spring.xml
<!-- MINI-SQUARE AREA Begins -->
<bean id="miniTriangle" class="com.saravanansivaji.spring.MiniTriangle" autowire="byType"/>
<bean id="pointA" class="com.saravanansivaji.spring.Point">
<property name="X" value="10"></property>
<property name="Y" value="10"></property>
</bean>
<bean id="pointB" class="com.saravanansivaji.spring.Point">
<property name="X" value="10"></property>
<property name="Y" value="20"></property>
</bean>
<bean id="pointC" class="com.saravanansivaji.spring.Point">
<property name="X" value="10"></property>
<property name="Y" value="40"></property>
</bean>
<!-- MINI-SQUARE AREA Ends-->
When we execute, we get the below spring exception,
Exception in thread “main” org.springframework.beans.factory.UnsatisfiedDependencyException
: Error creating bean with name ‘miniTriangle’ defined in class path resource [spring.xml]: Unsatisfied dependency expressed through bean property
‘pointA’: : No unique bean of type [com.saravanansivaji.spring.Point] is defined: expected single matching bean but found 3: [pointA, pointB, pointC]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException
:
No unique bean of type [com.saravanansivaji.spring.Point] is defined: expected single matching bean but found 3: [pointA, pointB, pointC]
Autowiring by Type – allows a property to be autowired if exactly one bean of the property type exists in the container. If more than one exists, a fatal exception is thrown, which indicates that you may not use byType autowiring for
that bean. If there are no matching beans, nothing happens; the property is not set.
AutoWiring by Constructor (autowire=”constructor“)
==================================================
Slightly modifying my bean class, removing all the setters as follows
MiniTriangle.java
package com.saravanansivaji.spring;
public class MiniTriangle {
private Point pointA;
private Point pointB;
private Point pointC;
public MiniTriangle(Point pointA, Point pointB, Point pointC) {
this.pointA = pointA;
this.pointB = pointB;
this.pointC = pointC;
}
public Point getPointA() {
return pointA;
}
public Point getPointB() {
return pointB;
}
public Point getPointC() {
return pointC;
}
public void draw() {
System.out.println("Mini-Triangle drawn with the following points using AutoWire-constructor:");
System.out.println("("+getPointA().getX()+","+getPointA().getY()+")");
System.out.println("("+getPointB().getX()+","+getPointB().getY()+")");
System.out.println("("+getPointC().getX()+","+getPointC().getY()+")");
}
}
spring.xml
<!-- MINI-SQUARE AREA Begins -->
<bean id="miniTriangle" class="com.saravanansivaji.spring.MiniTriangle" autowire="constructor"/>
<bean id="pointA" class="com.saravanansivaji.spring.Point">
<property name="X" value="10"></property>
<property name="Y" value="10"></property>
</bean>
<bean id="pointB" class="com.saravanansivaji.spring.Point">
<property name="X" value="10"></property>
<property name="Y" value="20"></property>
</bean>
<bean id="pointC" class="com.saravanansivaji.spring.Point">
<property name="X" value="10"></property>
<property name="Y" value="40"></property>
</bean>
<!-- MINI-SQUARE AREA Ends-->
Output:
Mini-Triangle drawn with the following points using AutoWire-constructor:
(10,10)
(10,20)
(10,40)
Happy Coding 😀