In this post, We will learn about the Spring Autowiring byName & byType Example using a Demo Project
Autowiring in Spring
The autowiring in the spring framework enables us to inject the object dependency implicitly. It’s internally using setter or constructor injection.
One thing You should keep in mind that auto wiring can’t be used to inject primitive and string data types values. It works with Custom Object/reference only.
Advantage of Autowiring
When we use auto wiring then we require less code because we don’t need to write the code to inject the dependency in the config file explicitly.
Disadvantage of Autowiring
The downside is that the programmer has no control over auto wiring. and auto wiring can’t be used for primitive and string data types.
Autowiring modes
There are many auto wiring modes:
No. | Mode | Description |
---|---|---|
1 | no | It is the default auto wiring mode. It means no auto wiring by default. |
2 | byName | The byName auto wiring mode injects the bean dependency according to the name of the bean. In such a case, the property name and Spring bean name must be the same. It internally calls the setter method to inject dependency at runtime. |
3 | byType | The byType mode injects the bean dependency according to bean type. Here property name and bean name may be different. It internally calls the setter method to inject dependency at runtime. |
4 | constructor | The constructor auto wiring mode injects the bean dependency by calling the constructor of a bean class. It usually calls the constructor to inject dependency at runtime. |
5 | autodetect | This auto wiring mode is deprecated since Spring 3. |
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<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>AutowireByNameAndTypeExample</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 |
package com.kkhindigyan.model; public class Employee { private Integer employeeId; private String employeeName; private Double salary; private Address address; public Integer getEmployeeId() { return employeeId; } public void setEmployeeId(Integer employeeId) { this.employeeId = employeeId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public void setAddress(Address address) { this.address = address; } 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
We need to use autowire attribute of the bean element to apply the autowire modes 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 |
<?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"> <bean id="employee" class="com.kkhindigyan.model.Employee" autowire="byType"> <property name="employeeId" value="102883"></property> <property name="employeeName" value="Sean"></property> <property name="salary" value="80000"></property> <!-- <property name="address" ref="address"></property> --> </bean> <bean class="com.kkhindigyan.model.Address"> <property name="addressLine1" value="address line 1"></property> <property name="addressLine2" value="address line 2"></property> <property name="city" value="Delhi"></property> <property name="zipCode" value="959049"></property> </bean> </beans> |
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 |
package com.kkhindigyan.org; 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) { AbstractApplicationContext 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) applicationContext.close(); } } } |
If you run ClientTest.java as Java Application then it will give the below output:
Address [addressLine1=address line 1, addressLine2=address line 2, city=Delhi, zipCode=959049]
That’s all about Spring Autowiring byName & byType 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 @Autowired Annotation With Constructor Injection 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.