Since SPRING framework is very common technology used in application based on Java language let’s dive into some basics about this tool. SPRING use a principle called Inversion of Control (IoC) which mean that the flow of program is inverted.Inversion of Control can be achieved by various mechanism, but Spring uses the Dependency Injection (DI) deisgn pattern. SPRING (more precisive SPRING IoC container) is magaging the lifecycle and dependencies of objects. It takes the responsibility off from developers to manage this by them. IoC container manages:
- object creation
- wiring objects
- lifecycle of objects
- configuration

Objects which are managed by IoC container call in SPRING as Beans. Each Bean can be injected in 3 different ways:
- Constructor Injection
@Component
public class Engine {
public void start() {
System.out.println("Engine started!");
}
}
@Component
public class Car {
private final Engine engine;
// Constructor injection
public Car(Engine engine) {
this.engine = engine;
}
public void drive() {
engine.start();
System.out.println("Car is driving!");
}
}
- Setter Injection
@Component
public class Engine {
public void start() {
System.out.println("Engine started!");
}
}
@Component
public class Car {
private Engine engine;
// Setter injection
public void setEngine(Engine engine) {
this.engine = engine;
}
public void drive() {
engine.start();
System.out.println("Car is driving!");
}
}
- Field Injection (Less Recommended)
@Component
public class Engine {
public void start() {
System.out.println("Engine started!");
}
}
@Component
public class Car {
// Field injection
private Engine engine;
public void drive() {
engine.start();
System.out.println("Car is driving!");
}
}
Question which is coming now to my mind is when use specific type od Injection? Below you will find some tips about that:
- Constructor Injection is preferred for mandatory dependencies since it ensures all dependencies are initialized at object creation.
- Setter Injection is suitable for optional dependencies or when dependencies might change after the bean is created.
- Field Injection should generally be avoided in production code due to issues with testability and dependency visibility.
As well there is 3 ways to define Beans, below there is three of them:
XML-Based Configuration – Beans can be defined in an XML configuration file. This was the original method used in Spring but is less common in modern applications.
<bean id="myBean" class="com.example.MyClass">
<property name="dependency" ref="dependencyBean" />
</bean>
Java-Based Configuration – Java-based configuration uses @Configuration
and @Bean
annotations to define beans in a Java class. This is a modern, type-safe, and preferred method in Spring.
@Configuration
public class AppConfig {
@Bean
public MyClass myBean() {
return new MyClass();
}
}
Annotation-Based Configuration -Annotations simplify bean definition by combining component scanning and @Autowired
for dependency injection. There are also many other annotations in Spring that extend simply @Component
which is a general-purpose stereotype annotation that marks a class as a Spring-managed bean. For example @Repository
@Service
@Controller
.
@Component
public class MyClass {
// This class will be automatically detected as a bean
}
@Repository
public class MyRepository {
// This class will be automatically detected as a bean
}
@Service
public class MyService {
// This class will be automatically detected as a bean
}
@Controller
public class MyController {
// This class will be automatically detected as a bean
}
To summarize this article here are key benefits of DI and IoC in Spring:
- Loose Coupling: Components are not tightly bound to their dependencies.
- Easier Testing: Mock dependencies can be easily injected during testing.
- Scalability: Applications become modular and easier to maintain or extend.
- Centralized Configuration: Beans and their dependencies are managed in a central container.