is a design pattern used to achieve Inversion of Control (IoC) between classes and their dependencies. Instead of a class creating its own dependencies, the Spring container injects them at runtime.
In other words It means giving an object its dependencies from the outside, rather than the object creating them itself.
public class Car {
private Engine engine = new Engine(); // tightly coupled
}
public class Car {
private Engine engine;
public Car(Engine engine) {
this.engine = engine;
}
}
Spring uses various annotations and configuration methods to determine what dependencies to inject:
@Autowired annotation - Tells Spring to automatically inject a dependency.@Component, @Service, @Repository annotations - Mark classes as Spring beans eligible for injection.@Configuration and @Bean - Used for Java-based configuration to define beans.@ComponentScan() - Used for scanning for the beans in a given package.@SpringBootApplication (which includes @ComponentScan, @Configuration, and @EnableAutoConfiguration).These mechanisms allow Spring to build a dependency graph and resolve what needs to be injected where.