# Spring & Dependency Injection — Advanced Java

Source: https://www.geekswithgeeks.com/en/advanced-java/adv-spring-intro

> See how the Spring IoC container builds and injects your objects via constructor injection, and how Spring Boot auto-configures a runnable app from the classpath.

## IoC container

Instead of a class creating its collaborators with `new`, the Spring **container** builds them and **injects** them (constructor injection preferred). This makes wiring configurable and code trivially testable with fakes/mocks.

## Spring Boot

Boot auto-configures sensible defaults from the classpath, embeds a web server, and runs as a plain `java -jar`. Annotate a bean, declare its dependencies in the constructor, and Boot does the rest.

```java
@Service
class OrderService {
    private final OrderRepository repo;
    OrderService(OrderRepository repo) { this.repo = repo; }  // injected
}

@SpringBootApplication
public class App {
    public static void main(String[] a) { SpringApplication.run(App.class, a); }
}
```
