Practically bean dependencies are explicitly set in bean configuration files and it is really is a good practice to follow. But Spring is capable of automatically resolving dependencies at runtime. This automatic resolution of bean dependencies is also called autowiring. This type of bean dependencies can also be referred to as collaborating beans or just as collaborators.
There are 5 different types of autowiring modes which are ‘no’, ‘byName’, ‘byType’, ‘constructor’, and ‘autodetect’. In this post, I am taking down ‘byType‘ mode.
Autowiring by type allows a property to be autowired if there is exactly one bean of the property type in the container. If there is more than one, a fatal exception is thrown, and this indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens; the property is not set. If this is not desirable, setting the dependency-check=”objects” attribute value specifies that an error should be thrown in this case.
Sections in this post: Enable annotations support Autowire dependency using @Autowired Test the dependency
Enable annotations support
We must first enable the annotation configuration support in application because we will be using @Autowired annotation to plug-in the dependency. Annotations are enabled with following tag in bean configuration file.
A typical bean configuration file (e.g. applicationContext.xml) will look like this:
<? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://www.springframework.org/schema/beans < context:annotation-config /> < bean id = "employee" class = "com.howtodoinjava.autowire.byType.EmployeeBean" autowire = "byType" > < property name = "fullName" value = "Lokesh Gupta" /> </ bean > < bean id = "department" class = "com.howtodoinjava.autowire.byType.DepartmentBean" > < property name = "name" value = "Human Resource" /> </ bean > </ beans > |
Autowire dependency using @Autowired
In above configuration, I have enabled the autowiring by type for ’employee’ bean. It has been done using:
autowire="byType"
Now in EmployeeBean.java, we have to use @Autowired annotation like this:
package com.howtodoinjava.autowire.byType; import org.springframework.beans.factory.annotation.Autowired; public class EmployeeBean { @Autowired private DepartmentBean departmentBean; private String fullName; public DepartmentBean getDepartmentBean() { return departmentBean; } public void setDepartmentBean(DepartmentBean departmentBean) { this .departmentBean = departmentBean; } public String getFullName() { return fullName; } public void setFullName(String fullName) { this .fullName = fullName; } } |
And DepartmentBean looks like this which has been set:
package com.howtodoinjava.autowire.byType; public class DepartmentBean{ private String name; public String getName() { return name; } public void setName(String name) { this .name = name; } } |
Test the dependency
To test that bean has been set properly, run following code:
package com.howtodoinjava.autowire.byType; 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[] { "com/howtodoinjava/autowire/byType/application-context.xml" }); EmployeeBean employee = (EmployeeBean)context.getBean( "employee" ); System.out.println(employee.getFullName()); System.out.println(employee.getDepartmentBean().getName()); } } Output: Lokesh Gupta Human Resource |
Clearly, dependency was injected by type successfully.
Pingback: Spring Beans Autowiring Tutorial | Unsekhable