chk.buddi
chk.buddi

Reputation: 603

What is the advantage of @Autowired annotaion?

In my spring example ,I declared two beans with following XML configuration file.

EmployeeBean.java

package autowire;

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

public class EmployeeBean {
    private String fullName;  
    @Autowired
    private DepartmentBean departmentBean; 


    public String getFullName() {
        return fullName;
    }
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
    public DepartmentBean getDepartmentBean() {
        return departmentBean;
    }


}

DepartmentBean.java

package autowire;

public class DepartmentBean {
    private String name;

    public String getName() {
        return name;
    }

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

}

spring-servlet.xml

<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"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">


    <context:annotation-config />

    <bean id="employee" class="autowire.EmployeeBean" autowire="byType">
      <property name="fullName" value="Charith"></property>
    </bean>

    <bean id="deptment" class="autowire.DepartmentBean">
      <property name="name" value="IT Department"></property>
    </bean>


</beans>

TestAutowire .java

package autowire;

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

public class TestAutowire {

    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext(new String[] {"spring-servlet.xml"});

      EmployeeBean employee = (EmployeeBean)context.getBean("employee");
      System.out.println(employee.getFullName());
      System.out.println(employee.getDepartmentBean().getName());

    }

}

Above example is woking fine.After that I removed '@Autowired' annotation and add following lines to EmployeeBean.java

public void setDepartmentBean(DepartmentBean departmentBean) {
    this.departmentBean = departmentBean;
}

Now the example working fine with same output.My question is , What is the actual benefit when used '@Autowired' annotation?Because the code working fine without annotation but with setter method also.Pls help me.

Upvotes: 0

Views: 452

Answers (1)

Stephan
Stephan

Reputation: 43013

@AutoWired can be useful as it saves you time writing "wiring" code. You don't have to call somewhere in your code the setDepartment method to init your object. Spring will do it for you.

In your case, see below how to accomplish this using annotations only. Note the use of @Component annotation for indicating auto scan components to Spring. Also note that no XML file is now needed.

EmployeeBean.java

package autowire;

/* Imports go here ... */

@Component
public class EmployeeBean {
    private String fullName;

    @Autowired
    private DepartmentBean departmentBean; 

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public DepartmentBean getDepartmentBean() {
        return departmentBean;
    }   
}

DepartmentBean.java

package autowire;

/* Imports go here ... */

@Component
public class DepartmentBean {
    private String name;

    public String getName() {
        return name;
    }

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

TestAutowire .java

package autowire;

/* Imports go here ... */

public class TestAutowire {    
    public static void main(String[] args) {
      ApplicationContext context = new AnnotationConfigApplicationContext();
      context.scan("autowire");
      context.refresh();

      EmployeeBean employee = (EmployeeBean)context.getBean("employee");
      System.out.println(employee.getFullName());
      System.out.println(employee.getDepartmentBean().getName());
    }
}

References:

Upvotes: 1

Related Questions