20 .(a ) Autowire byName

1.  Autowire byName

·         So far we are injected data manually using spring.xml
·         Autowire is the concept it automatically inject the Dependencies
·         By default Auto wiring is disabled in Spring Framework
·         We have to enable by setting autowire=”” property in spring.xml
<bean id="employee" class="EmployeeBean" autowire="byName">
        <property name="fullName" value="Satya Kaveti/>
·         It supports only Secondary Data (Objects), it won’t support primitive data
·         IoC Automatically it can wires secondary data using both setter &const Injection
·         IoC can take care of DI

·         We have five types of auto wiring
1.    byname               :        Setter Injection
2.    byType                 :        Setter Injection
3.    Constructor        :        Constructor Injection
4.    autoDetect         :        Setter/ Constructor Injection
5.    None

·        We have two classes Student [int id, String name, Address addr] and Address[int dno, String city, String state]
·        Here Address Object is created as a Data member in Student Class
·        So, 1st we have to configure Address Class data.
·        So here Address Object is Ready, we have to autowire this property in Student Class using autowire=”byname/byType”

1.    byName               :        Setter Injection
The byName mode injects the object dependency according to name of the bean. In such case, property name and bean name must be same. It internally calls setter method.
·         Here it compares the Names
·         Means in compares the bean id in xml – with – Object name which is declared in Java Class
·         It uses setter Injection
·         If no id is found then that property remains un-wired, but never throws any exception.
·         Example
1.      Student.java
package bean;

public class Student {
   private int sno;
   private String name;
   private Address addr;----------------à(1)
   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 Address getAddr() {
          return addr;
   public void setAddr(Address addr) {
          this.addr = addr;
   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"?> 

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

<bean id="student" class="bean.Student" autowire="byName"-----------------------(3)
<property name="sno" value="101"></property>
<property name="name" value="SATYA KAVETI"></property>

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");

(1)   à Address object name is “addr” this is must match with id name in
(2)   ----à (2) in xml file
(3)   -à here we mentioned autowire=”byName ” so it’s compare with name of object
********* Address ************
D:no 409