Achieve Loose Coupling in Java with Spring Core and XML Configuration
18/05/25
5 mins
Spring Framework is one of the most popular Java frameworks for building enterprise-level applications. One of its core strengths is dependency injection (DI), which allows for better code modularity, testability, and flexibility.
In this blog, we’ll explore Spring Core using the XML configuration approach, using a simple Course example to demonstrate the concept of loose coupling through interfaces.
What Is Spring Core ?
Spring Core is the heart of the Spring Framework. It provides the features of Inversion of Control (IoC) and Dependency Injection (DI). These features allow objects to be created and wired together by the Spring container, not manually by developers.
Tight Coupling vs Loose Coupling
Without Spring (Tight Coupling):
public class College { public static void main(String[] args) { JavaCourse course = new JavaCourse(); // Direct object creation course.getTheCourse(5000); } }
If we wanted to switch to a different course ( e.g., SpringCourse ), we'd have to manually change and recompile code — this is tight coupling.
With Spring Core (Loose Coupling via XML)
Let’s decouple the logic by using an interface and inject the desired implementation using Spring’s XML config.
Step 1: Define the Interface and Implementations
// ICourse.java public interface ICourse { void getTheCourse(int price); }
// JavaCourse.java public class JavaCourse implements ICourse { public void getTheCourse(int price) { System.out.println("Java course costs ₹" + price); } }
// SpringCourse.java public class SpringCourse implements ICourse { public void getTheCourse(int price) { System.out.println("Spring course costs ₹" + price); } }
Step 2: Create the Main Class (Dependency Injection (DI))
// College.java public class College { private ICourse course; public void setCourse(ICourse course) { this.course = course; } public void start() { course.getTheCourse(6000); } }
This uses setter-based injection, which Spring will handle.
Step 3: Define the Beans in (XML Inversion of Control (IoC))
Create a file named applicationconfig.xml:
<?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="javaCourse" class="com.college.JavaCourse"/> <bean id="springCourse" class="com.college.SpringCourse"/> <bean id="college" class="com.college.College"> <property name="course" ref="springCourse"/> </bean> </beans>
You can switch to javaCourse anytime without touching the Java code.
Step 4: Load the Context and Run
// MainApp.java import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationconfig.xml"); College college = (College) context.getBean("college"); college.start(); } }
Summary of Key Concepts
Term | Description |
---|---|
IoC | Inversion of Control: Objects are created/managed by the Spring container |
DI | Dependency Injection: Dependencies are provided instead of being created manually |
XML Config | Bean configuration is done in XML files |
Loose Coupling | Objects depend on interfaces, not concrete implementations |