Home Tutorial Spring Spring3 Ioc Spring Autowire Autodetect

 
 

Spring Autowire Autodetect
Posted on: August 25, 2010 at 12:00 AM
Advertisement
In this tutorial you will see an example of spring autowiring autodetect mode.

Spring Autowire Autodetect

In this tutorial you will see an example of spring autowiring autodetect. When default constructor with no argument, it auto-wire by data type or it auto-wire by constructor.

Student.java

package spring.noautowiring.mode;

import org.springframework.beans.factory.annotation.Autowired;

public class Student {
  @Autowired
  private String age;
  private String name;
  private String address;

  public String getAge() {
    return age;
  }

  public void setAge(String age) {
    this.age = age;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }
  
  @Override
  public String toString() {
    return "Student [name=" + name + ", age=" + age + 
                          ",address=" + address + "]";
  }

}

College.java

package spring.noautowiring.mode;

import org.springframework.beans.factory.annotation.Autowired;

public class College {
  
  private Student student;
  private String registration;
  private String year;

  @Autowired
  public void setStudent(Student student) {
    this.student = student;
  }

  public String getRegistration() {
    return registration;
  }

  public void setRegistration(String registration) {
    this.registration = registration;
  }

  public String getYear() {
    return year;
  }

  public void setYear(String year) {
    this.year = year;
  }
  
  @Override
  public String toString() {
    return "College [registration=" + registration + ", Student=" + student + 
                          ",year=" + year + "]";
  }

}

AutowireMain.java

package spring.noautowiring.mode;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AutowireMain {
  public static void main(String[] args) {
    BeanFactory beanfactory = new ClassPathXmlApplicationContext(
        "context.xml");
    College coll = (Collegebeanfactory.getBean("college");
    System.out.println(coll);
  }
}

context.xml

<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-2.5.xsd">
 
 <!-- byName auto-Wiring mode -->
  <bean id="college" 
class="spring.noautowiring.mode.College" autowire="autodetect">
    
    <property name="registration" value="BL001" />
    <property name="year" value="2001" />
  </bean>

  <bean id="student" class="spring.noautowiring.mode.Student">
    <property name="address" value="Delhi" />
    <property name="age" value="22" />
    <property name="name" value="Raj" />
  </bean>
<!-- End -->

</beans>

When you run this application it will display output as shown below:


College [registration=BL001, Student=Student [name=Raj, age=22,address=Delhi],year=2001]

Download this example code

Advertisement

Related Tags for Spring Autowire Autodetect:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 25, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com