ArticlesProjectsWeeklyCredentialsAbout

Spring Dependency Injection Example

Spring XML bean wiring, constructor injection, and a JUnit test that needs no application server — showing exactly why Spring made Java testing practical.

springjavadependency-injection

Constructor injection, XML wiring, and a unit test with a stub DAO — the core Spring pattern.

Source code
# Spring Dependency Injection Example

```sh
# Run unit tests (no Spring context needed)
javac -cp junit.jar DeviceDao.java DeviceService.java DeviceServiceTest.java
java  -cp .:junit.jar junit.textui.TestRunner DeviceServiceTest

# Run with Spring context
javac -cp spring.jar:commons-dbcp.jar DeviceDao.java DeviceService.java
# Load beans.xml from application code: new ClassPathXmlApplicationContext("beans.xml")
```