Spring Dependency Injection using XML Configuration
springwiring.xml
configuration file.
<?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>
package com.dependencies;
public interface Harddisk {
public void read();
public void write();
}
package com.dependencies;
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;
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");
}
}
package com.devices;
import com.dependencies.Harddisk;
public class Computer {
private Harddisk harddisk;
public Computer(Harddisk harddisk) {
this.harddisk = harddisk;
}
public void switchOn(){
System.out.println("Computer is switched on");
harddisk.read();
harddisk.write();
}
}
package com.devices;
import com.dependencies.Harddisk;
public class Laptop {
private Harddisk harddisk;
public void setHarddisk(Harddisk harddisk) {
this.harddisk = harddisk;
}
public void switchOn(){
System.out.println("Laptop is switched on");
harddisk.read();
harddisk.write();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="harddiskA" class="com.dependencies.Sandisk" />
<bean id="harddiskB" class="com.dependencies.WesternDigital" />
<bean id="computer" class="com.devices.Computer">
<constructor-arg ref="harddiskA" />
</bean>
<bean id="laptop" class="com.devices.Laptop">
<property name="harddisk" ref="harddiskB" />
</bean>
</beans>
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("springwiring.xml");
Computer computer = (Computer) context.getBean("computer");
computer.switchOn();
Laptop laptop = (Laptop) context.getBean("laptop");
laptop.switchOn();
}
}
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
FAQ Section
1. What is the purpose of XML configuration in Spring?
XML configuration defines how Spring beans are created and wired together using tags and attributes in an external XML file.
2. What is the difference between constructor and setter injection?
Constructor injection provides dependencies at object creation time, while setter injection allows setting them after the object is created.
3. Can we mix constructor and setter injection in the same application?
Yes, Spring supports combining both types of injection for different beans as needed.
4. Is XML-based configuration still supported in modern Spring?
Yes. Although annotations and Java config are more popular, XML configuration is fully supported and useful in certain scenarios.
5. Where should I place the springwiring.xml
file?
Place it in the src/main/resources
directory so Spring can find it on the classpath.
6. What is ApplicationContext
?
It is the Spring container that manages beans and supports features like dependency injection, lifecycle management, and event propagation.
7. Is XML-based DI better than annotation-based?
Neither is objectively better—it depends on the use case. XML is more declarative and externalized; annotations are more concise and modern.
Leave a Comment