Spring autowiring by constructor
Practically bean dependencies are explicitly set in bean configuration files and it is really is a good practice to follow. ButSpring 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 ‘constructor‘ mode.
Autowiring by constructor is similar to byType, but applies to constructor arguments. In autowire enabled bean, it will look for class type of constructor arguments, and then do a autowire by type on all constructor arguments. Please note that if there isn't exactly one bean of the constructor argument type in the container, a fatal error is raised.
Sections in this post:
Autowire dependency using constructor
Create constructor dependency
Test the dependency
Autowire dependency using constructor
Autowiring by constructor is enabled by using autowire=”constructor” in bean definition in configuration file (i.e. application-context.xml).
A typical bean configuration file will look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
< beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:context = "http://www.springframework.org/schema/context" 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"> < context:annotation-config /> < bean id = "employee" class = "com.howtodoinjava.autowire.constructor.EmployeeBean" autowire = "constructor" > < property name = "fullName" value = "Lokesh Gupta" /> </ bean > < bean id = "department" class = "com.howtodoinjava.autowire.constructor.DepartmentBean" > < property name = "name" value = "Human Resource" /> </ bean > </ beans > |
Create constructor dependency
In above configuration, I have enabled the autowiring by constructor for ‘employee’ bean. It has been done by passing constructor arguments.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| package com.howtodoinjava.autowire.constructor; public class EmployeeBean { private String fullName; public EmployeeBean(DepartmentBean departmentBean) { this .departmentBean = departmentBean; } private DepartmentBean departmentBean; 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:
1
2
3
4
5
6
7
8
9
10
11
| package com.howtodoinjava.autowire.constructor; 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| package com.howtodoinjava.autowire.constructor; 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/constructor/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 constructor successfully.
Happy Learning !!
1 comment:
www0626
adidas soccer shoes
ugg outlet
michael kors outlet
canada goose outlet
ray ban sunglasses
ray ban sunglasses
rockets jerseys
canada goose outlet
true religion jeans
warriors jerseys
Post a Comment