Java Login


Hello guys, is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.

     << Previous
Next >>     

Bean Autowiring

In Spring framework, When you create a relationship between two beans, it is called bean auto-wiring. Usually we provide bean configuration details in the spring bean configuration file and we also specify the beans that will be injected in other beans using ref attribute. But Spring framework provides autowiring features too where we don't need to provide bean injection details explicitly.

The 5 different types of autowiring are 'no', 'byName', 'byType', 'constructor', and 'autodetect'. Autowiring is turned off by default, so the default autowiring mode for a bean is 'no'. Also, any constructor argument or property explicitly set will not be eligible for autowiring.

  1. <no> - No autowiring is performed. All references to other beans must be explicitly injected. This option is default for spring framework and it means that autowiring is OFF.
  2. <byName> - For this type of autowiring, setter method is used for dependency injection. Also the variable name should be same in the class where we will inject the dependency and in the spring bean configuration file.
  3. <byType> - Based on the type of class on a setter, if only one instance of the class exists in the IoC container it will be injected into this property. If there is more than one instance of the class a fatal exception is thrown.
  4. <constructor> - This is almost similar to autowire byType, the only difference is that constructor is used to inject the dependency.
  5. <autodetect> - If there is a valid constructor, the constructor autowiring mode will be chosen. Otherwise if there is a default zero argument constructor the byType autowiring mode will be chosen.

Example of Bean Auto-wiring byName

package autowiring;

public class Department
String departmentName;

public String getDepartmentName() {
return departmentName;

public void setDepartmentName(String departmentName) {
this .departmentName = departmentName;

package autowiring;

public class Employee {
private int id;
private String name;
Department dept;

public Department getDept() {
return dept;

public void setDept(Department dept) {
this.dept = dept;

public int getId() {
return id;

public void setId(int id) { = id;

public String getName() {
return name;

public void setName(String name) { = name;


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<bean id="emp" class="autowiring.Employee" autowire="byName">
<property name="id" value="1"/>
<property name="name" value="Test"/>

<bean id="dept" class="autowiring.Department">
<property name="departmentName" value="Marketing"/>


package autowiring;

import org.springframework.context.ApplicationContext;

public class Main {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext

Employee emp = (Employee)context.getBean("emp");
System.out.println(emp.getName()+" "

     << Previous
Next >>