Reading:  

Quick to the point introduction to Spring Framework


Auto Wiring

User can inject the object dependency implicitly by using Auto-wiring framework of spring. We cannot inject string and primitive values by using this Auto-wiring and it works only with reference.

The Auto-wiring modes of Spring are:

  • no: It is Default, no auto wiring by default need to set manually by using “ref” attribute
  • byName: It is used to inject the dependency object according to the bean name and it Auto wiring by name property.
  • byType: It is used to inject the dependency object according to the bean type and it Auto wiring by data type property.
  • constructor: It is used to inject the dependency by calling the class constructor.
  • autodetect: “autowired by constructor” is used for a default constructor or else use “autowire by type”.

 

byName autowiring mode:

The id of bean and reference name must be same in the byName auto-wiring mode.

Example:

Java file: Student

package pack1;  
public class Student {  
private Student_Details  stud;  

public void setName(Student stud) {
      System.out.println("Inside setName");
      this.stud = stud;
   }
public Student getName() {
      return stud;
   }
public void display(){
      stud.Stud_Address();
   }
}  
 

Java file: Student_Details

package pack1;  

public class Student_Details {  
  
    public Student_Details ()   {
       System.out.println("Inside Student_Details  constructor" );
    }
    public void Stud_Address(){
       System.out.println("Inside Stud_Address");
    }
}  
 

Java file: ExmTest

package pack1;  
  
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
  
public class ExmTest {  
      public static void main(String[] args) {  
        ApplicationContext cont = new ClassPathXmlApplicationContext("Beans.xml");
        Student st = (Student) cont.getBean("studentBean");
        st. Stud_Address();
      }  
}  
 

The configuration file is Beans.xml:

To use auto-wiring 'byName', then XML configuration file as shown below:

<?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="studentBean" class="pack1.Student" autowire="byName">
            <property name="stud" value="Suman"/>
   </bean>
         <bean id=" Student_Details" class="pack1.Student_Details"/>
       </property>
   </bean>
</beans>

Compile and run the program will get the below result:


Result:

Inside Student_Details constructor
Inside setName
Inside Stud_Address

byType autowiring mode:

The id of bean and reference name may be different in the byType auto-wiring mode.

Example:

Java file: Student

package pack1;
public class Student {
	private Student_Details stud;

	public void setName(Student stud) {
		System.out.println("Inside setName");
		this.stud = stud;
	}
	public Student getName() {
		return stud;
	}
	public void display() {
		stud.Stud_Address();
	}
}

Java file: Student_Details

package pack1;

public class Student_Details {

	public Student_Details() {
		System.out.println("Inside Student_Details  constructor");
	}
	public void Stud_Address() {
		System.out.println("Inside Stud_Address");
	}
}

Java file: ExmTest

package pack1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ExmTest {
	public static void main(String[] args) {
		ApplicationContext cont = new ClassPathXmlApplicationContext("Beans.xml");
		Student st = (Student) cont.getBean("studentBean");
		st.Stud_Address();
	}
}

The XML configuration file by using auto-wiring 'byType', is as shown below:

<?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="studentBean" class="pack1.Student" autowire="byType">
            <property name="stud" value="Suman"/>
   </bean>
         <bean id=" Student_Details" class="pack1.Student_Details"/>
       </property>
   </bean>
</beans>
 

Compile and run the program will get the below result:


Result:


Inside Student_Details constructor
Inside setName
Inside Stud_Address

constructor autowiring mode:

Spring container injects the dependency by parameterized constructor when using constructor auto-wiring mode.

For the above Java program the XML configuration file by using auto-wiring 'constructor', is as shown below:

<?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="studentBean" class="pack1.Student" autowire="constructor">
            <property name="stud" value="Suman"/>
   </bean>
         <bean id=" Student_Details" class="pack1.Student_Details"/>
       </property>
   </bean>
</beans>

 

no autowiring mode:

Spring container doesn't inject the dependency by auto-wiring mode when using no auto-wiring mode.

For the above Java program the XML configuration file by using auto-wiring 'no', is as shown below:

<?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="studentBean" class="pack1.Student" autowire="no">
            <property name="stud" value="Suman"/>
   </bean>
         <bean id=" Student_Details" class="pack1.Student_Details"/>
       </property>
   </bean>
</beans>

 

Autowiring Advantage:

  • Here user need not to write the code to inject the dependency explicitly, hence it requires the minimum code.

Autowiring Disadvantage:

  • Autowiring doesn’t support string and primitive values.
  • If possible prefer using explicit wiring because Auto-wiring is not much same than explicit wiring.
  • There is no control by user.
  • User cannot autowire the properties like Strings classes and also primitives.

 

Description

This tutorial covers various topics releated to Spring Framework as listed below

  • Framework Overview
  • Modules
  • Environment Setup
  • Saying Hello World with Spring
  • IoC Containers
  • Bean Definition
  • Bean Scopes 
  • Bean Life Cycle
  • Bean Post Processors
  • Bean Definition Inheritance
  • Dependency Injection
  • Injecting Inner Beans
  • Injecting Collection
  • Beans Auto-Wiring
  • Annotation Based Configuration
  • Java Based Configuration
  • Event Handling in Spring
  • Custom Events in Spring
  • JDBC Framework Overview
  • Transaction Management

 

 



Prerequisites

Prior knowledge of Java is essential

Audience

Beginners or students seeking quick introduction to Spring

Learning Objectives

This tutorial is for beginners seeking quick introduction to Spring Framework.

Author: Subject Coach
Added on: 22nd Jun 2015

You must be logged in as Student to ask a Question.

None just yet!