Spring Autowiring byType with Example

In Spring "AutoWiring byType" means Autowiring by property data type. Allows a property to be autowired if exactly one bean of the property type exists in the container. If more than one exists, a fatal exception is thrown, which indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens; the property is not set. We can Enable This Feature by Adding "autowire = "byType" to The bean Tag in Spring Configuration File(i.e.applicationContext.xml). If Property Type in spring bean and bean class name in application match then Spring Container will do dependency Injection by using Setter Method.

So Friends with out wasting time We will See Example

Tools Used In This Article

  • Spring 4.3.2
  • Maven 4.0.0
  • JDK 1.7

Project Directory Structure

Please See The Complete Project Directory Structure below


Maven Dependencies

Add The Following Dependencies to the pom.xml

  • spring-core
  • spring-context

<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.practice</groupId>
<artifactId>autowiringbynamedemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
</dependencies>
</project>

Laptop.java


package com.practice;

public class Laptop {
private String laptopName;
private Model model;
public String getLaptopName() {
return laptopName;
}
public void setLaptopName(String laptopName) {
this.laptopName = laptopName;
}
public Model getModel() {
return model;
}
public void setModel(Model model) {
this.model = model;
}
@Override
public String toString() {
return "Laptop [laptopName=" + laptopName + ", model=" + model + "]";
}



}


Model.java


package com.practice;

public class Model {
private int modelNumber;

public int getModelNumber() {
return modelNumber;
}

public void setModelNumber(int modelNumber) {
this.modelNumber = modelNumber;
}

@Override
public String toString() {
return "Model [modelNumber=" + modelNumber + "]";
}


}

Setting up The Spring Configuration File

In Spring We need to Enable autowire=byType like below.

applicationContext.xml


<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-4.3.xsd">
<bean id = "laptop" class = "com.practice.Laptop" autowire = "byType">
<property name = "laptopName value = "xxxx"></property>
</bean>
<bean id = "modelBean" class = "com.practice.Model">
<property name = "modelNumber" value = "1567"></property>
</bean>
</beans>

Let's create Test class and Run The code


package com.practice.test;


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

import com.practice.Laptop;

public class AppTest {


public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Laptop account = (Laptop)context.getBean("laptop");
System.out.println(account.toString());


}

}


output


Laptop [laptopName=xxxx, model=Model [modelNumber=1567]]