In this post, We will learn about the Spring @Autowired Annotation With Constructor Injection Example using a Demo Project.
Spring Dependency Injection
- The dependency Injection is a fundamental aspect of the Spring framework through which the Spring framework container injects objects or instances into other objects or “dependencies”.
- In another word, We can say that dependency Injection promotes loose coupling of software components and moves the responsibility of managing components onto the Spring container.
Types of Spring Dependency Injection:
Setter Based Dependency Injection:
In setter-based DI, the container will call setter methods of the class after invoking a no-argument constructor or no-argument static factory method to instantiate the bean.
Constructor Based Dependency Injection:
The constructor-based dependency injection is accomplished when the Spring container invokes a class constructor with a number of arguments and each representing a dependency on the other class.
When to use setter injection and constructor injection?
The Spring documentation recommends using constructor-based injection for mandatory dependencies, and setter-based injection for optional Dependency
@Autowired annotation
Spring @Autowired annotation is mainly used for automatic dependency injection. This annotation may be applied to before class variables and methods for auto wiring byType. We can also use @Autowired annotation on the constructor for constructor-based spring auto wiring.
Now Let’s try to understand Constructor Based dependency injection(DI) using @Autowired Annotation With the help of the below demo Project.
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkhindigyan.org</groupId> <artifactId>AutowiredAnnotationWithConstructorDI</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <org.springframework.version>5.2.13.RELEASE</org.springframework.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> </dependencies> </project> |
Employee.java
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.kkhindigyan.model; import org.springframework.beans.factory.annotation.Autowired; public class Employee { private Integer employeeId; private String employeeName; private Double salary; private Address address; @Autowired public Employee(Integer employeeId, String employeeName, Double salary, Address address) { super(); this.employeeId = employeeId; this.employeeName = employeeName; this.salary = salary; this.address = address; } public Integer getEmployeeId() { return employeeId; } public String getEmployeeName() { return employeeName; } public Double getSalary() { return salary; } public Address getAddress() { return address; } @Override public String toString() { return "Employee [employeeId=" + employeeId + ", employeeName=" + employeeName + ", salary=" + salary + "]"; } } |
Address.java
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 30 31 32 33 34 35 36 37 38 39 |
package com.kkhindigyan.model; public class Address { private String addressLine1; private String addressLine2; private String city; private Long zipCode; public String getAddressLine1() { return addressLine1; } public void setAddressLine1(String addressLine1) { this.addressLine1 = addressLine1; } public String getAddressLine2() { return addressLine2; } public void setAddressLine2(String addressLine2) { this.addressLine2 = addressLine2; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public Long getZipCode() { return zipCode; } public void setZipCode(Long zipCode) { this.zipCode = zipCode; } @Override public String toString() { return "Address [addressLine1=" + addressLine1 + ", addressLine2=" + addressLine2 + ", city=" + city + ", zipCode=" + zipCode + "]"; } } |
applicationContext.xml
To enable @Autowired annotation in Spring Framework we have to use <context:annotation-config/> tag in the config file as below.
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 30 31 |
<?xml version="1.0" encoding="UTF-8"?> <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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="employee" class="com.kkhindigyan.model.Employee" scope="prototype"> <constructor-arg name="employeeId" value="102883" type="java.lang.Integer" index="0"></constructor-arg> <constructor-arg name="employeeName" value="Sean" type="java.lang.String" index="1"></constructor-arg> <constructor-arg name="salary" value="80000" type="java.lang.Double" index="2"></constructor-arg> <!-- <constructor-arg name="address" ref="address" type="com.kkhindigyan.model.Address" index="3"></constructor-arg> --> </bean> <bean id="address" class="com.kkhindigyan.model.Address"> <property name="addressLine1" value=""></property> <property name="addressLine2" value=""></property> <property name="city" value="Delhi"></property> <property name="zipCode" value="959049"></property> </bean> </beans> @Override public String toString() { return "Address [addressLine1=" + addressLine1 + ", addressLine2=" + addressLine2 + ", city=" + city + ", zipCode=" + zipCode + "]"; } } |
ClientTest.java
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 30 31 32 33 34 35 |
package com.kkhindigyan.org; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.kkhindigyan.model.Address; import com.kkhindigyan.model.Employee; public class ClientTest { public static void main(String[] args) { ApplicationContext applicationContext = null; try { //Creating Instance of ApplicationContext Spring Container applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //Asking Spring Container to return Spring bean with Specific Id or name Employee employee = (Employee) applicationContext.getBean("employee"); System.out.println(employee); Address address = employee.getAddress(); //Address address = (Address) applicationContext.getBean("address"); System.out.println(address); } catch (Exception e) { e.printStackTrace(); }finally { if (applicationContext != null) ((AbstractApplicationContext) applicationContext).close(); } } } |
If you run ClientTest.java as Java Application then it will give the below output:
1 2 |
Employee [employeeId=102883, employeeName=Sean, salary=80000.0] Address [addressLine1=, addressLine2=, city=Delhi, zipCode=959049] |
That’s all about Spring @Autowired Annotation With Constructor Injection Example
You May Also Like:
Spring BeanFactory Container Example
Spring ApplicationContext Container Example
Annotation-based Configuration in Spring Framework Example
Spring Java-based Configuration Example
Spring Setter Dependency Injection Example
Spring @Autowired Annotation With Setter Injection Example
Spring Constructor based Dependency Injection Example
Spring Autowiring byName & byType Example
getBean() overloaded methods in Spring Framework
Spring Inner bean example
Spring Dependency Injection with Factory Method
Spring Framework @Qualifier example
Injecting Collections in Spring Framework Example
Spring Bean Definition Inheritance Example
Spring bean scopes with example
Spring JSR-250 Annotations with Example
Spring BeanPostProcessor Example
Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring with Jdbc java based configuration example
Spring JDBC NamedParameterJdbcTemplate Example
How to call stored procedures in the Spring Framework?
If you have any feedback or suggestion please feel free to drop in below comment box.