0

Hi I am newbee to Springs Framework I just want to knw what exactly Autowiring do in Spring framework?

Prabhakar Manthena
  • 2,223
  • 3
  • 16
  • 30

1 Answers1

0

Consider an example of Spring beans without autowiring first.

For e.g. Consider a class Customer

class Customer {
       private Person person;

       public Customer(Person person) {
        this.person = person;
       }

    public void setPerson(Person person) {
        this.person = person;
       }
 }

And class Person

class Person {

}

In spring bean configuration file the entries will be,

<bean id="customer" class="...Customer>
   <property name="person" ref="person" />
</bean>

<bean id="person" class="...Person" />

Using spring bean autowiring you can avoid writing the property tag inside the bean, in four different ways.

  1. Auto wiring by name

In this case the name of the property in Customer class must match with the id of the bean which you want to autowire i.e 'person'

Hence the entry of the Customer bean can be re-written as

<bean id="customer" class="...Customer" autoWire="byName" />

Similarly there are other auto wiring modes in spring, auto wiring byType, constructor, autodetect. You can refer link http://www.mkyong.com/spring/spring-auto-wiring-beans-in-xml/

Hetal Rachh
  • 1,393
  • 1
  • 17
  • 23