Show List

Spring Dependency Injection using Annotations and Java Component Scan

This example shows different steps used for Spring dependency Injection using Annotations and Java Component Scan. Below is the structure of the project we are going to create for this example
1. Create Maven project (From Java IDE go to File => New => Project => Maven Project) and add spring-context dependency. pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>org.itcodescanner</groupId>
<artifactId>spring</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.22</version>
</dependency>
</dependencies>
</project>
2. Create Harddisk interface, Sanddisk and WesternDigital classes implementing Harddisk in com.dependencies package. Harddisk interface has signature for methods read and write. 
package com.dependencies;

public interface Harddisk {
public void read();
public void write();
}
Create Sandisk and WesternDigital classes implementing Harddisk and overriding read and write methods. These classes are created with @Component annotation so spring would automatically create the Sandisk and WesternDigital bean objects. 
package com.dependencies;

import org.springframework.stereotype.Component;

@Component
public class Sandisk implements Harddisk{
@Override
public void read() {
System.out.println("Reading from Sandisk");
}

@Override
public void write() {
System.out.println("Writing from Sandisk");
}
}
package com.dependencies;

import org.springframework.stereotype.Component;

@Component
public class WesternDigital implements Harddisk{
@Override
public void read() {
System.out.println("Reading from WD");
}

@Override
public void write() {
System.out.println("Writing from WD");
}
}
3. Create consumer classes Computer and Laptop in the com.devices package. We have used Qualifier to specify which bean is to be injected when there are multiple Harddisk beans in the context.
package com.devices;

import com.dependencies.Harddisk;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class Computer {

@Autowired
@Qualifier("sandisk")
private Harddisk harddisk;

public Computer() {
}


public void switchOn(){
System.out.println("Computer is switched on");
harddisk.read();
harddisk.write();
}

}
package com.devices;

import com.dependencies.Harddisk;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class Laptop {
@Autowired
@Qualifier("westernDigital")
private Harddisk harddisk;

public Laptop(){

}

public void switchOn(){
System.out.println("Laptop is switched on");
harddisk.read();
harddisk.write();
}
}
4. Create Java class ConfigClass with @Configuration and @ComponentScan annotations. @Configuration identifies the class as configuration bean and @ComponentScan annotation provides the packages for Spring to scan for the beans. Spring makes these beans available for the application.
package com.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.dependencies, com.devices")
public class ConfigClass {
}
5. Create Workstation (main) class which will load the application context passing the config class to create and wire beans. Get the required bean from the application context and execute the method.

From the console output we can see that Sandisk was processed as dependency in the computer object and WD was processed as dependency in the laptop object.
import com.config.ConfigClass;
import com.devices.Computer;
import com.devices.Laptop;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Workstation {

public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigClass.class);

Computer computer = (Computer) context.getBean("computer");
computer.switchOn();

Laptop laptop = (Laptop) context.getBean("laptop");
laptop.switchOn();
}
}
Output:
Computer is switched on
Reading from Sandisk
Writing from Sandisk
Laptop is switched on
Reading from WD
Writing from WD

Process finished with exit code 0


Source Code:
https://github.com/NumeroUnoDeveloper/spring-java-component-scan

    Leave a Comment


  • captcha text