Java Login

About javalogin.com

Hello guys,
javalogin.com 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

Department.java

package autowiring;

public class Department
{
String departmentName;

public String getDepartmentName() {
return departmentName;
}

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

Employee.java

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) {
this.id = id;
}

public String getName() {
return name;
}

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

}

context.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframework.org/schema/context">

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

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

</beans>

Main.java

package autowiring;

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

public class Main {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext
("autowiring/context.xml");

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


     << Previous
Next >>