20.(c) Autowire - constructor

3.  Autowire -  constructor

The constructor mode injects the dependency by calling the constructor of the class. It calls the constructor having large number of parameters.

·        Spring Auto wiring by constructor is similar to spring auto wiring byType [internally it will considers as byType only ]
·        But with little difference, in byType we used setter injection here we have to use constructor injection
·         If you have 3 constructors in a class, zero-arg, one-arg and two-arg then injection will be performed by calling the two-arg constructor.

1.      Student.java
package bean;

public class Student {
       private int sno;
       private String name;
       private Address addr;
      
       //************ setAddr() Replaced by Constrctor
       public Student(Address addr) {
              this.addr = addr;
       }
      
       public Address getAddr() {
              return addr;
       }
       //************************************************
       public int getSno() {
              return sno;
       }
       public void setSno(int sno) {
              this.sno = sno;
       }
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       }
      

       public void getData(){
              System.out.println("********* Address ************");
              System.out.println(getSno()+" \n"+getName()+"\n D:no "+getAddr().getDno());
              System.out.println(getAddr().getCity()+"\n "+getAddr().getState());
             
       }

}

2.      Address.java
package bean;

public class Address {
       private int dno;
       private String city;
       private String state;
       public int getDno() {
              return dno;
       }
       public void setDno(int dno) {
              this.dno = dno;
       }
       public String getCity() {
              return city;
       }
       public void setCity(String city) {
              this.city = city;
       }
       public String getState() {
              return state;
       }
       public void setState(String state) {
              this.state = state;
       }   
     }

3.      Spring.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" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 


<bean id="xyz" class="bean.Address">
       <property name="dno" value="409"></property>
       <property name="city" value="HYDERABAD"></property>
       <property name="state" value="ANDHRA PRADESH"></property>
</bean>

<bean id="student" class="bean.Student" autowire="constructor">
<property name="sno" value="101"></property>
<property name="name" value="SATYA KAVETI"></property>
</bean>

4.   AutowireMain.java
package bean;

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

public class AutowireMain {

       public static void main(String[] args) {
             
              ApplicationContext context=new ClassPathXmlApplicationContext("res/spring.xml");
              Student student = (Student) context.getBean("student");
              System.out.println("----------------------------");
              student.getData();  
             
       }
      
}

 Output
----------------------------
********* Address ************
101
SATYA KAVETI
D:no 409
HYDERABAD
ANDHRA PRADESH