Show List
Spring Dependency Injection using Annotations and XML 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. These methods will be overridden in Sandisk and WesternDigital classes.
package com.dependencies;
public interface Harddisk {
public void read();
public void write();
}
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.
There is switchOn method which calls read and write methods of the harddisk.
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 xml file springcomponentscan.xml in the resources folder with component-scan attribute.
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.dependencies, com.devices" />
</beans>
5. Create Workstation (main) class which will load the application context passing the xml 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.devices.Computer;
import com.devices.Laptop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Workstation {
public static void main(String[] args) {
ApplicationContext context
= new ClassPathXmlApplicationContext("springcomponentscan.xml");
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
Leave a Comment