20 (d ) Autowire - autodetect

4.  Autowire -  autodetect

It is deprecated since Spring 3.

·         Spring Autowiring by autodetect uses either of two modes i.e. constructor or byType modes. First it will try to look for valid
·         Constructor with arguments, If found the constructor mode is chosen. If there is no constructor defined in bean, or
·         Explicit default no-args constructor is present, the autowire byType mode is chosen.
·        It is deprecated since Spring 3..

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 void setAddr(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="autodetect">
<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




Post a Comment

Thank You

Previous Post Next Post