Popular Feed

Autowiring in Spring using XML


What is Autowiring?

Autowiring helps you to resolve dependency injection automatically. 

You need not give any hints to the IOC container for searching bean instances.

The spring container can autowire relationships between collaborating beans without using 

<constructor-arg> and <property> elements.

REAL USE Autowiring is – Its helps us to cut down on the amount of xml configuration 

To perform autowiring of beans, spring provides us an attribute - autowire


  1. byName

  2. byType

  3. constructor

  4. default 

  5. no


byname- In this case, spring framework attempts to find out a bean in the configuration file, whose id is matching with the property name to be wired.

If a bean is found with id as property name then that class object will be injected into that property by calling setter injection. If no id is found the that property 

Remains un-wired, but never throws any exception.

autowire spring


Example byname-

package com.javaupgrad.atuowire;
public class Heart {
	public void pump() {
		// TODO Auto-generated method stub

		System.out.println("start pumping");
	}
}


package com.javaupgrad.atuowire;
public class Human {
	private Heart heart; 

	public void setHeart(Heart heart) {
		this.heart = heart;
	}
	public void startPumping(){
		heart.pump();
	}
}


Main class- Name Body

package com.javaupgrad.atuowire;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.javaupgrad.loose.Student;
public class Body {
	public static void main(String[] args) {
		ApplicationContext context=new ClassPathXmlApplicationContext("autowire.xml");
		Human human=context.getBean("human", Human.class);
		human.startPumping();
	}
}

Configure xml file name- autowire.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"

    xsi:schemaLocation="http://www.springframework.org/schema/beans 

      http://www.springframework.org/schema/beans/spring-beans.xsd"> 


<bean id="heart" class="com.javaupgrad.atuowire.Heart">

</bean>


<bean id="human" class="com.javaupgrad.atuowire.Human" autowire="byName">

</bean>

 

</bean>

</beans>

byType- 

If a bean is found with class as property type then that class object will be injected into that property by calling setter injection. If no class found the property remains unwired, but never throws any exception just like before.



Example byType-

First create one class name Heart.

package com.javaupgrad.atuowire;
public class Heart {
	public void pump() {
		System.out.println("start pumping");
	}
}

package com.javaupgrad.atuowire;
public class Human {
	private Heart heart; 
	public void setHeart(Heart heart) {
		this.heart = heart;
	}
	public void startPumping(){
		heart.pump();
	}
}

package com.javaupgrad.atuowire;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.javaupgrad.loose.Student;
public class Body {
	public static void main(String[] args) {
		ApplicationContext context=new ClassPathXmlApplicationContext("autowire.xml");
		Human human=context.getBean("human", Human.class);
		human.startPumping();
	}
}

Configure xml file name- autowire.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"

    xsi:schemaLocation="http://www.springframework.org/schema/beans 

      http://www.springframework.org/schema/beans/spring-beans.xsd"> 


<bean id="heartabc" class="com.javaupgrad.atuowire.Heart">

</bean>


<bean id="human" class="com.javaupgrad.atuowire.Human" autowire="byType">

</bean>

 

</bean>

</beans>

Constructor  autowiring

In the case of constructor autowiring mode, spring container injects the dependency by highest parameterized constructor .

if you have 3 constructor in a class then a well performed highest parameterized constructor.

Example Constructor  autowiring

package com.javaupgrad.atuowire;
public class Heart {
	public void pump() {
		System.out.println("start pumping");
	}
}

package com.javaupgrad.atuowire;
public class Human {
	private Heart heart; 
	public void setHeart(Heart heart) {
		this.heart = heart;
	}
	public void startPumping(){
		heart.pump();
	}
}

Main class- Name Body

package com.javaupgrad.atuowire;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext
import com.javaupgrad.loose.Student;
public class Body {
	public static void main(String[] args) {
		ApplicationContext context=new ClassPathXmlApplicationContext("autowire.xml");
		Human human=context.getBean("human", Human.class);
		human.startPumping();
	}
}

Configure xml file name- autowire.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"

    xsi:schemaLocation="http://www.springframework.org/schema/beans 

      http://www.springframework.org/schema/beans/spring-beans.xsd"> 


<bean id="heart" class="com.javaupgrad.atuowire.Heart">

</bean>


<bean id="human" class="com.javaupgrad.atuowire.Human" autowire="constructor">

</bean>

 

</bean>

</beans>




No comments: