Backend Development 5 min read

Why Use Spring? Understanding How It Solves Coupling Problems with Dependency Injection

This article compares traditional object creation with Spring’s dependency injection, demonstrating how injecting DAO beans via XML configuration decouples service and data layers, includes Java code examples, test results, and visual analogies of gear systems to illustrate reduced coupling.

IT Xianyu
IT Xianyu
IT Xianyu
Why Use Spring? Understanding How It Solves Coupling Problems with Dependency Injection

Purpose: compare traditional object creation with Spring’s injection to achieve decoupling, using a Service layer calling a Dao layer as example.

Method 1: Traditional Approach

1. Service layer

/**
 * @description : Service layer
 */
public class UserService {
    public void add() {
        System.out.println("service add...");
        UserDao dao = new UserDaoImpl();
        dao.query();
    }
}

2. UserDao interface

public interface UserDao {
    void query();
}

3. Implementation class

public class UserDaoImpl implements UserDao {
    @Override
    public void query() {
        System.out.println("dao query...");
    }
}

Test result shows that the Service directly creates the implementation; any failure in the Dao implementation immediately breaks the Service, indicating high coupling.

Conclusion 1: Directly invoking the implementation in UserService creates tight coupling; a problem in the implementation causes immediate failure.

Method 2: Spring Bean Injection

1. XML bean configuration

<?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="userService" class="Service.UserService">
        <!-- inject Dao object -->
        <property name="dao" ref="Dao"/>
    </bean>
    <bean id="Dao" class="Dao.UserDaoImpl"/>
</beans>

2. Service layer

import Dao.UserDao;
public class UserService {
    public void add() {
        System.out.println("service add......");
        dao.update();
    }
    private UserDao dao;
    public void setDao(UserDao dao) { this.dao = dao; }
}

3. UserDao interface

public interface UserDao {
    void update();
}

4. Implementation class

public class UserDaoImpl implements UserDao {
    @Override
    public void update() {
        System.out.println("Dao update......");
    }
}

Test code loads the XML configuration with Spring’s ApplicationContext and obtains the UserService bean, invoking add() without directly instantiating the Dao.

import Service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBean {
    @Test
    public void testBean() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }
}
Conclusion 2: By injecting the Dao via an external XML file, the Service no longer depends on a concrete implementation, achieving loose coupling.

Overall Summary

The traditional approach is likened to a tightly meshed gear set where a failure in one gear can halt the whole system. Spring’s injection acts like separate gears linked through a third‑party configuration, minimizing inter‑gear dependencies and allowing individual components to fail without affecting others.

JavaBackend DevelopmentIoCSpringdependency injectionInversion of Control
IT Xianyu
Written by

IT Xianyu

We share common IT technologies (Java, Web, SQL, etc.) and practical applications of emerging software development techniques. New articles are posted daily. Follow IT Xianyu to stay ahead in tech. The IT Xianyu series is being regularly updated.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.